OpenCv 011---像素归一化

1 前备知识

归一化:把数据变成(0,1)或者(1,1)之间的小数,主要是为了方便数据处理。归一化是一种简化计算的方式,即将有量纲的表达式,经过变换,化为无量纲的表达式,成为纯量。有以下几种方式:

(1)x‘ = (x-x_min)/(x_max-x_min)

其中,x是归一化前的值,x‘是归一化后的值,x_max、x_min分别是样本的最大最小值;

(2)平均归一化:x‘ = (x-u)/(x_max-x_min)

其中,x是归一化前的值,x‘是归一化后的值,u是样本的均值,x_max、x_min分别是样本的最大最小值;

*注:(1)和(2)有一个缺陷就是当有新数据加入时,可能导致max和min的变化,需要重新定义。

(3)非线性归一化
  1)对数函数转换:y = log10(x)
  2)反余切函数转换:y = atan(x) * 2 / π
    3)经常用在数据分化比较大的场景,有些数值很大,有些很小。通过一些数学函数,将原始值进行映射。该方法包括 log、指数,正切等。需要根据数据分布的情况,决定非线性函数的曲线,比如log(V, 2)还是log(V, 10)等。

2 所用到的主要OpenCv API

/** @brief Normalizes the norm or value range of an array.

The function cv::normalize normalizes scale and shift the input array elements so that
\f[\| \texttt{dst} \| _{L_p}= \texttt{alpha}\f]
(where p=Inf, 1 or 2) when normType=NORM_INF, NORM_L1, or NORM_L2, respectively; or so that
\f[\min _I \texttt{dst} (I)= \texttt{alpha} , \, \, \max _I \texttt{dst} (I)= \texttt{beta}\f]

when normType=NORM_MINMAX (for dense arrays only). The optional mask specifies a sub-array to be
normalized. This means that the norm or min-n-max are calculated over the sub-array, and then this
sub-array is modified to be normalized. If you want to only use the mask to calculate the norm or
min-max but modify the whole array, you can use norm and Mat::convertTo.

In case of sparse matrices, only the non-zero values are analyzed and transformed. Because of this,
the range transformation for sparse matrices is not allowed since it can shift the zero level.

Possible usage with some positive example data:
@code{.cpp}
vector<double> positiveData = { 2.0, 8.0, 10.0 };
vector<double> normalizedData_l1, normalizedData_l2, normalizedData_inf, normalizedData_minmax;

// Norm to probability (total count)
// sum(numbers) = 20.0
// 2.0 0.1 (2.0/20.0)
// 8.0 0.4 (8.0/20.0)
// 10.0 0.5 (10.0/20.0)
normalize(positiveData, normalizedData_l1, 1.0, 0.0, NORM_L1);

// Norm to unit vector: ||positiveData|| = 1.0
// 2.0 0.15
// 8.0 0.62
// 10.0 0.77
normalize(positiveData, normalizedData_l2, 1.0, 0.0, NORM_L2);

// Norm to max element
// 2.0 0.2 (2.0/10.0)
// 8.0 0.8 (8.0/10.0)
// 10.0 1.0 (10.0/10.0)
normalize(positiveData, normalizedData_inf, 1.0, 0.0, NORM_INF);

// Norm to range [0.0;1.0]
// 2.0 0.0 (shift to left border)
// 8.0 0.75 (6.0/8.0)
// 10.0 1.0 (shift to right border)
normalize(positiveData, normalizedData_minmax, 1.0, 0.0, NORM_MINMAX);
@endcode

@param src input array.
@param dst output array of the same size as src .
@param alpha norm value to normalize to or the lower range boundary in case of the range
normalization.
@param beta upper range boundary in case of the range normalization; it is not used for the norm
normalization.
@param norm_type normalization type (see cv::NormTypes).
@param dtype when negative, the output array has the same type as src; otherwise, it has the same
number of channels as src and the depth =CV_MAT_DEPTH(dtype).
@param mask optional operation mask.
@sa norm, Mat::convertTo, SparseMat::convertTo
*/

