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. Byte array to QImage
Forum Updated to NodeBB v4.3 + New Features

Byte array to QImage

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 8 Posters 4.8k Views 3 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 Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Hi,

    Besides the points of my fellows, are you sure about the size of the image ? 1200x1920 seems a bit non standard for a camera.

    On that point, what camera is that ?
    How do you get data from it ?
    How did you configure it ?

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

    hardikgarg19H 1 Reply Last reply
    4
    • beeckscheB beecksche

      @hardikgarg19
      Maybe the set format is wrong. Try Format_Grayscale8 or Format_RGB32.

      Or try to leave the format blank, so QImage will detect the reading format by itself.

      QImage::fromData((uchar*)pBuf, iBytes*sy);
      

      All described in the docs

      hardikgarg19H Offline
      hardikgarg19H Offline
      hardikgarg19
      wrote on last edited by
      #6

      @beecksche
      I have tried the change, but, I am getting the bool value as false and no image is created.

          QImage img(1200,1920,QImage::Format_Grayscale8);
          img = QImage::fromData((uchar*)pBuf,iBytes*sy);
          bool b = img.save( "C:\\test_folder\\file_img.BMP");
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #7

        You really should try to find out what format the camera is sending - guessing is not the correct solution here.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        hardikgarg19H 1 Reply Last reply
        2
        • mranger90M Offline
          mranger90M Offline
          mranger90
          wrote on last edited by
          #8

          I'm curious about the "iBytes * sy" in your code.
          If you are truly grabbing an 8 bit gray scale image from the camera then the size of the data will likely be just "iBytes" which, and I'm going out on a limb here, should be imageWidth * imageHeight.
          If you are multiplying this by some factor, the mysterious "sy", then the image is probably RGB24 (sy == 3 ?) or RGB32 (sy == 4 ?).

          1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            Besides the points of my fellows, are you sure about the size of the image ? 1200x1920 seems a bit non standard for a camera.

            On that point, what camera is that ?
            How do you get data from it ?
            How did you configure it ?

            hardikgarg19H Offline
            hardikgarg19H Offline
            hardikgarg19
            wrote on last edited by
            #9

            @SGaist Hi,
            I am using a FPGA based camera (http://isgcameras.com/allegro-usb3-family/).

            Below is the code:

            long sx,sy,iBpp;
            //get the image size
            

            //pMedleyUcam is the camera object
            pMedleyUcam->get_SizeX(&sx);
            pMedleyUcam->get_SizeY(&sy);

            pMedleyUcam->GetBytesPerPixel(&iBpp);
            int iBytes=sx*iBpp; // horizontal image size in bytes

            BYTE* pBuf=(BYTE*)malloc(iBytes*sy);
            
            short v;
            pMedleyUcam->Grab(&v);
            BYTE *ptr=pBuf;
            for(int y=0;y<sy;y++)
                {
                    pMedleyUcam->GetImagePointer(0, sy-1-y,&var);
                    memcpy(ptr,var.pbVal,iBytes);
                    ptr+=iBytes;
                }
            

            //form the image using the pBuf
            QImage img(1200,1920,QImage::Format_Grayscale8);
            img = QImage::fromData((uchar*)pBuf,iBytes*sy);
            bool b = img.save( "C:\test_folder\file_img.BMP");

            b is returning false here

            1 Reply Last reply
            0
            • hskoglundH Online
              hskoglundH Online
              hskoglund
              wrote on last edited by
              #10

              Hi, if you try with double slashes

              bool b = img.save( "C:\\test_folder\\file_img.BMP");
              

              is b still returning false?

              hardikgarg19H 1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                You really should try to find out what format the camera is sending - guessing is not the correct solution here.

                hardikgarg19H Offline
                hardikgarg19H Offline
                hardikgarg19
                wrote on last edited by
                #11

                @Christian-Ehrlicher
                The camera allows me to check the available pixel format. Below is the list:
                Mono8
                Mono10p
                Mono12p
                BayerRGB
                BayerRGB10p
                BayerRGB12p
                RGB8
                RGB565P
                BGR8
                YCbCr422_8
                YUV400_8
                YUV8_UYV

                1 Reply Last reply
                0
                • hskoglundH hskoglund

                  Hi, if you try with double slashes

                  bool b = img.save( "C:\\test_folder\\file_img.BMP");
                  

                  is b still returning false?

                  hardikgarg19H Offline
                  hardikgarg19H Offline
                  hardikgarg19
                  wrote on last edited by
                  #12

                  @hskoglund Hi,
                  Yes, it is still returning false.

                  1 Reply Last reply
                  0
                  • hskoglundH Online
                    hskoglundH Online
                    hskoglund
                    wrote on last edited by hskoglund
                    #13

                    Looks like you're only getting the raw pixels from the camera (in the specified pixel format). To save to a file, like a BMP file., you need an header as well.
                    To get one, you could try create an empty QImage with the specified x and y size, and then fill it with those pixels you get from the camera, say something like:

                    QImage img(1200,1920,QImage::Format_Grayscale8);
                    for (int y = 0; (y < 1920); ++y)
                        for (int x = 0; (x < 1200); ++x)
                            img.setPixel(x,y,((uchar*)pBuf) + x + 1200 * y);
                    hardikgarg19H 1 Reply Last reply
                    4
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #14

                      According to this SO question you can do a line at a time:
                      https://stackoverflow.com/questions/14563180/raw-data-to-qimage

                      C++ is a perfectly valid school of magic.

                      hardikgarg19H 1 Reply Last reply
                      1
                      • hskoglundH hskoglund

                        Looks like you're only getting the raw pixels from the camera (in the specified pixel format). To save to a file, like a BMP file., you need an header as well.
                        To get one, you could try create an empty QImage with the specified x and y size, and then fill it with those pixels you get from the camera, say something like:

                        QImage img(1200,1920,QImage::Format_Grayscale8);
                        for (int y = 0; (y < 1920); ++y)
                            for (int x = 0; (x < 1200); ++x)
                                img.setPixel(x,y,((uchar*)pBuf) + x + 1200 * y);
                        hardikgarg19H Offline
                        hardikgarg19H Offline
                        hardikgarg19
                        wrote on last edited by
                        #15

                        @hskoglund Hi,
                        Yes, that was the problem. Thank you so much.

                        1 Reply Last reply
                        0
                        • fcarneyF fcarney

                          According to this SO question you can do a line at a time:
                          https://stackoverflow.com/questions/14563180/raw-data-to-qimage

                          hardikgarg19H Offline
                          hardikgarg19H Offline
                          hardikgarg19
                          wrote on last edited by
                          #16

                          @fcarney Hi,
                          Thank you so much for sharing the link. I have used the QImage.scanLine and it worked.

                          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