Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How To Spend QImage To Opencv?
Forum Update on Monday, May 27th 2025

How To Spend QImage To Opencv?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • qazaq408Q Offline
    qazaq408Q Offline
    qazaq408
    wrote on last edited by
    #1

    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?

    jsulmJ J.HilkJ 2 Replies Last reply
    0
    • qazaq408Q qazaq408

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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)?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • qazaq408Q qazaq408

        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?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

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


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @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.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved