Thread: Image comparison - How to read an image so you can compare pixels?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Image comparison - How to read an image so you can compare pixels?

    I am writing a program in C that compares two images taken by a camera to see if they are different. I have an idea of how to do this, by putting the images into 2 dimensional arrays and comparing the RGB value at corresponding array positions, however I cannot figure out how to get the image into an array.

    I've found a ton of example of how to do this in C#, but that's not so helpful. The only bit of C code I've been able to find uses fread() to read the images but that doesn't seem to work.

    I've also heard some suggestions that converting the images to greyscale is helpful in comparing the pixels. Agree? Disagree?

    Thanks in advance.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The issues of how to read the data from the disk using fread() and whether you convert to grayscale are extremely minor in comparison to handling the image file format. What kind of images will you be processing (I mean file format: TIFF, JPEG, BMP, PNG, what?)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    I have been working with jpegs but I'm open to changing if there's a better format to suit my needs.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dearbear View Post
    I have been working with jpegs but I'm open to changing if there's a better format to suit my needs.
    Reading a JPEG without the help of a library like libjpeg is extremely difficult. Without knowing more about your application and constraints, I would recommend converting the images to PPM format using some utility like ImageMagick. PPM is extremely easy to parse without the aid of specialized image libraries.

    More information would be helpful, though. How big are the images, how many of them will you be processing, what are your disk space constraints, etc
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Are you looking for bit identical, or just similar pictures? If you look for an identical match my guess is that you could create a checksum of the entire data section of the files and compare them.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Subsonics View Post
    Are you looking for bit identical, or just similar pictures? If you look for an identical match my guess is that you could create a checksum of the entire data section of the files and compare them.
    Might be worth a try, but for complex, compressed formats it's not necessarily a given that identical images will produce identical data blocks. It wouldn't be a reliable method.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by brewbuck View Post
    Might be worth a try, but for complex, compressed formats it's not necessarily a given that identical images will produce identical data blocks. It wouldn't be a reliable method.
    Mm, yeah I guess not. It would definately not work for identical images with different compression ratios for example.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    I can't tell you how big the images are because I don't have access right now to the webcam I've been using. This is a small scale project so there won't be that many images, maybe a dozen. If I use PPM, how do I read it into the array?

    The two images are taken within seconds of each other, so maybe the checksum method would be more reliable in this case?

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by dearbear View Post
    The two images are taken within seconds of each other, so maybe the checksum method would be more reliable in this case?
    If they are two different images, not a original and a copy it won't work, it relies on them being bit by bit identical.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Thanks for introducing me to PPM. It looks to be very easy to read in. I plan to start experimenting with it tomorrow. Thanks again to both of you for all your help!

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by brewbuck View Post
    Reading a JPEG without the help of a library like libjpeg is extremely difficult.
    ???
    2 lines of code on Windows...

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Alex31
    2 lines of code on Windows...
    If you have to specify "Windows", then you have a library in mind
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by Alex31 View Post
    ???
    2 lines of code on Windows...
    Not everyone uses Windows.

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    I've been working with the PPM file format and it is awesome. It is so easy to read and I am so grateful to you, brewbuck, for telling me about it. Thanks to everyone else as well for your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  2. Replies: 1
    Last Post: 12-01-2002, 01:24 PM
  3. Need help for Read 2 Dimention barcode from image
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 01-28-2002, 09:23 AM
  4. How to read a image in C++?
    By DramaKing in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 12:34 AM