Read image and access bytes in boost::gil - c++

Is it possible to:
read an image given by just a filename (not knowing the image format) to a 2d matrix rgb uncompressed form (e.g. read an JPG to a 2d array)
access the bytes of that image, copy them, change them... (e.g. inverse the colors, I need a pointer to the image bytes, setters/getters won't do )
save those bytes to any given image format (e.g. save the inversed image to PNG)
Is it possible with boost::gil ? Maybe there is a different library more appropriate for such a task?
Sample code would be highly appreciated.

Yes, you can do all that in boost::gil.
What you should know though, is that boost::gil is only a universal interface and doesn't handle reading/writing images all by itself. You still need to use a second library, e.g. libpng..

Yes, yes and yes.
There are functions that enable you to read and write JPEG, TIFF and PNG images: see here.
For the second bullet, it is what just GIL is meant to do. You can manipulate images using its facilities (click here).

Related

Read RGB triplets of JPEG files in C

to read bmp files we may use this http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx
as the header file and then get rgb triplets. How to get the rgb triplets of jpeg file, is there any such header file available. Please share the link if any.
The JPEG file format does not store the rgb triplets directly but it uses some sort of image compression. The file actually contains blocks of 64 (if I remember correctly) pixels which are attributed with a cosine pattern defining the actual colors.
You really should use a library (libjpeg, imagemagick, gd, ... e.g., depending on your use case) to read and decode the files and generate the rgb triplets in memory.
According to the answer to this question on MSDN, you could use the GDI+ component, which can load not only BMP, but JPG and other image formats too. From it, you will get a memory bitmap.
Here is an example on how to do that.
Check this library: libjpeg. This library implements JPEG image encoding, decoding,
and transcoding.

Save a raw image in OpenCV

I'm trying to use OpenCV to read/write images for me. Currently, I have them in a different, non-standard format, and I know how to get them into OpenCV's containers. Here are the requirements:
The pixels are 1, or 3 bands, U8, U16, U32, or F32
The images have metadata, random stuff, like the camera ID that took the images. I would like the metadata to be vi/notepad editable
I want to write as little code as possible when it comes to low level stuff. My experience is that this stuff requires the most maintenance.
I can define the format. It's only to read and write for these programs.
I don't want the pixels to be anything but binary, '0.5873499082' is way too much data for one float.
Is there a way to describe to OpenCV how to read and write image types it doesn't know? Are there image types already available for the types of images I have?
My interim solution is to use boost to serialize the image, and save the metadata in a separate file.
Try using gdal library for reading images and then convert it to IplImage.
OpenCV can't do that for you, you can store the metadata in a separate file, or you can use for example the jpeg exif (that won't be notepad editable though).

How can I process an image?

I'm building a program to convert an image file (whatever file type would be easiest) to G-Code for use on a rep-rap with a pen plotter attachment.
I'm wondering if i wanted to process the image pixel by pixel and check things like pixel color, how could I do this with C++?
I would really like to know how I can process a bitmap image, pixel by pixel, to check the color of the pixel.
The best way is to use a library, like for example Magick++.
When you load an image, you can access it's pixels data with Blob
You will probably want to use an existing library that has been tested.
But for fun/practice/etc, this would be a good exercise and wouldn't be impossible to do. The Bitmap Format is (relatively) simple compared with other image formats. The Wikipedia page has some tons of info, including some C++ code. It looks like once you've gotten past the header information, you get to a pixel array that shouldn't be difficult to parse.
Good luck.
Most image formats consist of a header and the actual raw image data. A bimpap image is no different. If you don't want to use one of the existing libraries, or if you are not allowed to, you should read about bitmap format :
http://en.wikipedia.org/wiki/BMP_file_format
Once you understand this you could create appropriate structs/classes to store the information you want from the header such as x,y size, bpp etc. And also have a pointer to the raw image data. You could then simpy iterate through every pixel and do whatever you want with it :)
Once you decipher the image file, I suggest you place the pixels into a matrix, for the first pass. (Future revisions can use other methods to access the pixels).
You can apply transformations to the pixels by using matrix multiplication. You can also access the pixels individually by using array indexing.
Search the web and SO for "introduction to graphics c++".

c++ read image pixels

I want a c++ Code to read every pixel from an image file. and i want to save the pixels like:
r[]
g[]
b[]
does somebody know how to do this?
The answer depends on the format of the image file. Is it a format which contains raw RGB data (such as uncompressed TGA)? Is it a YUV image? Is it a compressed image such as JPEG or PNG?
There are already plenty of C++ libraries out there which can read a wide variety of image file formats, and then provide the pixel-level access you require. Take a look at Adobe's GIL, or CImg for example.
There are many freely available libraries for reading different image file formats. Since you're using C++ you might want to look at Adobe's Generic Image Library (GIL) or even OpenCV.
This will sort you out, very easy to use and 'low level' image library:
http://easybmp.sourceforge.net/
Two libraries that I've used that jump to mind are:
ImageMagick
libGD
These libraries can handle a wide variety of image formats, depending on what you need.

How can I access the JPEG image pixels as a 3D array like we do in MATLAB?

I want to process an image in C++. How can I access the 3D array representing the JPEG image as is done in MATLAB?
I'd suggest using OpenCV for the task; C++ documentation is available here. The relevant (I believe) data structure which you'd have to use is the Point3_ class, which represents a 3D point in the image.
Well, I've never used MATLAB for such a task, but in C++ you will need some JPEG loader library like OpenIL or FreeImage. These will allow you to access the picture as byte arrays.
FreeImage's FreeImage_GetBits function has a detailed example in the documentation on how to access per pixel per channel data.
BTW, if you plan to do image processing in C/C++, I'd suggest you to check out the Insight Segmentation and Registration Toolkit and OpenCV.