感谢链接中各位博主的博文帮助我对图像预处理的理解!
一.图像预处理
图像预处理的主要目的:
消除图像中无关的信息,恢复有用的真实信息,增强有关信息的可检测性和最大限度地简化数据,从而改进特征抽取、图像分割、匹配和识别的可靠性。
直方图均衡化
目的:通过直方图均衡化,图像像素分配更加均匀,包含的像素灰度值更加丰富,对比度更高。
#include<opencv2/opencv.hpp>
#include<vector>
using namespace std;
using namespace cv;
int main() {
Mat temp = imread("C:\\Users\\26011\\Desktop\\毕设\\毕设程序\\picture\\2.bmp",0);
imshow("原图", temp);
equalizeHist(temp, temp);
imshow("均衡后", temp);
waitKey(-1);
}
滤波
图像处理中常见的滤波算法:均值滤波、中值滤波、高斯滤波等。
高斯滤波:对图像进行平滑的同时,能都更多的保留图像的总体灰度分布特征
GaussianBlur
GaussianBlur函数原型: 通过调整高斯函数的标准差(sigma)来控制平滑程度,sigma值越大,滤波窗口越大,滤波效果越明显(图像越模糊钝化)
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
double sigmaX, double sigmaY = 0,
int borderType = BORDER_DEFAULT );
参数详解如下:
1.src,输入图像,即源图像
2.dst,即目标图像,
3.ksize,高斯内核的大小。
4.sigma,控制平滑程度,sigma值越大,图像越模糊钝化
二值化
图像的二值化就是将图像上的像素点的灰度值设置为0或255,使整个图像呈现出明显的黑白效果,使图像中数据量大为减少,从而凸显出目标的轮廓。
OpenCv二值化函数——Threshold
void cvThreshold( const CvArr* src, CvArr* dst, double threshold,
double max_value, int threshold_type );
参数详解如下:
1.src是指输入的图像,
2.dst指的是处理后的图像,
3.threshold,表示阈值,
4. max_value是当threshold_type阈值类型type取THRESH_BINARY 或THRESH_BINARY_INV阈值类型时的最大值.,
5. threshold_type 表示运算方法,通过查询源文件可以看到type的取值情况;
#include <iostream>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat imag, result;
imag = imread("C:\\Users\\26011\\Desktop\\毕设\\毕设程序\\picture\\2.bmp",0); //将读入的彩色图像直接以灰度图像读入
namedWindow("原图", 1);
imshow("原图", imag);
result = imag.clone();
//进行二值化处理,选择30,200.0为阈值
threshold(imag, result, 30, 200.0, CV_THRESH_BINARY);
namedWindow("二值化图像");
imshow("二值化图像", result);
waitKey();
return 0;
}