Ask Your Question
0

how to get the second largest bin in histogram

asked 2017-06-17 19:16:13 -0600

waschbaer gravatar image

updated 2017-06-17 19:42:43 -0600

Hi,

I want to get the second largest bin in hisogram, do you have any idea how to do this? Delete the largest bin is not a desireble solution I think, because it will make a chao in hisogram, like when you try to get the index of second largest bin, and if you delete something, the true index will be +1 or -1.

I think I can do something like reorder the histogram by using the inscreasing result of each bin, but not using the primal bins ordering.

Or I could also try to find something similar to GetMInMaxValue, which is only for C but not for C++, and get the pointer to an array and set all of them as NaN, then try to calcHist again and do minMaxLoc.

Link to the GetMInMaxValue http://docs.opencv.org/2.4/modules/im...

Thank you. Best waschbaer

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-06-18 03:16:39 -0600

berak gravatar image

updated 2017-06-18 03:22:46 -0600

easy ;)

the whole trick is using a copy for "deleting" the largest bin (and not deleting the bin, just resetting its value)

you get the maximum using minMaxLoc, as usual:

double M;
Point pM;
minMaxLoc(mat, 0,&M, 0, &pM);

then you reset the max value from your Mat, and do it again:

Mat mat2 = mat.clone(); // leave original histogram(?) intact 
mat2.at<float>(pM) = 0; // careful with the type !

double M2;
Point pM2;
minMaxLoc(mat2, 0,&M2, 0, &pM2);
// now M2 is the "second largest" item in your hist ;)
edit flag offensive delete link more

Comments

Yeah, that is clever, Thank you.I am just not proper to be an programmer:)

waschbaer gravatar imagewaschbaer ( 2017-06-18 10:34:43 -0600 )edit

Just to point the difference between values and peak. The code works if you want to find 2nd largest value but not for 2nd highest peak. Eg. in bimodal histogram 2nd peak can be far from 2nd largest value (that usually is very close to 1st peak=1st largest value)

pklab gravatar imagepklab ( 2017-06-19 05:43:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-06-17 19:16:13 -0600

Seen: 2,722 times

Last updated: Jun 18 '17