C++ 如何将灰度/二进制Mat转换为QImage?
我已经开始学习Qt,并试图制作一个简单的视频播放器,它将加载视频并播放它。它工作得非常好。现在添加了阈值功能。阈值将从spinBox获得。 代码的编写方式是,阈值操作将使用spinBox中的值来完成,但值为0(其中显示正常视频)的情况除外。 这就是我的函数:C++ 如何将灰度/二进制Mat转换为QImage?,c++,qt,opencv,image-processing,C++,Qt,Opencv,Image Processing,我已经开始学习Qt,并试图制作一个简单的视频播放器,它将加载视频并播放它。它工作得非常好。现在添加了阈值功能。阈值将从spinBox获得。 代码的编写方式是,阈值操作将使用spinBox中的值来完成,但值为0(其中显示正常视频)的情况除外。 这就是我的函数: void Player::run() { while(!stop ) { if(!capture.read(frame)) stop = true; // convert RGB to gray
void Player::run()
{
while(!stop )
{
if(!capture.read(frame))
stop = true;
// convert RGB to gray
if(frame.channels() == 3)
{
if(thresh == 0)
{
cvtColor(frame, RGBframe, CV_BGR2RGB);
img = QImage((const unsigned char*)(RGBframe.data),
RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
}
else
{
Mat temp;
cvtColor(frame, temp, CV_BGR2GRAY);
threshold(temp, binary, thresh, 255, 0);
img = QImage((const unsigned char*)(binary.data),
binary.cols, binary.rows, QImage::Format_Indexed8);
bool save = img.save("/home/user/binary.png");
cout<<"threshold value = "<<thresh<<endl;
//imshow("Binary", binary);
}
}
else
{
if(thresh == 0) // original Image
{
img = QImage((const unsigned char*)(frame.data),
frame.cols,frame.rows,QImage::Format_Indexed8);
}
else // convert to Binary Image
{
threshold(frame, binary, thresh, 255, 0);
img = QImage((const unsigned char*)(binary.data),
binary.cols, binary.rows, QImage::Format_Indexed8);
}
}
emit processedImage(img);
this->msleep(delay);
}
}
void播放器::run()
{
当(!停止)
{
如果(!捕获.读取(帧))
停止=真;
//将RGB转换为灰色
if(frame.channels()==3)
{
如果(阈值==0)
{
CVT颜色(帧、RGB帧、CV_BGR2RGB);
img=QImage((常量无符号字符*)(RGBframe.data),
RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
}
其他的
{
垫温;
CVT颜色(框架、温度、CV_bgr2灰色);
阈值(温度、二进制、阈值、255、0);
img=QImage((const unsigned char*)(binary.data),
binary.cols、binary.rows、QImage::Format_Indexed8);
bool save=img.save(“/home/user/binary.png”);
cout您似乎缺少索引图像的颜色表。您需要添加颜色表(在while循环之前):
或者,正如所指出的,在Qt5.5中,您也可以使用格式QImage::format_Grayscale8
:
// From Qt 5.5
QImage image(inMat.data, inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Grayscale8);
您似乎缺少一个颜色表。在while循环之前添加以下内容:QVector sColorTable(256);for(int i=0;i<256;++i){sColorTable[i]=qRgb(i,i,i);}
,然后从二进制文件创建QImage
,然后添加img.setColorTable(sColorTable);
彩色图像输入框还是灰度图像输入框?afair grayscale在Qt中不是indexed8类型,afair我必须使用颜色表。感谢此附加功能@Miki@Optimus1072很高兴它起到了作用。我更新了问题的标题,以更好地反映这个问题,并帮助其他人找到这个解决方案。对于灰度图像,我们还需要一个colorMap right?仅适用于灰度图像对于灰度,在Qt>=5.5上,您可以使用QImage::Format_Grayscale8
而不是索引格式。
img.setColorTable(sColorTable);
// From Qt 5.5
QImage image(inMat.data, inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Grayscale8);
QImage cvMatToQImage(const cv::Mat &inMat)
{
switch (inMat.type())
{
// 8-bit, 4 channel
case CV_8UC4:
{
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_ARGB32);
return image;
}
// 8-bit, 3 channel
case CV_8UC3:
{
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_RGB888);
return image.rgbSwapped();
}
// 8-bit, 1 channel
case CV_8UC1:
{
#if QT_VERSION >= 0x050500
// From Qt 5.5
QImage image(inMat.data, inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Grayscale8);
#else
static QVector<QRgb> sColorTable;
// only create our color table the first time
if (sColorTable.isEmpty())
{
sColorTable.resize(256);
for (int i = 0; i < 256; ++i)
{
sColorTable[i] = qRgb(i, i, i);
}
}
QImage image(inMat.data,
inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Indexed8);
image.setColorTable(sColorTable);
#endif
}
default:
qWarning() << "cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
break;
}
return QImage();
}