Create Qt image pointer



  • Hello to all,

    My application has to do with image processing, and so far I used to first save my image in a png file and then displayed this image in a QGraphicsView using a QGeaphicsScene, as it seems from the code in this post. But I want not to save my image in my memory but get the index and the value of every pixel and pass it to a pointer of Qt. After have created my image in Qt pointer, then I want to displayed it. Could somebody help me with this task? How could I do this?

     @destroyed(ui->graphicsView_resultImage);
    

    ui->graphicsView_resultImage->setScene(scene_segmentation=new Canvas());
    QPixmap tmpmap (QPixmap("result.png"));
    pixmapItem_segm = scene_segmentation->addPixmap (tmpmap.scaled (ui->graphicsView_inputImage->width(),ui->graphicsView_inputImage->height()));
    ui->graphicsView_resultImage->setScene(scene_segmentation);@



  • Do you coincidentally mean painter, not pointer?

    As for manipulating image data you might have a look at the QImage class, as
    @
    QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen.
    @



  • Well, I have used the QImage class and I have created a pointer image_Qt of this class. With the following loop I get the pixel index and value with an other library and put them in the image_Qt pointer using setPixel(). I want to know how I could allocate the pointer image_Qt in my memory? How I could set the width and height of image_Qt and how I could display it later using QPixmap maybe?

    @QImage *image_Qt;

    InputImageType::Pointer image = InputImageType::New();

    for(unsigned int i = 0; i < size_x-1; i++)
      {
      for(unsigned int j = 0; j < size_y-1; j++)
      {
        InputImageType::IndexType pixelIndex;
        pixelIndex[0] = i;
        pixelIndex[1] = j;
    
       unsigned int value= image->GetPixel(pixelIndex);
       image_Qt->setPixel(i,j,value );
      }
    
      }@


  • You allocate the QImage as every other object.
    @
    QImage *image_Qt = new QImage;
    @

    You do not need the copy the image information pixel per pixel. QImage can directly load various image formats (including BMP, GIF, PNG and JPG). If your image data is already in memory you can pass it to the QImage constructor.

    To get an QPixmap from QImage just use QPixmap::convertFromImage().

    For just displaying an image you do not need a grahpics view or graphics scene at all. Every QLabel can display a QPixmap.

    I suggest that you read the documentation, above all "QImage":http://doc.qt.nokia.com/latest/qimage.html and "QPixmap":http://doc.qt.nokia.com/latest/qpixmap.html.



  • I do not want to read an image which has already been in my memory. I want to get the pixel index and value each time from an other pointer, then change the pixel value for exmaple. Then the Qimage pointer with the new values I want to display it. I have add this line in order to set the xy size of my image_Qt, in order to be the same with the size of my real image.But then I get an exception.

    @QImage *image_Qt;
    image_Qt->scaled(size_x,size_y);@

    Do you know what I am doing wrong?



  • I get the exception in these lines of my code

    @unsigned int value= image->GetPixel(pixelIndex);
    image_Qt->setPixel(i,j,value );@

    Might be something wrong with the casting of parameter value?



  • [quote author="jk_mk" date="1308220088"]
    @QImage *image_Qt;
    image_Qt->scaled(size_x,size_y);@
    Do you know what I am doing wrong?[/quote]

    Yes, I do. You haven't created a QImage object.
    QImage* is just a pointer, not an object. To create an object on the heap you have to use the new operator.

    So your code has to read
    @
    QImage *image_Qt = new QImage;
    image_Qt->scaled(size_x,size_y);@

    But this is nothing Qt related, these are the basics of C++. Before using Qt - and please don't be offended - you will have to learn C++ first.



  • Well, I have made the following code. But when I debug the code I get this message QImage::setPixel: coordinate () out of range
    , for all the coordinates and also I cannot get the result in my QGraphicsView.What I should change in my code? Or to add the size of QImage?

    @QImage *image_Qt = new QImage;
    image_Qt->scaled(size_x,size_y);

    InputImageType::Pointer image = reader->GetOutput();

    for(unsigned int i = 0; i < size_x-1; i++)
      {
      for(unsigned int j = 0; j < size_y-1; j++)
      {
        InputImageType::IndexType pixelIndex;
        pixelIndex[0] = i;
        pixelIndex[1] = j;
    
       unsigned int value= image->GetPixel(pixelIndex);
       image_Qt->setPixel(i,j,value );
      }
    
      }
    
    
    destroyed(ui->graphicsView_resultImage);
    

    ui->graphicsView_resultImage->setScene(scene_segmentation=new Canvas());
    QPixmap tmpmap;
    tmpmap = QPixmap::fromImage(*image_Qt);
    pixmapItem_segm = scene_segmentation->addPixmap (tmpmap.scaled (ui->graphicsView_inputImage->width(),ui->graphicsView_inputImage->height()));
    ui->graphicsView_resultImage->setScene(scene_segmentation);@



  • [quote author="jk_mk" date="1308227262"]Well, I have made the following code. But when I debug the code I get this message QImage::setPixel: coordinate () out of range
    , for all the coordinates and also I cannot get the result in my QGraphicsView.[/quote]

    Because you have to specify the size of the image at its creation, see the "various constructors":http://doc.qt.nokia.com/latest/qimage.html#QImage of QImage.

    In addition, copying the image pixel per pixel using QImage::setPixel() is extremely expensive.

    You do not need an external library to read the image from a file. Just load the file using QImage and then manipulate the QImage directly.

    You will find all the neccessary information in the "documentation":http://doc.qt.nokia.com/latest/qimage.html.
    And again - prerequisite of using Qt is basic knowledge of C++ and the willingness to read documentation.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.