How To Spend QImage To Opencv?



  • Now I need to Operat picture in program.
    win7 + Qt5.7+ vs2013 +opencv3.1

       I choose opencv to operat picture,the picture is coming another progran and his format is QImage, I need give the QImage to openCV's funtion ,but openCV's funtion can't read this format(QImage);
     I have to save QImage to local file ,then opencv's funtion read the local file ,like this
    

    QString pixPath = tr("F:/pix/1.png");
    testPix.save(pixPath); //testPix is a QImage
    IplImage* sImg = cvLoadImage(pixPath.toStdString().c_str(),1); //use opencv funtion

    but the is a problem,i need save QImage to local disk and read out again,this will impact the speed of the program,can I spend QImage to opencv's funtion dirctly?


  • Moderators

    @qazaq408 For sure not directly. I don't know the OpenCV API. First you need to check OpenCV documentation and see what you can pass to it. Is there something like loadFromData(char*) (this is just an example)?



  • @qazaq408
    I stumbled across this git-project a while a go. It should solve your issue.


  • Lifetime Qt Champion

    Hi,

    Before anything else, why do you need to pass a QImage to OpenCV ? Can't you use OpenCV directly ? What's the source of the image ?



  • @qazaq408 if you really need to pass a Qt image to OpenCV you may want to do something like this:

    QImage preview;
    ...
    auto image = cv::Mat(preview.height(),
        preview.width(),
        CV_8UC4,
        const_cast<uchar*>(preview.bits()),
        preview.bytesPerLine());
    

    The project mentioned in @J-Hilk post could also help you understand the process. Pay attention to the QImage's format (i.e. QImage::Format_RGB32, QImage::Format_ARGB32, etc.)

    And also as @SGaist told you, think if it's completely necessary to do such image conversion (Qt <-> OpenCV). You may end up doiing all the image related work in OpenCV alone (i.e. reading, manipulating and saving an image file) within a Qt application.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.