Grayscale in Images - OPENCV

2

Good afternoon, folks,

I have a .bmp image and I want to turn it to grayscale (0-255) through Opencv with C ++ (I do not want to save this grayscale image). From here, I want to go through the image doing the following:

For each pixel, if the grayscale is between 0-31, then this pixel belongs to class 1. If it is between 32-63 then this pixel belongs to class 2 ... and so it goes. We will thus have 8 classes.

Then I want to know how many pixels are in class 1, 2, 3, ..., 8.

How can I do this? I searched the Opencv documentation but could not find anything that would help me.

Thank you very much!

    
asked by anonymous 16.06.2016 / 20:17

1 answer

1
Mat img = imread("sua_imagem.bmp", CV_LOAD_IMAGE_GRAYSCALE);
vector<int> acumulador(8);

for (int i = 0; i < img.rows; i++)
    for (int j = 0; j < img.cols; j++) {
        int index = (int)img.at<uchar>(i, j) / 32;
        acumulador[index]++;
    }

This should solve your problem, the accumulator vector is size 8, where each position contains the number of pixels of the image belonging to that class.

    
06.06.2017 / 23:35