Mat xuenai =imread("xuenai.jpg");imshow("xuenai",xuenai);Matxuenai_blur(xuenai.size(),xuenai.type());blur(xuenai,xuenai_blur,Size(3,5));imshow("xuenai_blur",xuenai_blur);waitKet();
Mat xuenai =imread("xuenai.jpg");imshow("xuenai",xuenai);Matxuenai_Gauss(xuenai.size(),xuenai.type());GaussianBlur(xuenai,xuenai_Gauss,Size(-1,-1),10);imshow("xuenai_Gauss",xuenai_Gauss);waitKet();
Mat xuenai =imread("xuenai.jpg");imshow("xuenai",xuenai);Matxuenai_median(xuenai.size(),xuenai.type());medianBlur(xuenai,xuenai_median,5);imshow("xuenai_median",xuenai_median);waitKet();
Mat xuenai =imread("xuenai.jpg");imshow("xuenai",xuenai);Matxuenai_bilateral(xuenai.size(),xuenai.type());bilateralFilter(xuenai,xuenai_bilateral,-1,100,10);imshow("xuenai_bilateral",xuenai_bilateral);waitKet();
5.获取用来形态学操作的滤波器
CV_EXPORTS_W Mat getStructuringElement(int shape, Size ksize, Point anchor =Point(-1,-1));enumMorphShapes { MORPH_RECT =0, //!< a rectangular structuring element: \f[E_{ij}=1\f] MORPH_CROSS =1, //!< a cross-shaped structuring element: //!< \f[E_{ij} = \begin{cases} 1 & \texttt{if } {i=\texttt{anchor.y } {or } {j=\texttt{anchor.x}}} \\0 & \texttt{otherwise} \end{cases}\f]
MORPH_ELLIPSE =2//!< an elliptic structuring element, that is, a filled ellipse inscribed //!< into the rectangle Rect(0, 0, esize.width, 0.esize.height)};
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_erode(xuenai.size(),xuenai.type());erode(xuenai_threshold,xuenai_erode,kernal);imshow("xuenai_erode",xuenai_erode);waitKet();
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_dilate(xuenai.size(),xuenai.type());dilate(xuenai_threshold,xuenai_dilate,kernal);imshow("xuenai_dilate",xuenai_dilate);waitKet();
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_morphology(xuenai.size(),xuenai.type());morphologyEx(xuenai_threshold,xuenai_morphology,MORPH_OPEN,kernal);imshow("xuenai_morphology",xuenai_morphology);waitKet();
7.4 闭
原理
对输入图片先进行膨胀,然后进行腐蚀。可以用来屏蔽与滤波器大小相当的暗部。
效果
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_morphology(xuenai.size(),xuenai.type());morphologyEx(xuenai_threshold,xuenai_morphology,MORPH_CLOSE,kernal);imshow("xuenai_morphology",xuenai_morphology);waitKet();
7.5 顶帽
原理
对输入图片先进行开操作,然后原图-开操作图。可以用来提取与滤波器大小相当的亮部。
效果
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_morphology(xuenai.size(),xuenai.type());morphologyEx(xuenai_threshold,xuenai_morphology,MORPH_TOPHAT,kernal);imshow("xuenai_morphology",xuenai_morphology);waitKet();
7.6 黑帽
原理
对输入图片先进行闭操作,然后闭操作图-原图。可以用来提取与滤波器大小相当的暗部。
效果
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_morphology(xuenai.size(),xuenai.type());morphologyEx(xuenai_threshold,xuenai_morphology,MORPH_BLACKHAT,kernal);imshow("xuenai_morphology",xuenai_morphology);waitKet();
7.7 形态学梯度
原理
膨胀图与腐蚀图之差。可以用来 提取边界轮廓 ,但提取效果比不上专业的边缘检测算法。
效果
Mat xuenai =imread("xuenai.jpg");Matxuenai_gray(xuenai.size(),xuenai.type());cvtColor(xuenai,xuenai_gray,COLOR_BGR2GRAY);Matxuenai_threshold(xuenai.size(),xuenai.type());threshold(xuenai_gray,xuenai_threshold,100,255,THRESH_BINARY);imshow("xuenai_threshold",xuenai_threshold);Mat kernal=getStructuringElement(MORPH_RECT,Size(3,3));Matxuenai_morphology(xuenai.size(),xuenai.type());morphologyEx(xuenai_threshold,xuenai_morphology,MORPH_GRADIENT,kernal);imshow("xuenai_morphology",xuenai_morphology);waitKet();
enumNormTypes { NORM_INF =1, NORM_L1 =2, NORM_L2 =4, NORM_L2SQR =5, NORM_HAMMING =6, NORM_HAMMING2 =7, NORM_TYPE_MASK =7, //!< bit-mask which can be used to separate norm type from norm flags NORM_RELATIVE =8, //!< flag NORM_MINMAX =32//!< flag};
Mat xuenai =imread("xuenai.jpg");imshow("xuenai",xuenai);Matxuenai_canny(xuenai.size(),xuenai.type());Canny(xuenai,xuenai_canny,60,150);imshow("xuenai_canny",xuenai_canny);waitKet();
13.添加噪声
为了检测算法的稳定性,常常需要在图片中人为地添加一些噪声来进行检验。
13.1 椒盐噪声
staticvoidaddSaltNoise(constMat& src,Mat& dst,int num=1000){ dst=src.clone();for (int k =0; k < num; k++) { //随机取值行列,得到像素点(i,j)int i =rand() %dst.rows;int j =rand() %dst.cols; //修改像素点(i,j)的像素值for(int channel=0;channel<src.channels();channel++){dst.ptr(i,j)[channel]=255; } }for (int k =0; k < num; k++) { //随机取值行列 default_random_engine engine; uniform_int_distribution<unsigned>u(0,10000);int i =rand() %dst.rows;int j =rand() %dst.cols; //修改像素点(i,j)的像素值for(int channel=0;channel<src.channels();channel++){dst.ptr(i,j)[channel]=0; } }return;}