C++ OpenCV:标准化图像的像素值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23178257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:21:44  来源:igfitidea点击:

OpenCV: Normalizing pixel values of an image

c++opencv

提问by Anislein

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.

我试图将图像的像素值归一化为平均值为 0.0 和范数为 1.0,以使图像具有一致的强度。有一个 OpenCV 函数,即cvNormalize(src,dst,0,1,cv_MINMAX),但是这个函数可以用于我的目的吗?任何帮助表示赞赏。谢谢你。

回答by BConic

No, the documentationfor normalizesays:

不,文档normalize

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

normType=NORM_MINMAX(仅适用于密集数组)时,函数会normalize缩放和移动输入数组元素,以便:

equations
(source: opencv.org)

方程
(来源:opencv.org

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

因此,如果您使用normalize(src, dst, 0, 1, NORM_MINMAX, CV_32F);,您的数据将被标准化,以便最小值为 0,最大值为 1。

It is not clear what you meanby 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):

给图像的像素值均值 0.0 和范数 1.0是什么意思不清楚。正如你写的那样,我知道你想要对像素值进行归一化,以便通过堆叠图像列获得的向量的范数为 1.0。如果这是您想要的,您可以使用meanStdDev文档)并执行以下操作(假设您的图像是灰度的):

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.

如果您只想标准化以使像素值的方差为 1,请忽略第三行。

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

是的,这CV_32F意味着生成的图像将使用 32 位浮点数据类型(即float)。