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++){
    return (count*100.0)/(h*w);


    i used it like this

    @ QImage im1=QImage("c:/1.jpg");
    QImage im2=QImage("c:/2.jpg");

    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???

  • Hi, the jpg format is lossy i.e. it compresses and throws away
    "unneeded" pixel data, so every time you modify a .jpg file it usually gets smaller.

    You can try .png .bmp or .gif instead, they never throw away pixel data.

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.