Important: Please read the Qt Code of Conduct -

Method for getting pixel’s intensity value

  • Hello,

    I have taken an image's coordinates by subclassing QGrapicsScene and using these methods:

    Does anybody know what method should be used in order to take a pixel's intensity value from this image?Any suggestion could be helpful.

    Thanks in advance

  • what is the pixel intensity?

  • I want to display a grey-scale png image.Every pixel could have a value between 0 and 255.I think that 0 represents black and 255 represents white colour. To achieve this I must have access to my image contents.
    I am using a QGraphicsScene, in which I have made the following
    @scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor));@

    What should I use to get this pixel value? I have read thay scene->render() could be the solution. Is it true?

  • have a look at the pixelator example.

    sounds similar to what you want.

    they use
    @qGray(modelImage.pixel(index.column(), index.row()));@
    where modelImage is a QImage.

  • I had a look at this example, but in my application I am using a QGraphicsScene and not QImage.To be more specific I have subclassed QGraphicsScene in order to take the coordinates of my image when I press the mouse, and I have created a pointer named scene with parent the QGraphicsScene object.Then, to display my image in a graphicsView I wrote:

    @ scene->addPixmap(QPixmap(fileName, 0, Qt::AutoColor));

    Do you know how to render in an image using QGraphicsScene object?

  • Moderators

    Take a look at "QGraphicsScene::render()":

    You can use it to paint a QRect portion of the scene with a QPainter created with Your image.

  • Well I am trying to pass my QgraphicScene into a QImage and then use QImage::pixel(), but this doesn't work correctly I think. The point also is that I want to take the value with mousePressEvent. I have created the following code, but the "intensity" value doesn't change when I press the mouse, while the xy coordinates are correct. Canvas is the subbclassed class from QGraphicsScene.What am I doing wrong?

    @void Canvas::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    emit mousejustpressed(mouseEvent->scenePos().x(),mouseEvent->scenePos().y());

    @void MainWindow::mousejustpressed(int x,int y)
    int k1=256-y;

    QImage image(scene->sceneRect().size().toSize(),
    unsigned char intensity=image.pixel(x,y);



  • Moderators

    What You're doing is creating an empty image of the size of Your scene, and this obviously won't contain any useful data. You're missing the rendering step.

    So what You need to do is create an empty image of size 1x1 (You don't want to render the whole scene just to read a single pixel do You?). Than create a painter that can draw to this image. Use that painter to render this single pixel of your scene to the image and read it in grayscale to get Your intensity e.g. something like this @QImage buffer = QImage(1,1, QImage::Format_RGB32);
    QPainter painter(&buffer);
    yourCanvasPointer->render(&painter, QRectF(0.0, 0.0, 1.0, 1.0), QRectF(x, y, 1.0, 1.0));
    int intensity = qGray(buffer.pixel(0,0));@

    But if You just want to display an image and read a pixel color/intensity this seems like an overkill.
    You don't need a QGraphicsScene for that. You can simply draw this grayscale image using QPainter on any widget in its paint event. Than in this widgets mouse event get the coords and read it directly from Your source image without all this fuss.

  • Hello,

    I want to use QGraphicsScene render in order to take the pixel value (0 to 255), of a grayscale png image using a press mouse event.I am using a QGraphicsView to load my image.I have used the following code to take the pixel value using a QImage and a QPainter. But I have noticed that before loading my image in the QGraphicsView and click on it I take the value 205 instead of 255 (for the white colour). How could I get the right grayscale range (0-255)? What am I doing wrong? I hope someone could help.

    @ QImage image(scene->sceneRect().size().toSize(),

    QPainter painter(& image);


  • Moderators

    You mentioned earlier that you use mouseEvent->scenePos(). This gets You the position in "scene coord system" but You use it to retrieve pixel from image as it was in that image's "window coord system".
    The default scene creates coord system such that point (0,0) is in the center of the QGraphicsView when you render it. So if you click in the upper left corner of that view, lets say at (3,5) scenePos() will translate it to something e.g. (-140, -168) depending on the size of Your QGraphicsView and any translations/rotations/scaling done in the scene if any.

    You should use mouseEvent->pos() instead if you want it in "window coords".

    But it seems You generate yourself a lot of problems that are not really necessary by using QGraphicsScene in the first place. You could achieve the same thing easier with just QImage and a QWidget with overloaded paintEvent.
    Also as I pointed out earlier - If you really must use QGraphicsScene than don't render the whole scene just to get that 1 pixel. Render only that single pixel, otherwise it's very wasteful on resources, both memory and cpu time. Scene composition is not a trivial operation and it's a tremendous task to get a single pixel of an image.

Log in to reply