在opencv\sources\samples下面提供了很多的官方例程,是学习OpenCV的最好的资源。
视频教程地址:
https://www.bilibili.com/video/av17748771/?p=12
感谢贾志刚老师的视频教程。
开运算
闭运算
形态学梯度:膨胀减去腐蚀
顶帽(top hat)原图像与开操作之间的差值图像。
黑帽:闭操作图像和原图像之间的差值图像。
提取直线:
提取步骤
输入彩色图像—转化为灰度图像—转化为二值图像—定义结构元素—开操作提取水平与垂直线
代码示例:
#include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespace cv; int main(int argc,char ** argv) { Mat src,dst,dst1,dst2,dst3,dst4,img,gry_img,bin_img,img2; src = imread("1.jpg"); namedWindow("input window",CV_WINDOW_AUTOSIZE); imshow("input window",src); img = imread("2.png"); namedWindow("input window1",CV_WINDOW_AUTOSIZE); imshow("input window1",img); img2 = imread("3.png"); namedWindow("input window3",CV_WINDOW_AUTOSIZE); imshow("input window3",img2); char output_window[] = "morphology demo"; namedWindow(output_window,CV_WINDOW_AUTOSIZE); char output_window1[] = "1"; namedWindow(output_window1,CV_WINDOW_AUTOSIZE); char output_window2[] = "2"; namedWindow(output_window2,CV_WINDOW_AUTOSIZE); char output_window3[] = "3"; namedWindow(output_window3,CV_WINDOW_AUTOSIZE); char output_window4[] = "4"; namedWindow(output_window4,CV_WINDOW_AUTOSIZE); char output_window5[] = "5"; namedWindow(output_window5,CV_WINDOW_AUTOSIZE); char output_window6[] = "6"; namedWindow(output_window6,CV_WINDOW_AUTOSIZE); //形态学开运算 Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1));//结构单元 morphologyEx(src,dst,CV_MOP_OPEN,kernel); //开操作 imshow(output_window,dst); //形态学闭运算 morphologyEx(src,dst1,CV_MOP_CLOSE,kernel); imshow(output_window1,dst1); //形态学梯度 morphologyEx(src,dst2,CV_MOP_GRADIENT,kernel); imshow(output_window2,dst2); //形态学顶帽 morphologyEx(src,dst3,CV_MOP_TOPHAT,kernel); imshow(output_window3,dst3); //形态学黑帽 morphologyEx(src,dst4,CV_MOP_BLACKHAT,kernel); imshow(output_window4,dst4); /*****************************************************************************/ //把图像转化为灰度图像 cvtColor(img,gry_img,CV_BGR2GRAY); imshow(output_window5,gry_img); //转化为二值图像 adaptiveThreshold(~gry_img,bin_img,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,-2); // 二值化图像最大值 自适应 阈值类型 块大小 imshow(output_window6,bin_img); Mat out_img1,out_img2,temp; //水平结构元素 水平比较长,垂直方向只有一个像素,然后做形态学处理 Mat hline = getStructuringElement(MORPH_RECT,Size(img.cols/16,1),Point(-1,-1)); //垂直结构元素 Mat vline = getStructuringElement(MORPH_RECT,Size(1,img.cols/16),Point(-1,-1)); erode(bin_img,temp,hline); dilate(temp,out_img1,hline); bitwise_not(out_img1,out_img1); //取反 imshow("1Fineal img",out_img1); erode(bin_img,temp,vline); //腐蚀 dilate(temp,out_img2,vline); //膨胀 bitwise_not(out_img2,out_img2); //取反 imshow("2Fineal img",out_img2); /*****************************************************************************/ Mat img2_gry,out_img3,temp1,bin_img1,temp2; cvtColor(img2,img2_gry,CV_BGR2GRAY); Mat kernel2 = getStructuringElement(MORPH_RECT,Size(3,3),Point(-1,-1)); adaptiveThreshold(~img2_gry,bin_img1,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,-2); erode(bin_img1,temp1,kernel2); //开操作,先腐蚀,后膨胀 dilate(temp1,out_img3,kernel2); bitwise_not(out_img3,out_img3); //取反 blur(out_img3,out_img3,Size(3,3),Point(-1,-1)); imshow("3",out_img3); waitKey(0); return 0; }
运行效果:
分别提取横线和竖线
消除线的干扰
源码请到github下载:
https://github.com/MRwangmaomao/OpencvMorphology-Project.git重点内容
————————————————
版权声明:本文为CSDN博主「南山二毛」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_16481211/article/details/79578537

