Java and OpenCV: Calculate Median / Mean / Stdev value of MAT (Gray-Image)

tim picture tim · Mar 13, 2014 · Viewed 8.2k times · Source

I've got an image as grayscale image using

Mat m = Highgui.imread(path, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

in Java and now I want to calculate the median value, the mean value and the standard deviation. I'n new to this and I simply don't know how to find out the calculations and stuff... Tried 2 hours of googeling, please give me some basic advise on how to do research on this. I tried keywords like "mean value java opencv" and found this:

http://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#meanStdDev

I'm now doing this:

MatOfDouble mu = new MatOfDouble();
MatOfDouble sigma = new MatOfDouble();
Core.meanStdDev(m, mu, sigma);

But HOW the hell to access the mean/stdev value as a double? Tried things like

double d = mu.get(0,0)

but there's something wrong. I can't find it out by reading the OpenCV Java docs myself unfortunately as I don't know what to look for/at.

I need mean/stdev to calculate the thresholds for the canny filter... Thanks so far in advance

Answer

Chewxy picture Chewxy · Mar 16, 2014

try this:

double d = mu.get(0,0)[0]

mu.get(0,0) returns a double[], so you can just get the first element - it's equivalent to the C++ version of:

mu.val[0]

Hope it helps.