Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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