First time here? Check out the FAQ!

Ask Your Question
0

Plotting the pixel density of a binary image

asked Oct 16 '0

giggon44 gravatar image

Hello,

I am trying to find the density of pixels in a binary image and plot it. For example, if I pass through a binary image of 200x200 pixels, the plot x-axis would be the same size as the width of the image, while the y-axis would be the pixel density (#of white pixels) in that column. I am having trouble figuring out how to go about this, I have looked into calchist but it doesnt seem to be what I need.

Thank you.

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Oct 16 '0

berak gravatar image

you can use cv::reduce() to 'project' the pixels onto one of the image axes, like a "spatial histogram":

// dummy data, just for visualization:
Mat img(200,300,CV_8U,Scalar(0));
circle(img,Point(140,100),40,Scalar(255),-1);
circle(img,Point(190,110),20,Scalar(255),-1);

// project to  x-axis
Mat proj;
reduce(img, proj, 0, REDUCE_SUM, CV_32F); // sum needs larger depth

// barchart visualization
Mat draw(img.size(), CV_8U,Scalar(0));
for (int x=0; x<draw.cols; x++) {
    float v = proj.at<float>(0,x) / 255; // sum/255 == count
    line(draw, Point(x,img.rows-1), Point(x,img.rows-int(v)), Scalar(255), 1);
}

image description

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Oct 16 '0

Seen: 1,745 times

Last updated: Oct 16 '20