阅读OpenCV Mat 16bit到QImage 8bit Greyscale

问题描述 投票:0回答:1

我想做的事

  1. 文件:TIFF,2页,灰度,16位
  2. OpenCV:2x Mat,灰度,16位(CV_16UC1)
  3. Qt:2x QImage,灰度,8位(Format_Greyscale8)

我想读一个16位的Tiff,然后将其转换为8bit。

void ImgProc::Load_Image_2xTiff_16bit_G(Mat *Mat_A_in, Mat *Mat_B_in, string file_name)
{  
    vector<Mat> vec_Mat;
    vec_Mat.reserve(2);

    imreadmulti(file_name, vec_Mat);

    *Mat_A_in = vec_Mat[0];
    *Mat_B_in = vec_Mat[1];
}

转变

void ImgProc::Convert_Mat16_2_QIm8(QImage *QIm_Out, Mat *Mat_In, double scale_factor)
{
    unsigned int rows = Mat_In->rows;
    unsigned int cols = Mat_In->cols;
    *QIm_Out = QImage(cols, rows, QImage::Format_Grayscale8);
    unsigned char* line_QIm;

    if (!Mat_In->data)
        return;

    for(unsigned int y = 0; y < rows; y++)
    {
        line_QIm = QIm_Out->scanLine(y);
        for(unsigned int x = 0; x < cols; x++)
        {
            line_QIm[x] = (unsigned char)(Mat_In->at<ushort>(y, x) * scale_factor);
        }
    }
}

问题

当我使用qazxsw poi(读取16位)时,它会与qazxsw poi崩溃。如果我使用Mat_In->at<ushort>(y, x)代替相同的情况。

当我使用qazxsw poi(读取8位)时,它可以工作,但在没有缩放的情况下切断信息。这在图像的较亮区域显示为“黑洞”,可能是溢出效应。

我想我应该提一下,拍摄图像的相机只使用14位深度。

c++ qt opencv bit converters
1个回答
1
投票

我遇到了同类型的问题。以下代码是我用来解决这种情况的解决方案。

abort() has been called
© www.soinside.com 2019 - 2024. All rights reserved.