OpenCv图像处理之图像归一化

OpenCv图像处理之图像归一化

归一化

图像处理中,图像单通道像素值为0~255之间的uchar类型,通常使用min-max归一化将其转化为0~1区间之间,既不会改变数据的分布和信息存储,又加速了后续网络的计算。
min-max归一化公式:x* = x-x_min/(x_max-x_min);每个像素值减去最小值后除以图像中最大值和最小值的差

中心化

中心化就是零均值化,对于每一个像素减去图像像素的均值即可。E(X-E(X)) = 0,有效避免Z型更新,加速网络的收敛速度。

标准化

标准化就是图像的每个像素值减去图像像素的均值得到的结果,再除以标准差,使得分布符合标准正态分布,这样有利于消除量纲对结果的影响,加速收敛速度。比如不同相机拍摄的亮度、对比度和尺度不同的图片,如果不使用标准化对图像进行预处理,那么必然会影响最终的结果。

来看一下源码中归一化函数的声明和参数描述

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());

@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::

上述参数描述的是将输入的图像src归一化为图像dst(输出的图像),dst要和src拥有相同的尺度,其中alpha是归一化区间的下限,beta是归一化区间的上限,norm_type是归一化的类型,默认为NORM_L2。dtype默认为负数,表示dst和src拥有相同的数据类型和通道数。

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace std;
using namespace cv;

void resize_img(Mat &mat, int width, int height, int interpolation = INTER_AREA);

int main() {

    Mat original_img, normalize_img, gray_img;
    original_img = imread("D:/cat.jpg", 3);
    if (original_img.empty()) {
        cout << "open the file failed" << endl;
        return -1;
    }
    double scale = 0.5;
    int width = int(original_img.cols * scale);
    int height = int(original_img.rows * scale);
    Mat clone_img = original_img.clone();
    resize_img(clone_img, width, height);
    imshow("1-original_img", clone_img);
    cvtColor(clone_img, gray_img, COLOR_BGR2GRAY);
    imshow("2-original_gray", gray_img);
    //convertTo(outputArray,rtype,alpha,beta);
    //gray_img.convertTo(gray_img,CV_32F);gray将自身像素值乘以alpha+beta赋值给gray_img,alpha默认为1,beta默认为0。rtype为转换后的数据类型,将uchar类型转成float
    gray_img.convertTo(gray_img, CV_32F);
    normalize_img = Mat::zeros(Size(width, height), CV_32FC1);
    cv::normalize(gray_img, normalize_img, 1.0, 0, NORM_MINMAX);
    imshow("3-norm_maxmin_img", normalize_img);
    waitKey(0);
    destroyAllWindows();
    return 0;
}

void resize_img(Mat &mat, int width, int height, int interpolation) {
    resize(mat, mat, Size(width, height), 0, 0, interpolation);
}

效果显示
在这里插入图片描述

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值