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. Display RGB HALCON image as QImage
Forum Updated to NodeBB v4.3 + New Features

Display RGB HALCON image as QImage

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 2.1k Views 1 Watching
  • 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.
  • SGaistS SGaist

    Did you check all the values of the modified image ?

    R Offline
    R Offline
    rujma
    wrote on last edited by
    #5

    @SGaist I did not check the values since I am not sure what I am looking for in comparison with the original image.

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

      Compare the CountChannels values, the width and height values.

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

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Compare the CountChannels values, the width and height values.

        R Offline
        R Offline
        rujma
        wrote on last edited by rujma
        #7

        @SGaist I can answer the Width and Height question. When I use the InterleaveChannels method, the width is 4x larger than the original image, while the height stays the same. I tried to divide by 4 when constructing the QImage as shown in the matching example, but no luck.

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

          What do you get if using the sizes returned by your interleaved image ?

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

          R 2 Replies Last reply
          0
          • SGaistS SGaist

            What do you get if using the sizes returned by your interleaved image ?

            R Offline
            R Offline
            rujma
            wrote on last edited by
            #9

            @SGaist Pretty sure it is the same thing as the one on the printscreen. The division by 4 did not make a difference (I think). But I shall confirm it on my test setup tomorrow.

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

              Are you also sure that you choose the correct image format for QImage ?

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

              R 1 Reply Last reply
              0
              • SGaistS SGaist

                Are you also sure that you choose the correct image format for QImage ?

                R Offline
                R Offline
                rujma
                wrote on last edited by
                #11

                @SGaist I have tried several, mainly _ARGB, _RGB888, _Premultiplied, .... all unsuccessfully.

                1 Reply Last reply
                0
                • SGaistS SGaist

                  What do you get if using the sizes returned by your interleaved image ?

                  R Offline
                  R Offline
                  rujma
                  wrote on last edited by rujma
                  #12

                  @SGaist said in Display RGB HALCON image as QImage:

                  What do you get if using the sizes returned by your interleaved image ?

                  While showing the interleaved image, it is "wider" compared with the screen shot image. I think is due to the fact that the width is 4x higher.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    ollarch
                    wrote on last edited by
                    #13

                    This is because instead of an image that contains 3 channels each one containing only R or G or B values, you now have one image that have only 1 channel containing RGBA values. When you get the image pointer you have width*4 that is correct. "width" is not really the image width, is the "bytes per line" in this case.

                    What happens if you use this QImage contructor?

                    QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
                    
                    R 1 Reply Last reply
                    0
                    • O ollarch

                      This is because instead of an image that contains 3 channels each one containing only R or G or B values, you now have one image that have only 1 channel containing RGBA values. When you get the image pointer you have width*4 that is correct. "width" is not really the image width, is the "bytes per line" in this case.

                      What happens if you use this QImage contructor?

                      QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
                      
                      R Offline
                      R Offline
                      rujma
                      wrote on last edited by
                      #14

                      @ollarch said in Display RGB HALCON image as QImage:

                      This is because instead of an image that contains 3 channels each one containing only R or G or B values, you now have one image that have only 1 channel containing RGBA values. When you get the image pointer you have width*4 that is correct. "width" is not really the image width, is the "bytes per line" in this case.

                      What happens if you use this QImage contructor?

                      QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
                      

                      The image is exactly the same as the one on the screenshot, nothing changed.

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        ollarch
                        wrote on last edited by
                        #15

                        Could you try to save the processed image to disk and check it?

                        R 1 Reply Last reply
                        0
                        • O ollarch

                          Could you try to save the processed image to disk and check it?

                          R Offline
                          R Offline
                          rujma
                          wrote on last edited by
                          #16

                          @ollarch said in Display RGB HALCON image as QImage:

                          Could you try to save the processed image to disk and check it?

                          I have saved the images using the following code:

                                      _currentHImage.WriteImage("png", 0, "images/himage");
                                      
                                                  HalconCpp::HImage interleavedImg = _currentHImage.InterleaveChannels("argb", "match", 0); // Only if the image is RGB
                                      
                                                  unsigned char* imgRawPtr = (unsigned char *)interleavedImg.GetImagePointer1(&type, &width, &height);
                                      
                                                  QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
                                      
                                                  qimage.save("images/qimage.png");
                          

                          The images are equal! The only problem is showing them on the paint.drawImage....
                          himage
                          qimage

                          1 Reply Last reply
                          0
                          • R Offline
                            R Offline
                            rujma
                            wrote on last edited by
                            #17

                            Looks like I was using the wrong format and not scaling the image correctly.
                            This is the fix:

                            if(_currentHImage.CountChannels().I() == 3)
                                    {
                                        HalconCpp::HImage interleavedImg = _currentHImage.InterleaveChannels("argb", "match", 0); // Only if the image is RGB
                                        unsigned char* imgRawPtr = (unsigned char *)interleavedImg.GetImagePointer1(&type, &width, &height);
                                        QImage qimage(imgRawPtr, width / 4, height, width, QImage::Format_RGB32);
                                        QImage newqimage = qimage.scaled(400, 300, Qt::KeepAspectRatio);
                                        QPainter painter(this);
                                        painter.drawImage(QPoint(0, 0), newqimage);
                                    }
                                    else
                                    {
                                        unsigned char* imgRawPtr = (unsigned char*)_currentHImage.GetImagePointer1(&type, &width, &height);
                                        QImage qimage(imgRawPtr, width, height, QImage::Format_Grayscale8);
                                        QImage newqimage = qimage.scaled(400, 300, Qt::KeepAspectRatio);
                                        QPainter painter(this);
                                        painter.drawImage(QPoint(0, 0), newqimage);
                                    }
                            }
                            

                            Thanks for all your help!

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

                              Nice !

                              Since you have it working now, please mark the thread as solved so other forum members may know a solution has been found :-)

                              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
                              0

                              • Login

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