Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to draw a circle on a histogram

I'm trying to draw a circle at the most populated bin in the histogram. I've tried a lot of things, but I just can't figure it out:

Mat data(1, total_lengths.size(), CV_8UC1, Scalar(0));

// ...

int histSize = 256;
float range[] = { 0, 256 }; //the upper boundary is exclusive
const float* histRange = { range };
bool uniform = true, accumulate = false;
Mat hist;
calcHist(&data, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);

int hist_w = 600, hist_h = 600;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(255, 255, 255));
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

float largest_hist = 0;
float largest_hist_j = 0;
float largest_hist_i = 0;

for (int j = 0; j < hist.rows; j++)
{
    for (int i = 0; i < hist.cols; i++)
    {
        if (hist.at<float>(j, i) > largest_hist)
        {
            largest_hist = hist.at<float>(j, i);
            largest_hist_j = j;
            largest_hist_i = i;
        }
    }
}

for (int i = 1; i < histSize; i++)
{
    line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(hist.at<float>(i - 1))),
        Point(bin_w*(i), hist_h - cvRound(hist.at<float>(i))),
        Scalar(0, 0, 0), 1, 8, 0);
}

largest_hist_j /= hist.rows;
largest_hist_j *= hist_w;

circle(histImage, Point(largest_hist_j, 0), 2, Scalar(255, 127, 0), 2);

imshow("calcHist Demo", histImage);
waitKey();

How to draw a circle on a histogram

I'm trying to draw a circle at the most populated bin in the histogram. I've tried a lot of things, but I just can't figure it out:

Mat data(1, total_lengths.size(), CV_8UC1, Scalar(0));

// ...

int histSize = 256;
float range[] = { 0, 256 }; //the upper boundary is exclusive
const float* histRange = { range };
bool uniform = true, accumulate = false;
Mat hist;
calcHist(&data, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);

int hist_w = 600, hist_h = 600;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(255, 255, 255));
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

float largest_hist = 0;
float largest_hist_j = 0;
float largest_hist_i = 0;

for (int j = 0; j < hist.rows; j++)
{
    for (int i = 0; i < hist.cols; i++)
    {
        if (hist.at<float>(j, i) > largest_hist)
        {
            largest_hist = hist.at<float>(j, i);
            largest_hist_j = j;
            largest_hist_i = i;
        }
    }
}

for (int i = 1; i < histSize; i++)
{
    line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(hist.at<float>(i - 1))),
        Point(bin_w*(i), hist_h - cvRound(hist.at<float>(i))),
        Scalar(0, 0, 0), 1, 8, 0);
}

// this is where it goes haywire
largest_hist_j /= hist.rows;
largest_hist_j *= hist_w;
 
circle(histImage, Point(largest_hist_j, 0), 2, Scalar(255, 127, 0), 2);

imshow("calcHist Demo", histImage);
waitKey();