19,468
社区成员
发帖
与我相关
我的任务
分享
QImage image("E:/Test_Picture/ver.tif");
qDebug()<<image.format();
cv::Mat mat = cv::Mat(image.height(),image.width(),
CV_8UC1, (uchar*)image.bits(), image.bytesPerLine());
cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC1 );
int from_to[] = {0,0,1,1,2,2};
cv::mixChannels(&mat,1,&mat2,1,from_to,1);
namedWindow("QImage2Mat");
imshow("QImage2Mat",mat2);
QImage image("D:/Picture/boy.jpg");
qDebug()<<image.format();
cv::Mat mat = cv::Mat(image.height(),image.width(),
CV_8UC4, (uchar*)image.bits(), image.bytesPerLine());
cv::Mat mat2 = cv::Mat(mat.rows, mat.cols, CV_8UC3 );
int from_to[] = {0,0,1,1,2,2};
cv::mixChannels(&mat,1,&mat2,1,from_to,3);
namedWindow("QImage2Mat");
imshow("QImage2Mat",mat2);
QImage image(aa->m_pRawBuffer,1000,700,QImage::Format_RGB888);
Mat mat =Mat(image.height(),image.width(),
CV_8UC3, (uchar*)image.bits(), image.bytesPerLine());
Mat mat2 =Mat(mat.rows, mat.cols, CV_8UC3 );
int from_to[] = {0,0,1,1,2,2};
mixChannels(&mat,1,&mat2,1,from_to,3);
namedWindow("test");
imshow("test",mat2);
cv::Mat QImage2cvMat(QImage image)
48.{
49. cv::Mat mat;
50. qDebug() << image.format();
51. switch(image.format())
52. {
53. case QImage::Format_ARGB32:
54. case QImage::Format_RGB32:
55. case QImage::Format_ARGB32_Premultiplied:
56. mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
57. break;
58. case QImage::Format_RGB888:
59. mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
60. cv::cvtColor(mat, mat, CV_BGR2RGB);
61. break;
62. case QImage::Format_Indexed8:
63. mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
64. break;
65. }
66. return mat;
67.}
QImage cvMat2QImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
qDebug() << "CV_8UC4";
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
cv::Mat QImage2cvMat(QImage image)
{
cv::Mat mat;
qDebug() << image.format();
switch(image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}