Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QImage and OpenCV Mat problem

    General and Desktop
    4
    9
    383
    Loading More Posts
    • 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.
    • S
      stvokr last edited by

      Hi all.

      I'm using Windows 10 and Linux 18.04 with Qt 5.12 and OpenCV 4.0.1.
      I have a problem when loading a QImage and then converting it to an OpenCV Mat.
      It seems that QImage has more data in its array than the Mat.

      Properties:
      Qimage: width=1001px, height=1001px, type=QImage::Format_Grayscale, bytesPerLine=1004
      Mat: width=1001px, height=1001px, type=CV_8UC1, bytesPerLine=1001

      This problem does only occur when the image width is odd.

      Does anyone know why and how to solve it?

      The result images are confusing. The pixels are shifted.

      711e428e-4798-47e1-bd81-7c5969e274bb-grafik.png

      fe6c9da0-67c7-4e2e-ba19-d0e352090a63-grafik.png

      Regards,
      Oliver

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        IIRC, it will depend on the image format and size. There can be padding involved.

        In any case, it's usually done the other way around: load image with OpenCV, apply processing and move then to QImage.

        What are you currently doing ?

        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 Reply Quote 1
        • S
          stvokr last edited by

          Hi,

          currently I am loading a QImage from a file.
          Then I encode it to Base64-Format and insert the Base64-String into an xml-structure.

          This xml is sended through tcp/ip to a server where the server is decoding the Base64-String back to an image. And then I am processing the image with OpenCV.

          My current workaround is to save the QImage to hard disk and load again it with OpenCV. That works.

          Regards,
          Oliver

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            That might be a silly question but why not directly encode the file content ?

            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 Reply Quote 0
            • beecksche
              beecksche last edited by

              @stvokr
              QImage uses a 32-bit alignment, I also had some issues with the bytes: https://forum.qt.io/topic/105321/different-bytes-count-of-qimage

              1 Reply Last reply Reply Quote 1
              • S
                stvokr last edited by

                @SGaist
                I will try that, but I need that QImage object directly. It should be possible to display the base64 string directly in a web browser as HTML content.

                @beecksche
                Thanks for that.

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  There's no problem with that but you didn't gave much information about your application so it's not easy to provide recommendations.

                  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 Reply Quote 0
                  • S
                    stvokr last edited by

                    @SGaist
                    I figured out that the main problem is not transferring the image but loading the image with QImage. Even when I just load the image from harddisk the bytesPerLine is 1004 for an 8 bit pixel image with 1001px width. So as already mentioned from @beecksche the 32-bit alignment will prevent the correct direct conversion from QImage to cv::Mat.

                    1 Reply Last reply Reply Quote 0
                    • Christian Ehrlicher
                      Christian Ehrlicher Lifetime Qt Champion last edited by

                      Afaics OpenCV::Mat also supports padding. Images are always padded so it would be better to teach OpenCV::Mat about it.

                      Qt has to stay free or it will die.

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post