opencv——提取水平线与垂直线

推荐博主——点击这里

实现代码

#include
#include
using namespace std;
using namespace cv;
int main()
{
     
	Mat srcimage=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/水平与垂直.png");
	if(!srcimage.data) {
     
		cout<<"No image!"<<endl; return -1; 
	}
	namedWindow("原图");
	imshow("原图",srcimage);
	//转化为灰度图像
	Mat grayimage;
	cvtColor(srcimage,grayimage,COLOR_BGR2GRAY);
	//转为二值化图像
	Mat image;
	//void adaptiveThreshold( InputArray src, OutputArray dst,double maxValue, int adaptiveMethod,int thresholdType, int blockSize, double C );
	adaptiveThreshold(grayimage,image,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,-2);
	//定义垂直和水平结构体
	Mat lelement=getStructuringElement(MORPH_RECT,Size(1,srcimage.rows/16),Point(-1,-1));
	Mat helement=getStructuringElement(MORPH_RECT,Size(srcimage.cols/16,1),Point(-1,-1));
	//开操作
	/*
	Mat temp,dst;
	erode(image,temp,hline);//对垂直线进行腐蚀
	dilate(temp,dst,hline);//再膨胀,对水平线被腐蚀部分进行还原,最后提取到水平线
	bitwise_not(dst,dst);//反向取像素
	namedWindow("提取水平线");
	imshow("提取水平线",dst)
	*/
	Mat finalimage;
	morphologyEx(image,finalimage,MORPH_OPEN,helement);
	namedWindow("提取水平线");
	imshow("提取水平线",finalimage);
	waitKey(0);
	return 0;
}

你可能感兴趣的:(opencv,opencv,计算机视觉)