How to compare two images properly
-
here is what i tried:
@double compare(const QImage &im1, const QImage &im2)
{
int count=0;
int w=im1.width();
int h=im1.height();for(int i=0;i<h;i++){ QRgb *rgb1=(QRgb*)im1.constScanLine(i); QRgb *rgb2=(QRgb*)im2.constScanLine(i); for(int j=0;j<w;j++){ if(rgb1[j]==rgb2[j]) count++; } } return (count*100.0)/(h*w);
}
@i used it like this
@ QImage im1=QImage("c:/1.jpg");
QImage im2=QImage("c:/2.jpg");
im2=im2.scaled(im1.size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
qDebug()<<compare(im1,im2);@when i compare two identical images without modifying them i get 100%,but when i modify one of them using (paint on windows i just add a dot) the percentage drops to 4%---5%,the size of two files looks like this after modifying
1.jpg 338 ko unmodified
2.jpg 259 ko it's the same as 1.jpg after modification (using paint on windows i just add a dot)maybe other formats will work fine???
-
Or you will need to come up with some sort of a threshold comparison rather than using "==". Comparing images "properly" is incredibly poorly defined though, so you really need to nail down what you want to know about two images before you worry about writing the code. For example, if image A is lightened by a small amount by adding an offset to every channel, then every pixel is different in the result image, but they are perceptually very similar. Painting a dot in an image is a more obvious change, but may cover fewer pixels. Which should register as a bigger change? Human visual perception is more focused on green. Should computed differences follow human perception and have a bigger weight for differences in the green channel? Personally, I would do the sum of all the pixels resulting from subtracting image A minus image B as a starting point for this kind of work, but image similarity is basically a whole field of research.