简体   繁体   English

opencv Qt如何有效地创建彩色图像并将其传递给QImage?

[英]opencv Qt how to efficiently create and pass color image to QImage?

This question is partially answered at how to convert an opencv cv::Mat to qimage 这个问题在如何将opencv cv :: Mat转换为qimage时得到部分回答。

I use Qt 5.5, and opencv 2.4, mac yosemite 10.10.5. 我使用Qt 5.5和opencv 2.4,mac优胜美地10.10.5。

I'm looking for an efficient way to display a color image with a QImage, whose color information come from 3 distinct cv::Mat objects which contain the 3 RGB channels of a color image. 我正在寻找一种使用QImage显示彩色图像的有效方法,该图像的颜色信息来自3个不同的cv :: Mat对象,其中包含彩色图像的3个RGB通道。 For my own "education" I need to understand how to originally work with the channels separated. 对于我自己的“教育”,我需要了解如何在分离的渠道下进行工作。 It is unclear to me what will be the most efficient way to end up with the QImage from these 3 separated channels. 对我来说还不清楚,从这三个分离的通道中最终获得QImage的最有效方法是什么。

The cv::Mat objects are declared and assigned as follows: cv :: Mat对象的声明和分配如下:

imRed = new Mat(Size(naxis1, naxis2), CV_64F);
imGreen = new Mat(Size(naxis1, naxis2), CV_64F);
imBlue = new Mat(Size(naxis1, naxis2), CV_64F);


double* redP = imRed->ptr<double>(0);
double* greenP = imGreen->ptr<double>(0);
double* blueP = imBlue->ptr<double>(0);

for (long i = 0; i < nPixels; i++)
{

    redP[i] = (double) rawProcess.imgdata.image[i][0];;
    greenP[i] = (double) rawProcess.imgdata.image[i][1];
    blueP[i] = (double) rawProcess.imgdata.image[i][2];

}

where rawProcess is an object from the Libraw library which contains the image data. 其中rawProcess是来自Libraw库的对象,其中包含图像数据。 redP , greenP , blueP are, as their name indicate, the pointer to the red, green and blue channel, and this code assumes (correct me if i'm wrong) that the cv::Mat addresses of the pixel values are continuous in memory. redPredPgreenPblueP是指向红色,绿色和蓝色通道的指针,并且此代码假定(如果我错了,请纠正我)像素值的cv::Mat地址是连续的记忆。

From here, how shall I efficiently pass the buffer of the color image to QImage, ie, minimizing intermediate copy, loops etc... (if necessary, my system can use openMP, I have 4 threads available)? 从这里开始,如何有效地彩色图像的缓冲区传递给QImage,即,最小化中间副本,循环等...(如果需要,我的系统可以使用openMP,我有4个线程可用)?

Thanks 谢谢

This code below is working. 下面的代码正在工作。

Starting from the above code (with naxis1 and naxis2 the width and height of the image): 从上面的代码开始(使用naxis1和naxis2表示图像的宽度和高度):

imRed = new Mat(Size(naxis1, naxis2), CV_64F);
imGreen = new Mat(Size(naxis1, naxis2), CV_64F);
imBlue = new Mat(Size(naxis1, naxis2), CV_64F);


double* redP = imRed->ptr<double>(0);
double* greenP = imGreen->ptr<double>(0);
double* blueP = imBlue->ptr<double>(0);

for (long i = 0; i < nPixels; i++)
{

    redP[i] = (double) rawProcess.imgdata.image[i][0];;
    greenP[i] = (double) rawProcess.imgdata.image[i][1];
    blueP[i] = (double) rawProcess.imgdata.image[i][2];

}

I add: 我加:

std::vector<cv::Mat> matVector;
matVector.push_back(imRed);
matVector.push_back(imGreen);
matVector.push_back(imBlue);

cv::Mat colorMat32;
cv::merge(matVector, colorMat32);

cv::Mat colorMat888

double alpha = (double) (255 / 65535);
double beta = 0.0;

colorMat32.convertTo(colorMat888, CV_8UC3, alpha, beta);

newPaintImage = new QImage(colorMat888.data, naxis1, naxis2, QImage::Format_RGB888);

Thanks @Micka. 谢谢@Micka。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM