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. fill a buffer of unsigned char* from QImage
Forum Updated to NodeBB v4.3 + New Features

fill a buffer of unsigned char* from QImage

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 4.7k 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.
  • J.HilkJ J.Hilk

    @LeaA
    QImageBits is the way to go, show us what you tried, that resulted in a broken image.

    from the top of my head, this should make a copy of your raw data.

    QImage img;
    uchar copiedData[img.byteCount()];
    memcpy(copiedData, img.bits(), img.byteCount());
    
    
    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #7

    Just read cv::Mat documentation:

    Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

    QImage* image = new QImage("/tmp/myImage.pgm",this/*parent that will take care of cleaning up*/);
    cv:Mat cv_image(image.height(),image.width(),CV_8UC3,image.bits());
    imwrite("/tmp/new_image.pgm",cv_image);
    

    @J.Hilk said in fill a buffer of unsigned char* from QImage:

    uchar copiedData[img.byteCount()];

    This is not valid in C++, only in C99

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    J.HilkJ 1 Reply Last reply
    1
    • L Offline
      L Offline
      LeaA
      wrote on last edited by
      #8

      @VRonin
      So, because of it I want to move with for loop on the QImage and copy the data to a buffer and then sent it to CV:Mat

      VRoninV 1 Reply Last reply
      0
      • L LeaA

        @VRonin
        So, because of it I want to move with for loop on the QImage and copy the data to a buffer and then sent it to CV:Mat

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #9

        Same problem, you have to let the buffer survive. allocate it on the heap and then handle the cleanup

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • L Offline
          L Offline
          LeaA
          wrote on last edited by
          #10

          yet, I think I not do it good..
          this is my code:
          unsigned char* image_buff = new char [image.bytesCount()];
          int step = 0;
          for( int i=0; i < image.height(); ){
          image_buff[step] = image.scanLine(i);
          step += image.bytesPerLine() - image.width();
          }

          and then I using CV:Mat with the buffer
          and delete [] buffer.
          but I doesn't work...

          VRoninV 1 Reply Last reply
          0
          • VRoninV VRonin

            Just read cv::Mat documentation:

            Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

            QImage* image = new QImage("/tmp/myImage.pgm",this/*parent that will take care of cleaning up*/);
            cv:Mat cv_image(image.height(),image.width(),CV_8UC3,image.bits());
            imwrite("/tmp/new_image.pgm",cv_image);
            

            @J.Hilk said in fill a buffer of unsigned char* from QImage:

            uchar copiedData[img.byteCount()];

            This is not valid in C++, only in C99

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

            @VRonin said in fill a buffer of unsigned char* from QImage:

            @J.Hilk said in fill a buffer of unsigned char* from QImage:

            uchar copiedData[img.byteCount()];

            This is not valid in C++, only in C99

            ups 😳

            uchar *copiedData = new uchar[img.byteCount()];
            

            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
            0
            • L Offline
              L Offline
              LeaA
              wrote on last edited by
              #12

              the QImage store the data non-sequential and because of it I want copy the data to a buffer in sequential order

              1 Reply Last reply
              0
              • L LeaA

                yet, I think I not do it good..
                this is my code:
                unsigned char* image_buff = new char [image.bytesCount()];
                int step = 0;
                for( int i=0; i < image.height(); ){
                image_buff[step] = image.scanLine(i);
                step += image.bytesPerLine() - image.width();
                }

                and then I using CV:Mat with the buffer
                and delete [] buffer.
                but I doesn't work...

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #13

                @LeaA said in fill a buffer of unsigned char* from QImage:

                image_buff[step] = image.scanLine(i);

                This only copies 1 char use memcpy as @J-Hilk suggested

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  LeaA
                  wrote on last edited by
                  #14

                  I did so and I'm getting the same result

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    LeaA
                    wrote on last edited by
                    #15

                    I want copy pixel pixel and jump on the headers that the QImage add to the buffer

                    D 1 Reply Last reply
                    0
                    • L LeaA

                      I want copy pixel pixel and jump on the headers that the QImage add to the buffer

                      D Offline
                      D Offline
                      Devopia53
                      wrote on last edited by Devopia53
                      #16

                      @LeaA

                      Take a look at image formats(QImage::Format_RGB888/CV_8UC3) and byte ordering between QImage and cv::Mat.
                      Just reading this will help to you: here

                      1 Reply Last reply
                      3
                      • L Offline
                        L Offline
                        LeaA
                        wrote on last edited by
                        #17

                        I get the same result but the image is swapped..

                        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