簡體   English   中英

將OpenCV Mat 16位讀取為QImage 8位灰度

[英]Read OpenCV Mat 16bit to QImage 8bit Greyscale

我想做的事

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

我想讀取一個16位的Tiff,然后將其轉換為8位。

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);
        }
    }
}

問題

當我使用Mat_In->at<ushort>(y, x) (讀取16位)時,它abort() has been called崩潰了。 如果我改用<short>也會發生同樣的情況。

當我使用Mat_In->at<uchar>(y, x) (讀取8位)時,它可以工作,但是會切斷信息而不進行縮放。 這在圖像的較亮區域顯示為“黑洞”,可能是溢出效果。

我想我應該提一下,拍攝圖像的相機僅使用14位深度。

我遇到了同樣類型的問題。 以下代碼是我用來解決這種情況的解決方案。

   #include <opencv2/core.hpp>
   #include <opencv2/imgproc.hpp>
   #include <opencv2/highgui.hpp>

   #include <QImage>

    int main(void)
    {

      //Read the 16 bit per pixel image.
      cv::Mat I = cv::imread('16BitsPerPixelImage.tiff',cv::IMREAD_ANYDEPTH|cv::IMREAD_ANYCOLOR);

     //Convert from 16 bit per pixel to 8 bit per pixel using a min max normalization and store it a 8 bit per pixel.
     cv::normalize(I,I,0.,255.,cv::NORM_MINMAX,CV_8U);

    // Then actualy the easiest way to convert it to a QImage is to save a temporary file and open it using QT functions.
    // PNG use a compression without loss algorithm.
      cv::imwrite("/tmp/convert16to8.png",I);

      QImage QI;

      QI.load("/tmp/convert16to8.png");

      return EXIT_SUCCESS;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM