c++qtopencvqimageopencv-mat

convert Qimage to cvMat 64FC3 format


i have searched a lot on the internet but i have only found how to convert Qimage to RGB format, i want to convert an Qimage to cv mat format CV_64FC3. i have really bad results when i work with CV_8UC3

here is my code :

QImage myImage;
myImage.load("C://images//PolarImage300915163358.bmp");

QLabel myLabel;
myLabel.setPixmap(QPixmap::fromImage(myImage));

//myLabel.show();

cv::Mat image1 = QImage2Mat(myImage);



Mat img;

image1.convertTo(img, CV_64FC3, 1.0 / 255.0);

and here is the function that i used :

cv::Mat QImage2Mat(QImage const& src)
{
     cv::Mat tmp(src.height(),src.width(),CV_8UC3,(uchar*)src.bits(),src.bytesPerLine());
     cv::Mat result; // deep copy just in case (my lack of knowledge with open cv)
     cvtColor(tmp, result,CV_BGR2RGB);
     return result;
}

please help me i m new to both opencv and Qt


Solution

  • Not sure what you mean with bad results, but you are assuming that QImage also loads the image as OpenCV (BGR). In the documentation it tells you that they use ARGB.

    So, knowing this you have 2 options:

    1. Convert to QImage::Format_RGB888 the Qimage using the function convertToFormat and then this line cvtColor(tmp, result,CV_BGR2RGB); is not needed, since it will be already in RGB.
    2. Use CV_8UC4 when creating the cv::Mat and then drop the first channel (channel alpha) using either split and join or mixchannels.