Grayscale in Images - OPENCV

Asked

Viewed 564 times

2

Good afternoon Personal,

I have an image . bmp and want to turn it into grayscale (0-255), through Opencv with C++(I don’t want to save this image in grayscale). From there, I want to go through the image by 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 on. We will thus have 8 classes.

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

How can I do that? I searched the Opencv documentation but couldn’t find anything to help me with that.

Thank You Very Personal!

  • Hello. Welcome to SOPT. This site is not a forum nor a ranked one. What have you done? What is your specific difficulty in this lot of things you want to do? If you haven’t done it yet, do the [tour]. Oh, and look here on the site yourself. There is a lot of content that can help you. Like this, for example: http://answall.com/questions/82354/edi%C3%A7%C3%A3o-de-imagens-using-opencv-sem-fun%C3%A7%C3%B5es-pronto

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.