Search code examples
c++qtpixel

Coordinates of a pixel between two images


I am looking for a solution to easily compute the pixel coordinate from two images.

Question: If you take the following code, how could I compute the pixel coordinate that changed from the "QVector difference" ? Is it possible to have an (x,y) coordinate and find on the currentImage which pixel it represents ?

char *previousImage;
char *currentImage;
QVector difference<LONG>;

for(int i = 0 ; i < CurrentImageSize; i++)
{
    //Check if pixels are the same (we can also do it with RGB values, this is just for the example)
    if(previousImagePixel != currentImagePixel)
    {
        difference.push_back(currentImage - previousImage);
    }
    currentImage++;
}

EDIT: More information about this topic:

  • The image is in RGB format
  • The width, the height and the bpp of both images are known
  • I have a pointer to the bytes representing the image

The main objective here is to clearly know what is the new value of a pixel that changed between the two images and to know which pixel is it (its coordinates)


Solution

  • There is not enough information to answer, but I will try to give you some idea.

    You have declared char *previousImage;, which implies to me that you have a pointer to the bytes representing an image. You need more than that to interpret the image.

    1. You need to know the pixel format. You mention RGB, So -- for the time being, let's assume that the image uses 3 bytes for each pixel and the order is RGB
    2. You need to know the width of the image.
    3. Given the above 2, you can calculate the "Row Stride", which is the number of bytes that a row takes up. This is usually the "bytes per pixel" * "image width", but it is typically padded out to be divisible by 4. So 3 bpp and a width of 15, would be 45 bytes + 3 bytes of padding to make the row stride 48.
    4. Given that, if you have an index into the image data, you first integer-divide it against the row stride to get the row (Y coordinate).
    5. The X coordinate is the (index mod the row stride) integer-divided by the bytes per pixel.