PDA

View Full Version : Compare 2 images in QT?



harshita
20th September 2011, 05:28
hi all,
i want to compare 2 QImages, please share the logic, i have already used operator==, but it takes to much time.
Can someone explain how scanline function works?

thanks in advance

ChrisW67
20th September 2011, 07:17
scanLine() returns a pointer to bytePerLine() bytes of data representing one horizontal line in the image. If the images are of identical size and format then loop over all the lines in the image to access the raw data and compare them however you like. I haven't looked, but I suspect the operator==() checks for equal pixels (if the answer is not trivial) regardless of image format.

What is the source of the two images? Files, generated, captured? Same source or different sources?

harshita
20th September 2011, 07:32
scanLine() returns a pointer to bytePerLine() bytes of data representing one horizontal line in the image. If the images are of identical size and format then loop over all the lines in the image to access the raw data and compare them however you like. I haven't looked, but I suspect the operator==() checks for equal pixels (if the answer is not trivial) regardless of image format.

What is the source of the two images? Files, generated, captured? Same source or different sources?

Thanx Chris,
For now i am testing comparison so the source is the File and image can be of different format and sizes, this is what i have assumed in the actual scenario. can u please sketch a code for using scanline() .
thanx again

ChrisW67
21st September 2011, 05:23
If the image is of a different size then the images are not equal.
If the images are of the same format then you could possibly compare scanline by scanline until you find a byte that differs (memcmp()) , or run out of bytes.
If the images are of different formats then you have to fetch the RGBA components of each pixel in turn until you find a pixel that differs, or run out of pixels.

If you look at the implementation of QImage::operator==() you'll find that this is exactly how it does it. :)


The reason I asked about the source is that there may be other definitions of "compare image" that are more efficient. If you are after an absolute equality then it may be adequate to check if the source files are identical at a byte level (directly our using a hash) rather than a pixel-by-pixel logical equality check.

harshita
21st September 2011, 06:32
hey chris,
thats exactly wot i am looking for. I want to calculate hash of an image.
QCryptographicHash class seems to be a solution, but i ma not sure?

how do i convert a QImage to QByteArray?

thanx