在qpixMap中显示一个CV :: MAT(键入CV_32F)
Display a cv::Mat (type CV_32F) in a QPixmap
我正在使用摄像头获取cv :: mat对象的imgtomo1映像。它是CV_32F图像。我试图使用QPIXMAP在Qlabel上展示它。这是我的代码:
cv::Mat imgTomo; imgTomo1.convertTo(imgTomo,CV_8UC1); 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( imgTomo.data, imgTomo.cols, imgTomo.rows, static_cast<int>(imgTomo.step), QImage::Format_Indexed8); image.setColorTable( sColorTable ); _afficheImg->setPixmap(QPixmap::fromImage(image));
不幸的是,显示的图像仍然是黑色的。当我是OpenCV的新手时,我的格式有些迷失。我认为转换应该起作用,所以我真的不知道我在误解什么。
编辑:我已删除了fllowning线:
imgtomo1.convertto(imgtomo,cv_8uc1(;
这导致信息丢失。现在,我没有黑屏,但是有些"雪"(我猜是非常奇特的巫婆表格1至0的像素(,我看不到我的相机可以显示什么。
谢谢您的回答,grégoire
我不确定您的代码有什么问题,但是我使用以下代码将cv::Mat
映像转换为QImage
。
if (frame.channels()== 3){
cv::cvtColor(frame, RGBframe, CV_BGR2RGB);
img = QImage((const unsigned char*)(RGBframe.data),
RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
}
else
{
img = QImage((const unsigned char*)(frame.data),
frame.cols,frame.rows,QImage::Format_Indexed8);
}
您甚至可以查看Follwing链接,以获取有关如何将Mat
图像转换为QImage
的更多信息。
1.convert cv_8u type
Mat Temp;
CurrentMat.convertTo(Temp, CV_8U);
2.检查通道号:
int theType = Temp.type();
int channel_number = (theType / 8) + 1;
if(channel_number == 4){
cvtColor(Temp, Temp, CV_BGRA2BGR);
}
3.您可以使用此代码将MAT类型转换为Qimage:
QImage putImage(const Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS=1
if (mat.type() == CV_8UC1)
{
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++)
colorTable.push_back(qRgb(i, i, i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
// 8-bits unsigned, NO. OF CHANNELS=3
if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
相关文章:
- 将CHW格式的浮点向量转换为cv::Mat
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- 将 cv::mat 转换为 QImage
- 将 cv::Mat 转换为 std::vector 的通用函数
- 在 QML VideoOutput 中将 cv::mat 显示为 QVideoFrame
- 如何将 cv::Mat 转换为 vtkImageData?
- 如何将 vtkImageData 转换为 cv::Mat?
- 如何总结所有cv::Mat元素
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 在 cv::Mat 中生成 2D 坐标网格
- 将 cv::Mat 转换为无符号的 int 像素
- OpenCV 分段错误(核心转储)在使用 cv::Mat::at 时
- 如何将多个 cv::Mat 从 c++ dll 传递到 opencvsharp Mat c#?
- 如何在不复制数据的情况下将 cv::Mat 转换为 2d 标准::矢量
- 如何使用 (*mpORBextractorLeft)(im,cv::Mat(),mvKeys,mDescriptors)
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 如何将 cv::mat 对象从 python 模块传递到 c++ 函数并返回返回的 cv::mat 类型为对象?
- 移动语义和 cv::Mat
- OpenCV 错误:断言在 cv::Mat 行 522 中失败