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

This is a discussion on Image comparison - How to read an image so you can compare pixels? within the C Programming forums, part of the General Programming Boards category; I am writing a program in C that compares two images taken by a camera to see if they are ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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
    21,773
    Quote Originally Posted by Alex31
    2 lines of code on Windows...
    If you have to specify "Windows", then you have a library in mind
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    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, 08:12 AM
  2. Replies: 1
    Last Post: 12-01-2002, 12: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21