qt 显示basler灰度图片

图像的数据是以字节为单位保存的,每一行的字节数必须是4的整数倍,不足的补0。
如果不是整数倍,则根据公式: linebyte = ( width * bitcount + 31 )/32 * 4;
其中bitcount为图像像素的位数,对于灰度图片QImage::Format_Indexed8,bitcount=8。
当width不为4的整数倍的时候,直接使用QImage::QImage ( uchar * data, int width, int height, Format format )这个构造函数就会出现问题,需要将每一行的字节补足成4的整数倍。

		int biBitCount = 8; //灰度图像像素bit数 
		int lineByte = (nWidth * biBitCount + 31 )/32 * 4;//补足后的字节数
        if (nWidth == lineByte)
        {
     
            QImage imgBuff(buff, nWidth, nHeight, QImage::Format_Indexed8);
            imgBuff.setColorTable(vcolorTable);
            OutImage = imgBuff;
        }
        else
        {
     
            BYTE* qBuffer = new BYTE[lineByte * nHeight]; //分配内存
            uchar* QImagePtr = qBuffer;
            for (int i = 0; i < nHeight; i++) //Copy line by line 
            {
     
                memcpy(QImagePtr, buff, nWidth);//将图片放入新的内存

                QImagePtr += lineByte;

                buff += nWidth;

            }
            QImage imgBuff(qBuffer, nWidth, nHeight, QImage::Format_Indexed8);  //封装QImage
            imgBuff.setColorTable(vcolorTable); //设置颜色表  
            OutImage = imgBuff;
        }        

参考
https://blog.csdn.net/boyemachao/article/details/90212477?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_utm_term-1&spm=1001.2101.3001.4242

https://blog.csdn.net/lyc_daniel/article/details/9014043?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

你可能感兴趣的:(QT)