OpenCV学习笔记之提取水平线垂直线

包含头文件

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<iostream>
#include <opencv2\imgproc\types_c.h>
using namespace std;
using namespace cv;

四步:先转化为灰度图再转化为二值化图像再定义结构元素最后运用开运算

int main() {
	Mat src = imread("线.jpg");  // 载入图片并显示;
	imshow("src", src);


	Mat gray ;					//转化为灰度图并显示。
	cvtColor(src, gray, CV_BGR2GRAY);	
	imshow("灰度图", gray);


	Mat binary;					//转化为二值化图像并显示。
	adaptiveThreshold(~gray, binary,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,15,-2);
	imshow("二值化图像",binary);
	                            //定义结构元素
	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 temp1,temp2;			//先腐蚀再膨胀
	erode(binary,temp1,hline);
	dilate(temp1, temp2, hline);
	bitwise_not(temp2,temp2);  转化背景颜色
	imshow("横线", temp2);

	Mat temp3, temp4;			//先腐蚀再膨胀
	erode(binary, temp3, vline);
	dilate(temp3, temp4, vline);
	bitwise_not(temp4, temp4);	
	imshow("竖线", temp4);


	waitKey(0);
	return (0);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值