Qimage到cv :: Mat转换奇怪的行为 [英] Qimage to cv::Mat convertion strange behaviour
问题描述
我正在尝试创建一个应用程序,试图在其中集成opencv和qt.
I am trying to create an application where I am trying to integrate opencv and qt.
通过使用以下代码,我成功地将cv :: Mat转换为QImage:
I managed successfully to convert a cv::Mat to QImage by using the code below:
void MainWindow::loadFile(const QString &fileName)
{
cv::Mat tmpImage = cv::imread(fileName.toAscii().data());
cv::Mat image;
if(!tmpImage.data || tmpImage.empty())
{
QMessageBox::warning(this, tr("Error Occured"), tr("Problem loading file"), QMessageBox::Ok);
return;
}
/* Mat to Qimage */
cv::cvtColor(tmpImage, image, CV_BGR2RGB);
img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888);
imgLabel->setPixmap(QPixmap::fromImage(img));
imgLabel->resize(imgLabel->pixmap()->size());
saveAsAct->setEnabled(true);
}
但是,当我尝试通过使用以下代码将QImage转换为cv :: Mat时:
However, when I am trying to convert the QImage to cv::Mat by using the following code:
bool MainWindow::saveAs()
{
if(fileName.isEmpty())
{
QMessageBox::warning(this, tr("Error Occured"), tr("Problem loading file"), QMessageBox::Close);
return EXIT_FAILURE;
}else{
outputFileName = QFileDialog::getSaveFileName(this, tr("Save As"), fileName.toAscii().data(), tr("Image Files (*.png *.jpg *.jpeg *.bmp)\n *.png\n *.jpg\n *.jpeg\n *.bmp"));
/* Qimage to Mat */
cv::Mat mat = cv::Mat(img.height(), img.width(), CV_8UC4, (uchar*)img.bits(), img.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);
cv::imwrite(outputFileName.toAscii().data(), mat);
}
saveAct->setEnabled(true);
return EXIT_SUCCESS;
}
我没有成功,结果是图像完全混乱.在我搜索的网络中,我看到人们正在使用这种方式,而没有提及任何特定的问题.有人对导致问题的原因有任何想法吗?预先感谢.
I have no success and the result is totally disordered image. In the net that I searched I saw that the people are using this way without mentioning any specific problems. Does someone have any idea, about what could be cause the problem? Thanks in advance.
西奥多尔
P.S.我在带有gnome-3.4的Arch Linux系统下使用opencv 2.4和Qt 4.8
P.S. I am using opencv 2.4 and Qt 4.8, under a Arch Linux system with gnome-3.4
推荐答案
只需找出正确"的解决方案,即可复制(不引用)QImage到cv :: Mat
Just find out the "correct" solution of copying(not reference) the QImage to cv::Mat
马丁·贝克特(Martin Beckett)的答案几乎是正确的
The answer of Martin Beckett is almost correct
for (int i=0;i<image.height();i++) {
memcpy(mat.ptr(i),image.scanline(i),image.bytesperline());
}
我没有看到完整的代码,但我想您可能想以这种方式使用它
I don't see the full codes, but I guess you may want to use it like this way
cv::Mat mat(image.height(), image.width(), CV_8UC3);
for (int i=0;i<image.height();i++) {
memcpy(mat.ptr(i),image.scanline(i),image.bytesperline());
}
但是此代码存在问题, cv :: Mat 可能分配的内存与QImage的"bytesperline"相同
But this code exist a problem, the memory allocated by cv::Mat may not have the same "bytesperline" as the QImage
我找到的解决方案是先引用QImage,然后将其克隆
The solution I found is take the reference of the QImage first, then clone it
return cv::Mat(img.height(), img.width(), format, img.bits(), img.bytesPerLine()).clone();
马丁·贝克特(Martin Beckett)建议的解决方案可以在大多数情况下产生正确的结果 有时候,直到我找到它,我才注意到有一个错误.
The solution of Martin Beckett suggested could generate correct result in most of the times, I didn't notice there are a bug until I hit it.
无论如何,我希望这是一个正确"的解决方案.如果发现任何错误,请让 大家都知道,所以我们可以进行更改以改进代码.
Whatever, I hope this is a "correct" solution. If you find any bug(s), please let everyone know so we could have a change to improve the codes.
这篇关于Qimage到cv :: Mat转换奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!