[SOLVED] How to compare two images pixel by pixel, and exlude certain pixels from the comparison?
-
So we have this 2 QImages
@ QImage screenshot_Qimage;
QImage previous_QImage;@This code runs perfectly and completed in only 4 milleseconds:
@ if(screenshot_Qimage.operator ==(previous_QImage))
{
qDebug() << "Same images";
}@But what if i want to exlude certain pixels? Lets say i dont want from it to compare the pixel(50,50) . I got no other choise but to compare those 2 images pixel by pixel(am i right?).. So the code below compares every pixel of these 2 images:
@if(screenshot_Qimage.width()==previous_QImage.width() && screenshot_Qimage.height()==previous_QImage.height()) { bool something_is_not_the_same=false; int screenshot_Qimage_width=screenshot_Qimage.width(); int screenshot_Qimage_height=screenshot_Qimage.height(); for(int i=1; i<screenshot_Qimage_width; i++) { if(something_is_not_the_same) break; for(int j=1; j<screenshot_Qimage_height; j++) { if(QColor(screenshot_Qimage.pixel(i,j)).name()!=QColor(previous_QImage.pixel(i,j)).name()) { qDebug() << "different pixel detected"; something_is_not_the_same=true; break; } } } }@
Well it takes 3 seconds to be executed, :/
-
This line takes a lot of time to execute:
@if(QColor(screenshot_Qimage.pixel(i,j)).name()!=QColor(previous_QImage.pixel(i,j)).name())@
Simplify it and benchmark again
@if(screenshot_Qimage.pixel(i,j) != previous_QImage.pixel(i,j))@ -
use QImage::constScanLine to access raw image data line by line. That will be even faster (probably as fast as the QImage comparison operator).
Watch out for false negatives arising from differing image formats/bitness. -
[quote author="beemaster" date="1361030805"]
Simplify it and benchmark again
@if(screenshot_Qimage.pixel(i,j) != previous_QImage.pixel(i,j))@[/quote]comparing QRgbs will always return true... -> tested
that is the reason i putted the QColor().name -
How is that ever possible? Is it a bug in Qt?
[quote author="Leon" date="1361039869"][quote author="beemaster" date="1361030805"]
Simplify it and benchmark again
@if(screenshot_Qimage.pixel(i,j) != previous_QImage.pixel(i,j))@[/quote]comparing QRgbs will always return true... -> tested
that is the reason i putted the QColor().name
[/quote] -
[quote author="DerManu" date="1361031628"]use QImage::constScanLine to access raw image data line by line. That will be even faster (probably as fast as the QImage comparison operator).
Watch out for false negatives arising from differing image formats/bitness.[/quote]So for example if i want to exclude only the pixel (50,50) you are saying to scan all the lines except line 50, and scan line 50 pixel by pixel? I think that's a great idea.. ;)
-
[quote author="beemaster" date="1361040003"]How is that ever possible? Is it a bug in Qt?
[/quote]Well see it yourself..
My test was simple:
Comparing them would always return true..
so i placed 2 qdebugs and what i saw? they were different but still comparing the 2 QRgbs would return true.. thus i putted the QColor.name -
What version of Qt are you using?
-
Just looked up, QRgb is just a typedef for unsigned int
@
typedef unsigned int QRgb;
@
It's very likely you have some error in your code. You can't have 2 different ints which are equal :) -
[quote author="beemaster" date="1361040820"]Just looked up, QRgb is just a typedef for unsigned int
@
typedef unsigned int QRgb;
@
It's very likely you have some error in your code. You can't have 2 different ints which are equal :)
[/quote]i got no words ^^
probably i have done something wrong in my code, its true that u can compare 2 QRbs..
removed Qcolor.name , from 3000 milleseconds to 30 :)Nice thank u :)