Ask Your Question
0

Exception for normalize 3D histogram (minMaxLoc)

asked 2016-11-28 02:54:00 -0500

LBerger gravatar image

Hi ,

I want to use normalize 3d histogram and I have got an exception :

OpenCV Error: Assertion failed (_img.dims() <= 2) in cv::minMaxLoc, file G:\Lib\opencv\modules\core\src\stat.cpp, line 2408

Does it mean that normalize does not accept 3D Mat?

My program is (Opencv 3.1-dev VS2015):

    Mat m = imread("f:/lib/opencv/samples/data/baboon.jpg", CV_LOAD_IMAGE_ANYCOLOR);

    float hRange[] = { 0, 256 };
    const float* etendu[] = { hRange, hRange,hRange };
    int hBins = 30;
    int tailleHist[] = { hBins, hBins, hBins };
    int canaux[] = { 0, 2,1 };
    cv::Mat histogramme;
    cv::calcHist(&m, 1, canaux, Mat(), histogramme, 2, tailleHist, etendu, true, false);
    normalize(histogramme, histogramme, 0, 255, cv::NormTypes::NORM_MINMAX, -1, cv::Mat());
    cv::calcHist(&m, 1, canaux, Mat(), histogramme, 3, tailleHist, etendu, true, false);
    normalize(histogramme, histogramme, 0, 255, cv::NormTypes::NORM_MINMAX, -1, cv::Mat());// EXCEPTION HERE
    waitKey();
edit retag flag offensive close merge delete

Comments

hmm, i'd think minMaxLoc (or normalization even) makes only sense for a single image plane, no ?

berak gravatar imageberak ( 2016-11-29 01:39:25 -0500 )edit

Yes of course but before calcBackProject I need to normalize 3D histogram (relative to maximum). With your comment I try minMaxIdx(histogramme, &x, &y); // Nice it's ok minMaxLoc(histogramme, &x, &y); // Bad an exception

LBerger gravatar imageLBerger ( 2016-11-29 03:47:54 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-11-29 04:40:53 -0500

LBerger gravatar image

updated 2016-11-29 04:51:54 -0500

berak gravatar image

No I can't. But I solve my problem : I don't use normalize

Mat m = imread("f:/lib/opencv/samples/data/baboon.jpg", CV_LOAD_IMAGE_ANYCOLOR);
Mat mask(m.size(),CV_8UC1,Scalar::all(0));
Rect rZone(Point(220, 330), Point(260, 360));
mask(rZone) = 255;

float hRange[] = { 0, 255 };
const float* etendu[] = { hRange, hRange,hRange };
int hBins = 30;
int tailleHist[] = { hBins, hBins, hBins };
int canaux[] = { 2, 1,0 };
double hMin, hMax;
cv::Mat histogramme;
cv::Mat resultMap;
cv::Mat backproj1d;

and then

for (int dim=1;dim<=3;dim++)
      {
        cv::calcHist(&m, 1, canaux, mask, histogramme, dim, tailleHist, etendu, true, false);
        minMaxIdx(histogramme, &hMin, &hMax);
        cout<<"Hist extremum = "<<hMin<<"\t"<<hMax<<"\n";
        if (dim<3)
            normalize(histogramme, histogramme, 0, 255, cv::NormTypes::NORM_MINMAX, -1, cv::Mat());
        else
            histogramme = 255 * histogramme / hMax;

        minMaxIdx(histogramme, &hMin, &hMax);
        cout << "after normalize Hist extremum = " << hMin << "\t" << hMax << "\n";
        cv::calcBackProject(&m, 1, canaux, histogramme, backproj1d, etendu, 1, true);
        applyColorMap(backproj1d, resultMap, cv::COLORMAP_JET);

the end

    imshow(format("backproject %dD",dim), resultMap);
    waitKey();
}
rectangle(m, rZone, Scalar(255, 255, 255));
imshow("original", m);
waitKey();



}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-11-28 02:54:00 -0500

Seen: 448 times

Last updated: Nov 29 '16