CV_EXPORTS_W void normalize( InputArray src, InputOutputArray dst, double alpha = 1, double beta = 0,
                             int norm_type = NORM_L2, int dtype = -1, InputArray mask = noArray());

/** @overload
@param src input array.
@param dst output array of the same size as src .
@param alpha norm value to normalize to or the lower range boundary in case of the range
normalization.
@param normType normalization type (see cv::NormTypes).
*/

CV_EXPORTS void normalize( const SparseMat& src, SparseMat& dst, double alpha, int normType );

3 程序代码

//程序DEMO流程:
//读取图片--》判断并显示图片--》转换为灰度图--》转换为浮点数类型数组--》四种归一化方式
//1)scale and shift by NORM_MINMAX
//2)scale and shift by NORM_INF
//3)scale and shift by NORM_L1
//4)scale and shift by NORM_L2
//--》归一化的范围设置为1.0 - 0
//--》不同的归一化方式结果出来要乘以对应的数值
//--》将结果转换为CV_8UC1
//--》显示图片

#include"opencv2\opencv.hpp"
#include"iostream"

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    Mat src = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\colormap.png");
    if (src.empty())
    {
        printf("Could not load image...\n");
        return -1;
    }
    imshow("srcImg", src);

    Mat src_gray, src_gray_f;
    cvtColor(src, src_gray, CV_RGB2GRAY);//转换为灰度图
    src_gray.convertTo(src_gray_f, CV_32F);//转换为浮点数类型数组

    //scale and shift by NORM_MINMAX
    Mat dst = Mat::zeros(src_gray.size(), CV_32FC1);
    normalize(src_gray_f, dst, 1.0, 0, NORM_MINMAX);
    Mat result = dst * 255;
    result.convertTo(dst, CV_8UC1);
    imshow("NORM_MINMAX", dst);

    //scale and shift by NORM_INF
    normalize(src_gray_f, dst, 1.0, 0, NORM_INF);
    result = dst * 255;
    result.convertTo(dst, CV_8UC1);
    imshow("NORM_INF", dst);

    //scale and shift by NORM_L1
    normalize(src_gray_f, dst,1.0, 0, NORM_L1);
    result = dst * 100000000;
    result.convertTo(dst, CV_8UC1);
    imshow("NORM_L1", dst);

    //scale and shift by NORM_L2
    normalize(src_gray_f, dst, 1.0, 0, NORM_L2);
    result = dst * 10000;
    result.convertTo(dst, CV_8UC1);
    imshow("NORM_L2", dst);

    waitKey(0);
    return 0;
}

4 运行结果

5 扩展及注意事项

null

原文地址:https://www.cnblogs.com/Vince-Wu/p/11150315.html

时间: 2024-08-04 06:23:01

OpenCv 011---像素归一化的相关文章

OpenCV改变像素颜色

Mat src=imread("image/color.jpg"); imshow("a",src); int i,j; int cPointR,cPointG,cPointB,cPoint;//currentPoint; for(i=1;i<src.rows;i++) for(j=1;j<src.cols;j++) { cPointB=src.at<Vec3b>(i,j)[0]; cPointG=src.at<Vec3b>(i,

OpenCV亚像素级的角点检测

亚像素级的角点检测 目标 在本教程中我们将涉及以下内容: 使用OpenCV函数 cornerSubPix 寻找更精确的角点位置 (不是整数类型的位置,而是更精确的浮点类型位置). 理论 代码 这个教程的代码如下所示.源代码还可以从 这个链接下载得到 #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #inclu

opencv图像像素值读取

