How to manipulate an image at pixel level in C?
How do I read an image in C so that I can have direc开发者_Python百科t 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?
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.
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.
To manipulate jpeg files use libjpeg, the tiff files use libtiff, etc.
Another easy option is gd which allows manipulation of many image formats (PNG, JPEG, GIF, ...).
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.
MagickWand from ImageMagick is another good option
精彩评论