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. [SOLVED] How to compare two images pixel by pixel, and exlude certain pixels from the comparison?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to compare two images pixel by pixel, and exlude certain pixels from the comparison?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 9.8k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    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, :/

    1 Reply Last reply
    0
    • B Offline
      B Offline
      beemaster
      wrote on last edited by
      #2

      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))@

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerManu
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #4

          [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

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beemaster
            wrote on last edited by
            #5

            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]

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Leon
              wrote on last edited by
              #6

              [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.. ;)

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leon
                wrote on last edited by
                #7

                [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

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  beemaster
                  wrote on last edited by
                  #8

                  What version of Qt are you using?

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    beemaster
                    wrote on last edited by
                    #9

                    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 :)

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      Leon
                      wrote on last edited by
                      #10

                      [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 :)

                      1 Reply Last reply
                      0

                      • Login

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