Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to compare two images properly
Forum Updated to NodeBB v4.3 + New Features

How to compare two images properly

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    karim24
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wrosecrans
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved