1 #include <opencv2/opencv.hpp> 2 #include <opencv2/core/core.hpp> 3 #include <opencv2/highgui/highgui.hpp> 4 #include<iostream> 5 #include<stdlib.h> 6 using namespace cv; 7 using namespace std; 8 int main() 9 { 10 Mat Img,grayImg,gradx,blurgradx,gradAbsx,threshImg,close_threshImg,erode_threshImg,dilate_threshImg; 11 Img=imread("E:\txm1.jpg"); 12 imshow("0原图",Img); 13 cvtColor(Img,grayImg,COLOR_BGR2GRAY); 14 imshow("1灰度图",grayImg); 15 Scharr(grayImg,gradx,CV_16S,1,0); 16 imshow("2边缘检测图",gradx); 17 convertScaleAbs(gradx,gradAbsx); 18 imshow("3Abs图",gradAbsx); 19 blur(gradAbsx,blurgradx,Size(5,5)); 20 imshow("4滤波图",blurgradx); 21 threshold(blurgradx,threshImg,150,255,CV_8UC1); 22 imshow("4.5阈值分割图",threshImg); 23 Mat kernal=getStructuringElement(MORPH_RECT,Size(25,5)); 24 morphologyEx(threshImg,close_threshImg,MORPH_CLOSE,kernal); 25 imshow("5闭操作",close_threshImg); 26 Mat edkl=getStructuringElement(MORPH_RECT,Size(3,3)); 27 erode(close_threshImg,erode_threshImg,edkl,Point(-1,-1),8); 28 imshow("6腐蚀操作",erode_threshImg); 29 dilate(erode_threshImg,dilate_threshImg,edkl,Point(-1,-1),15); 30 imshow("7膨胀操作",dilate_threshImg); 31 waitKey(0); 32 return 0; 33 34 }
运行结果: