views:

504

answers:

5

How do I read an image in C so that I can have direct control over its pixels (like we do in MATLAB)?

I tried the old way:

FILE *fp;
fp = fopen("C:\\a.tif","r");

that just gives me the ascii form of the image file (I think).

How can I get pixel level control over the image and do some basic operations like say, inverting the image?

+2  A: 

Have a look at libtiff. Nearly all image formats have some sort of header data and many are compressed to keep filesize reasonable. You can of course write C code to read the headers and perform decompression, but there's no need since someone else has already done it. Look here for some C image libraries.

Another question is what form you want the data in for manipulation - a common choice is 24-bit RGB (i.e. R, G, and B each vary from 0 to 255). In MATLAB you also see R, G, B as doubles varying from 0.0 to 1.0. Or you may want a different colour space (HSV, YUV, etc.).

Remember that integer operations are faster, but they can be more troublesome.

Artelius
+1 for answering OP's question but also reminding them to not reinvent the wheel
JeffH
+2  A: 

To manipulate jpeg files use libjepg

For the tiff files use libtiff

and so on...

And an easy option is gd

Xolve
+1  A: 

There are some open-source libs to load images of different types and libtiff to load tiff images. If you just want to play around and the file format is not very important, then I would take a look on netpbm format (inparticular P5 and P6). It is pretty easy to write a loader and saver for it and of course a good exercise.

quinmars
+1  A: 

MagickWand from ImageMagick is another good option

Lou Franco
A: 

OpenCV is computer vision library, but can be used for "low level" tasks too. It supports BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM, SR, RAS, TIFF, TIF.

Harriv