如何将灰度/二进制垫子转换为qimage
How to convert Grayscale/binary Mat to QImage?
我已经开始学习QT,并正在尝试制作一个简单的视频播放器,该视频播放器将加载视频并播放它。它运行得很好。现在添加了阈值功能。阈值将从Spinbox获得。代码的编写方式是,阈值操作将使用Spinbox中的值进行,除了在值0处(显示正常视频)。所以这是我的功能:
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);
}
}
对于自旋框值等于0,它运行良好,但是当spinbox值增加时,我只会得到黑屏。我尝试了imshow(cv:: Mat binary)
,它显示了正确的二进制图像,但是当我尝试保存QImage img
时,它是一些随机的黑白像素(尽管原始帧的大小相同)。
看来您缺少索引图像的颜色表。您需要添加一个彩色表(在段循环之前):
QVector<QRgb> sColorTable(256);
for (int i = 0; i < 256; ++i){ sColorTable[i] = qRgb(i, i, i); }
,从二进制Mat
创建QImage
之后,您需要添加
img.setColorTable(sColorTable);
或,如@kubaober所指出的那样,从QT 5.5中,您也可以使用格式QImage::Format_Grayscale8
:
// From Qt 5.5
QImage image(inMat.data, inMat.cols, inMat.rows,
static_cast<int>(inMat.step),
QImage::Format_Grayscale8);
通常,您可以将所有Mat
转到QImage
转换中。下面有 bug纠正和更新的cvMatToQImage
版本最初在此处找到。
然后,您可以从代码中删除所有转换为QImage
并改用此功能。
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();
}
相关文章:
- 将 cv::mat 转换为 QImage
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上
- 警告在将 OpenCV 垫映像投射到 QImage 时使用旧式转换
- 将 QImage 转换为字符*
- 将qimage*转换为void*,反之亦然
- 如何将 QImage 中的像素数据转换为其他格式
- 将命令行参数转换为qimage
- 与iMread相比,qimage to cv ::垫子转换了奇怪的行为
- 如何将灰度/二进制垫子转换为qimage
- 在CV :: MAT和Qimage之间进行转换会使程序崩溃
- 将Qimage转换为cvMat 64FC3格式
- 如何将 QImage 转换为 opencv Mat
- QImage转换返回null
- 将IplImage IPL_DEPTH_32S转换为QImage Format_RGB32的最快方法
- 将 uint16 dicom 映像转换为 qimage
- 从灰度图像到QImage的快速转换
- 将 QVideoFrame 转换为 QImage
- 使用 QImage::loadFromData 将 cv::mat 转换为 QImage
- Qt5转换QImage到OpenGL格式