openCV学习5-膨胀、腐蚀+形态学操作

1.膨胀、腐蚀




#include <opencv2\core\core.hpp>  
#include <opencv2\highgui\highgui.hpp>  
#include <opencv2\imgproc\imgproc.hpp>  
#include <opencv2\objdetect\objdetect.hpp>  
#include <opencv2\imgproc\types_c.h>  
#include <opencv2\objdetect\objdetect_c.h>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cmath>
#include<cstdio>

using namespace std;
using namespace cv;

int ele=3;
int max_size=21;
char name[]="out";
Mat src,dst;

void CallBack(int ,void*)
{
	int s=ele*2+1;
	Mat el=getStructuringElement(MORPH_RECT,Size(s,s),Point(-1,-1));
	//dilate(src,dst,el,Point(-1,-1),1);//膨胀
	erode(src,dst,el);//腐蚀
	imshow(name,dst);
	return;
}

int main()
{
	src=imread("cat.jpg");
	if(src.empty())
	{
		cout<<"!!???"<<endl;
		return -1;
	}

	namedWindow("cat!",1);
	imshow("cat!",src);

	namedWindow(name,1);
	createTrackbar("Element Size : ",name,&ele,max_size,CallBack);
	CallBack(0,0);

	waitKey(0);
	return 0;
}

2.形态学操作







#include <opencv2\core\core.hpp>  
#include <opencv2\highgui\highgui.hpp>  
#include <opencv2\imgproc\imgproc.hpp>  
#include <opencv2\objdetect\objdetect.hpp>  
#include <opencv2\imgproc\types_c.h>  
#include <opencv2\objdetect\objdetect_c.h>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cmath>
#include<cstdio>

using namespace std;
using namespace cv;

int main()
{
	Mat src,dst;
	src=imread("cat.jpg");
	if(src.empty())
	{
		cout<<"!!???"<<endl;
		return -1;
	}
	namedWindow("cat",1);
	imshow("cat",src);

	Mat kernel=getStructuringElement(MORPH_RECT,Size(5,5),Point(-1,-1));
	//morphologyEx(src,dst,CV_MOP_OPEN,kernel);//开操作,去掉小的对象
	//morphologyEx(src,dst,CV_MOP_CLOSE,kernel);//闭操作,填掉小的洞
	//morphologyEx(src,dst,CV_MOP_GRADIENT,kernel);//形态学梯度
	//morphologyEx(src,dst,CV_MOP_TOPHAT,kernel);//顶帽
	//morphologyEx(src,dst,CV_MOP_BLACKHAT,kernel);//黑帽
	namedWindow("out",1);
	imshow("out",dst);

	waitKey(0);
	return 0;
}

3.提取水平与垂直线



#include <opencv2\core\core.hpp>  
#include <opencv2\highgui\highgui.hpp>  
#include <opencv2\imgproc\imgproc.hpp>  
#include <opencv2\objdetect\objdetect.hpp>  
#include <opencv2\imgproc\types_c.h>  
#include <opencv2\objdetect\objdetect_c.h>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cmath>
#include<cstdio>

using namespace std;
using namespace cv;

int main()
{
	Mat src,dst;
	src=imread("milk.png");
	if(src.empty())
	{
		cout<<"!!???"<<endl;
		return -1;
	}
	namedWindow("milk!",1);
	imshow("milk!",src);

	//把彩色->灰色的
	Mat src_gray;
	cvtColor(src,src_gray,CV_BGR2GRAY);
	imshow("gray",src_gray);

	//变成二值图像
	Mat bin;
	adaptiveThreshold(~src_gray,bin,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,-2);
	imshow("bin",bin);

	//开操作
	Mat hline=getStructuringElement(MORPH_RECT,Size(src.cols/16,1),Point(-1,-1));//水平
	Mat vline=getStructuringElement(MORPH_RECT,Size(1,src.rows/16),Point(-1,-1));//垂直

	Mat temp;
	//erode(bin,temp,hline);
	//dilate(temp,dst,hline);
	//erode(bin,temp,vline);
	//dilate(temp,dst,vline);
	morphologyEx(bin,dst,CV_MOP_OPEN,vline);
	bitwise_not(dst,dst);
	blur(dst,dst,Size(3,3),Point(-1,-1));
	imshow("heihei",dst);

	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39396954/article/details/80514133