包含头文件
#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);
}