I need to do a program in C or C++ (preference in C), that reads a black and white .bmp and makes a histogram with the levels of gray(0 to 255) in each pixel. I tried looking for libraries that might help me, but so far i havent found anything and didnt even start. Can anyone guide me through this? I have practicly no knowledge in C, and this has to be done in this language. I have seen OpenCV and this ImageClass.h library but nothing helped. How should i start, and does anyone have any material that actually might help me study to do this?
-
Welcome to Stack Overflow. Please take the time to read [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) what and how you can ask here. – πάντα ῥεῖ Mar 06 '17 at 05:27
-
These kind of questions are explicitly _off-topic_ here. Read point #4 from this [help center article](http://stackoverflow.com/help/on-topic). – πάντα ῥεῖ Mar 06 '17 at 05:27
-
Hint: `int freq[256];` – user4581301 Mar 06 '17 at 05:38
-
1Some people suggesting C++ and libraries, but this can be done very easily in C without extra waffle. Lookup the spec of the BMP file format. Then your program should open the file, read the header data into a struct and all following data into and array. Then iterate on the array to create your histogram. Start at following link maybe http://stackoverflow.com/questions/1968561/getting-the-pixel-value-of-bmp-file?rq=1 – Toby Mar 06 '17 at 09:29
1 Answers
To get the individual pixel data from the bmp file in C++, it's much easier to use a library - i.e. stb_image is a very simple to use header-only-library for loading various image files. But there are many other libraries out there, which can do the same (i.e. CImg, CBitmap if you're on windows, FreeImage, ...). OpenCV can load images, but it can do much more complex tasks too - i.e. apply various common algorithm to image data. For just calculating a simple histogram OpenCV is maybe overkill.
Once you can access the raw pixel data from the bmp file in a array-like structure, calculating the histogram is straight forward: Just loop over every pixel and increase a counter depending on the color-value. In case of a grayscale image, this can be 0 (for black), 255 (for white) or any value in between. A histogram is simply the frequency of pixels in all possible colors.
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
int main(int argc, char* argv[]) {
int width, height, bpp;
unsigned char* pixels = stbi_load("grayscale.bmp", &width, &height, &bpp, 1);
int histogram[256] = { 0 };
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
histogram[pixels[x + y * height]]++;
}
}
stbi_image_free(pixels);
return 0;
}

- 8,721
- 13
- 75
- 126
-
Thanks, you realy helped me a lot with that, i was almost using OpenCV. Ill try the ones you recommended. Thanks again. – Rodrigo Dias Mar 07 '17 at 00:41
-
@RodrigoDias You're welcome! If the answer helped you, please mark it as accepted answer by clicking on the checkmark on the left side of the answer. Thank you! – Constantin Mar 07 '17 at 01:40