opencv形态学直线提取

#include<opencv2\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int agrc, char**agrv){


Mat src = imread("5.jpg");
if (src.empty()){
cout << "没有找到图片" << endl;
return -1;

}
namedWindow("1", CV_WINDOW_NORMAL);
imshow("1", src);
Mat dst1, dst2,dst3;
cvtColor(src, dst1, CV_BGR2GRAY);//转为灰度图


//转为二值图像
adaptiveThreshold(~dst1, dst2, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
namedWindow("2", CV_WINDOW_NORMAL);
imshow("2", dst2);




//结构元素
Mat hine = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1), Point(-1,-1) );
Mat vhine = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1));
//自定义设置的矩形结构元素
Mat kernel = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1, -1));


Mat temp;
/*erode(dst2, temp, kernel);
dilate(temp, dst3, kernel);*/


morphologyEx(dst2, dst3,CV_MOP_OPEN,kernel);


bitwise_not(dst3, dst3);
namedWindow("3", CV_WINDOW_NORMAL);
imshow("3", dst3);




//char INPUT_WIN[] = "input image";
//char OUTPUT_WIN[] = "result image";
//namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
//imshow(INPUT_WIN, src);


//Mat gray_src;
//cvtColor(src, gray_src, CV_BGR2GRAY);
//imshow("gray image", gray_src);


//Mat binImg;
//adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
//imshow("binary image", binImg);


//// 水平结构元素
//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 kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));


//Mat temp,dst;
//erode(binImg, temp, kernel);
//dilate(temp, dst, kernel);
//// morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
//bitwise_not(dst, dst);
////blur(dst, dst, Size(3, 3), Point(-1, -1));
//imshow("Final Result", dst);








waitKey(0);
return 0;
}

猜你喜欢

转载自blog.csdn.net/gt18120588267/article/details/79902734