I am trying to create an application where I am trying to integrate opencv and qt.
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);
}
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.
Theoodore
P.S. I am using opencv 2.4 and Qt 4.8, under a Arch Linux system with gnome-3.4