Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

problem with QImage function



  • 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


  • Qt Champions 2019

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



  • @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.


  • Qt Champions 2019

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



  • @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.


  • Qt Champions 2019

    @hardikgarg19 said in problem with QImage function:

    But, I am not sure how to do that.

    As member of the class for example.



  • @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


  • Qt Champions 2019

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


Log in to reply