Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: Normalizing pixel values of an image

Tags:

c++

opencv

I am trying to normalize the pixel values of an image to have a mean value of 0.0 and a norm of 1.0 to give the image a consistent intensity. There is one OpenCV function, i.e. cvNormalize(src,dst,0,1,cv_MINMAX), but can this function be used for my purpose? Any help is appreciated. Thank you.

like image 783
Anislein Avatar asked Apr 20 '14 03:04

Anislein


1 Answers

No, the documentation for normalize says:

When normType=NORM_MINMAX (for dense arrays only), the functions normalize scale and shift the input array elements so that:

equations
(source: opencv.org)

Hence, if you use normalize(src, dst, 0, 1, NORM_MINMAX, CV_32F);, your data will be normalized so that the minimum is 0 and the maximum is 1.

It is not clear what you mean by giving the pixel values of the image mean 0.0 and a norm of 1.0. As you wrote it, I understand that you want to normalize the pixel values so that the norm of the vector obtained by stacking image columns is 1.0. If that is what you want, you can use meanStdDev (documentation) and do the following (assuming your image is grayscale):

cv::Scalar avg,sdv;
cv::meanStdDev(image, avg, sdv);
sdv.val[0] = sqrt(image.cols*image.rows*sdv.val[0]*sdv.val[0]);
cv::Mat image_32f;
image.convertTo(image_32f,CV_32F,1/sdv.val[0],-avg.val[0]/sdv.val[0]);

If you just want to normalize so that the variance of the pixel values is one, ignore the third line.

And yes, the CV_32F means that the resulting image will use 32 bit floating point datatype (i.e. float).

like image 152
BConic Avatar answered Oct 04 '22 20:10

BConic