1

I want to read the contents of every pixel in an image i have and convert it to a bit-stream (raw bits) or contain it in a 2-D array . Which would be the best place to start looking for such a conversion?

Specifics of the image : Standard test image called lena.bmp size : 256 x 256 Bit depth of pixel : 8

Also I would like to know the importance of the number of bits per pixel with regards to this question since packing and unpacking will also be incorporated .

jleahy
  • 16,149
  • 6
  • 47
  • 66
  • There's not much to convert, the BMP (DIB) format is more or less a raw dump of an array of pixel, prefixed with few headers that you can quickly find with a Google search. – Matteo Italia Jun 06 '12 at 20:43
  • I want to be able to write a program that converts the pixel value into hex and then into its consequent binary value and then store it in an array. Would OpenCV be a good option or is anything simpler recommended ? – user1227372 Jun 06 '12 at 23:08

2 Answers2

4

CImg is a nice simple, lightweight C++ library which can load and save a number of image formats (including BMP).

It's a single header file, so there's no need to compile or link the library. Just include the header, and you're good to go.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • +1 I completely forgot about CImg, haven't used it in a while. – karlphillip Jun 06 '12 at 22:00
  • Hopefully this helps . I sincerely thank you guys for your help. – user1227372 Jun 06 '12 at 23:54
  • Cimg is pretty effective but i ended up using structs to define the bmp structure and used took a relatively longer approach to store pixel values into a 3d array but i chose a 24bit 512x512 which also works out for my project. Thank you so very much for ur advice. – user1227372 Jun 14 '12 at 15:59
2

You should investigate OpenCV: a cross-platform computer vision library. It provides a C++ API as well as a C API, and it supports many image formats including bmp.

In the C++ interface, cv::Mat is the type that represents a 2D image. A simple application that loads and displays an image can be found here.

To learn how to access the matrix elements (pixels) you can check these threads:

OpenCV’s C++ interface offers a short introduction to cv::Mat. There has been many threads on Stackoverflow regarding OpenCV, there's a lot of valuable content around and you can benefit a lot by using the search box.

This page has a collection of books/tutorials/install guides focused on OpenCV, but this the newest official tutorial.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 2
    Isn't a big, complex Computer Vision library a tiiiny bit overkill for someone who just wants to access the pixels of a .bmp? – jalf Jun 06 '12 at 21:02
  • On Linux, the OpenCV libraries occupy less then 16MB. I don't know much space you have in your device, but I wouldn't call it big. About it being complex, I strongly disagree: [the test application](http://opencv.itseez.com/doc/tutorials/introduction/display_image/display_image.html#source-code) to load/display an image shows how simple it can be making things a lot easier. It seems a little bit overkill to write the code to read the bmp header yourself when there are other ways. – karlphillip Jun 06 '12 at 21:18
  • 1
    Well, considering you can download a single, simple cross-platform header file which, if you include it, allows you to load a .bmp file, I think that a 16 MB library is pretty much the definition of overkill. ;) – jalf Jun 06 '12 at 21:39