Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The following is a C++ code to gather the global maximum and draw a circle around it:

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;

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;
        }
    }
}

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);
}


float factor = static_cast<float>(largest_hist_j * bin_w) / static_cast<float>(histImage.cols - 1);
cout << "max value:  " << max_length << endl;
cout << "peak value: " << max_length*factor << endl;


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

The following is a C++ code to gather the global maximum and draw a circle around it:

vector<float> total_lengths;

// ... fill the vector with data here.

float max_length = 0;

for (size_t i = 0; i < total_lengths.size(); i++)
{
    if (total_lengths[i] > max_length)
        max_length = total_lengths[i];
}

for (size_t i = 0; i < total_lengths.size(); i++)
{
    total_lengths[i] /= max_length;
    total_lengths[i] *= 255.0f;
    total_lengths[i] = floorf(total_lengths[i]);
}

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

for (size_t i = 0; i < total_lengths.size(); i++)
    data.at<unsigned char>(0, i) = static_cast<unsigned char>(total_lengths[i]);

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;

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;
        }
    }
}

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);
}

 float factor = static_cast<float>(largest_hist_j * bin_w) / static_cast<float>(histImage.cols - 1);
cout << "max value:  " << max_length << endl;
cout << "peak value: " << max_length*factor << endl;
 
circle(histImage, Point(largest_hist_j * bin_w, 0), 2, Scalar(255, 127, 0), 2);