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