I am trying to create an application in which I am trying to integrate opencv and qt.
I was able to successfully convert cv :: Mat to QImage using the following code:
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; } 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 try to convert QImage to cv :: Mat 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")); 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 completely erratic. On the network I was looking for, I saw that people use this method without mentioning any specific problems. Does anyone have any ideas what could be causing the problem? Thanks in advance.
Theoodore
PS I am using opencv 2.4 and Qt 4.8 on an Arch Linux system with gnome-3.4