说到图像像素,肯定要先认识一下图像中的坐标系长什么样. 1. 坐标体系中的零点坐标为图片的左上角,X轴为图像矩形的上面那条水平线:Y轴为图像矩形左边的那条垂直线.该坐标体系在诸如结构体Mat,Rect,Point中都是适用的.(OpenCV中有些数据结构的坐标原点是在图片的左下角,可以设置的). 2. 在使用image.at<TP>(x1, x2)来访问图像中点的值的时候,x1并不是图片中对应点的x轴坐标,而是图片中对应点的y坐标(也就是编程中的pic.rows那行).x2同理. 3. 如果所

python opencv:像素运算

以下运算两个图像的大小需要一样 算术运算: cv2.add(img1, img2):两个图像像素相加 cv2.subtract(img1, img2):两个图像像素相减 cv2.multiply(img1, img2):两个图像像素相乘 cv2.divide(img1, img2):两个图像像素相除 cv2.mean(img):图像的均值 cv2.meanStdDev(img):方差 逻辑运算: cv2.bitwise_and(img1, img2):与 cv2.bitwise_or(img1,

Opencv 亚像素级别角点检测

Size winSize = Size(5,5); Size zerozone = Size(-1,-1); TermCriteria tc = TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 40, 0.001); cornerSubPix(img_gray1, vec_points, winSize, zerozone, tc); 原文地址:https://www.cnblogs.com/herd/p/9742018.html

OpenCV 学习(像素操作 Manipuating the Pixels)

OpenCV 学习(像素操作 Manipuating the Pixels) OpenCV 虽然提供了许多类型的图像处理函数,可以对图像进行各种常见的处理,但是总会有些操作时没有的,这时我们就需要自己来操纵像素,实现我们需要的功能.今天就来讲讲 OpenCV 进行像素级操作的几种方法,并做个比较. 在 OpenCV 中,图像用矩阵来表示,对应的数据类型为 cv::Mat . cv::Mat 功能很强大,矩阵的元素可以为字节.字.浮点数.数组等多种形式.对于灰度图像,每个像素用一个 8 bit 字

Atitit &#160;&#160;图像处理&#160;平滑&#160;也称&#160;模糊,&#160;归一化块滤波、高斯滤波、中值滤波、双边滤波)

Atitit   图像处理 平滑 也称 模糊, 归一化块滤波.高斯滤波.中值滤波.双边滤波) 是一项简单且使用频率很高的图像处理方法 用途 去噪 去雾 各种线性滤波器对图像进行平滑处理,相关OpenCV函数如下: 归一化块滤波器 (Normalized Box Filter) § 最简单的滤波器, 输出像素值是核窗口内像素值的 均值 ( 所有像素加权系数相等) § 高斯滤波器 (Gaussian Filter) § 最有用的滤波器 (尽管不是最快的). 高斯滤波是将输入数组的每一个像素点与 高斯

图像载入 imshow()[OpenCV 笔记5]

void imshow(const string& winname InputArray mat); winname 窗口表识名称 mat 需要显示的图像.InputArray类型,声明如下 typedef const _InputArray& InputArray; _InputArray定义比较复杂,类里先定义了一个枚举,然后是各类的模版类型和一些方法.遇到InputArray/OutputArray类型,可以把它当作Mat类型处理. 图像大小缩放 如果窗口是用CV_WINDOW_AU

图像处理之角点检测与亚像素角点定位

角点是图像中亮度变化最强地方反映了图像的本质特征,提取图像中的角点可以有效提高图像处理速度与精准度.所以对于整张图像来说特别重要,角点检测与提取的越准确图像处理与分析结果就越接近真实.同时角点检测对真实环境下的对象识别.对象匹配都起到决定性作用.Harr角点检测是图像处理中角点提取的经典算法之一,应用范围广发,在经典的SIFT特征提取算法中Harr角点检测起到关键作用.通常对角点检测算法都有如下要求: 1. 基于灰度图像.能够自动调整运行稳定,检测出角点的数目. 2. 对噪声不敏感.有一定的噪声