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. problem with QImage function
Forum Updated to NodeBB v4.3 + New Features

problem with QImage function

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.1k 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.
  • hardikgarg19H Offline
    hardikgarg19H Offline
    hardikgarg19
    wrote on last edited by
    #1

    Hello,
    I am grabbing a frame from the camera and trying to save it. I am not getting the proper image. There are tiles in the image: example: if I take a picture of a pen, in the top 1/4th part of the saved image I can see 3 pens from left to right and rest of the image is black.
    Below is the code:

    int frmSize;
    unsigned char *frameData=NULL;
    

    //ISG_GetFrame syntax: ISG_GetFrame(int cameraNumber, unsigned char **frameData, int *frameSize)
    /*Parametes:
    int camNum - value 0 to n used to select one of multiple cameras available. In the case
    of a single camera connection, value 0 should be used.
    unsigned char **frameData - pass the address location of the pointer that will be used to reference
    the returned image.
    int frameSize - takes the address of an integer that will hold the number of bytes in the image
    Return value:
    int status - returns 0 on success, -1 on timeout waiting for an available frame
    /

    ISG_GetFrame(camNum,&frameData,&frmSize);
    
    
    QImage img(frameData,1920,1200,QImage::Format_Mono);
    ISG_ReleaseFrame(camNum);
    //img=img.convertToFormat(QImage::Format_RGB16);
    img.save( "C:\\test_folder\\fileTEST20.JPG" );
    

    I have checked that the camera is returning MONO8 image with width 1920 and height 1200

    Please help me with this issue. Thank You

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      And what's your exact problem now?
      See ctor documentation: "The buffer must remain valid throughout the life of the QImage..."

      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
      0
      • Christian EhrlicherC Christian Ehrlicher

        And what's your exact problem now?
        See ctor documentation: "The buffer must remain valid throughout the life of the QImage..."

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

        @Christian-Ehrlicher Hello Christian,
        The buffer is valid till it is converted to QImage. Now the problem is that, I am trying to display QImage on QGraphicsview as a video and I am missing some frames. Below is the function that I am using:

        void MainWindow::whenNewFrame(const QImage& im)
        {
        QPixmap stream_pix=QPixmap::fromImage(im);
        if (!stream_pix.isNull())
        {
        delete ui->graphicsView->scene();
        ui->graphicsView->setScene(new QGraphicsScene(ui->graphicsView));
        ui->graphicsView->scene()->addPixmap(stream_pix);
        }
        }

        So, I want to know if there is anything I can do to speed up the process.

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Not deleting the QGraphicsScene and QGraphicsPixmapItem on every call to whenNewFrame will be a good starting point...

          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
          • Christian EhrlicherC Christian Ehrlicher

            Not deleting the QGraphicsScene and QGraphicsPixmapItem on every call to whenNewFrame will be a good starting point...

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

            @Christian-Ehrlicher if I remove the delete ui->graphicsView->scene() from the code then I got an error "QImage: out of memory, returning null image". I have read that addPixmap(stream_pix) will return a pointer to QGraphicsPixmapItem and I can update it instead of adding and deleting scene every time. But, I am not sure how to do that.

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @hardikgarg19 said in problem with QImage function:

              But, I am not sure how to do that.

              As member of the class for example.

              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
              • Christian EhrlicherC Christian Ehrlicher

                @hardikgarg19 said in problem with QImage function:

                But, I am not sure how to do that.

                As member of the class for example.

                hardikgarg19H Offline
                hardikgarg19H Offline
                hardikgarg19
                wrote on last edited by Christian Ehrlicher
                #7

                @Christian-Ehrlicher
                I am able to solve the problem using setPixmap() function. Below is the code:

                mainwindow.h

                QGraphicsPixmapItem *Item;
                

                mainwindow.cpp
                inside the constructor:

                QImage defImg=QImage(1920,1200,QImage::Format_Grayscale8);
                QPixmap defStream_pix=QPixmap::fromImage(defImg);
                ui->graphicsView->setScene(new QGraphicsScene(ui->graphicsView));
                Item=ui->graphicsView->scene()->addPixmap(defStream_pix);
                

                inside the constructor, a pixmap is created and added to the scene. "Item" is the returned pointer.
                now I have updated the "Item" whenever there is new image and update the scene.

                void MainWindow::whenNewFrame(const QImage& im)
                {
                    QPixmap stream_pix=QPixmap::fromImage(im);
                
                    if (!stream_pix.isNull())
                    {
                        Item->setPixmap(stream_pix);
                        ui->graphicsView->scene()->update();
                    }
                }
                

                Thank you @Christian-Ehrlicher

                /edit by moderator: added <code> tags

                1 Reply Last reply
                1
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Nice, then you can mark the topic as solved now :)

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

                  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