OpenCV实现直线检测并消除
作者:Leonwenbin
这篇文章主要为大家详细介绍了OpenCV实现直线检测并消除,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了OpenCV实现直线检测并消除的具体代码,供大家参考,具体内容如下
很简单,代码如下
#include<iostream> #include<opencv.hpp> using namespace cv; using namespace std; int main() { Mat img, dst, img_gary, img_bin, temp, r_line, c_line, kernel; char INPUT[] = "input"; char OUTPUT[] = "output"; char GRAY_IMG[] = "gary image"; char BIN_IMG[] = "binary image"; char DST_IMG[] = "final image"; img = imread("D:\\OpenCV\\images\\demo_test_2.jpg"); if (img.empty()) { cout << "image loading failed..." << endl; return -1; } namedWindow(INPUT, WINDOW_AUTOSIZE); imshow(INPUT, img); cvtColor(img, img_gary, COLOR_BGR2GRAY); imshow(GRAY_IMG, img_gary); adaptiveThreshold(~img_gary, img_bin, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2); imshow(BIN_IMG, img_bin); //水平结构元素 r_line = getStructuringElement(MORPH_RECT, Size(img.cols / 16, 1), Point(-1, -1)); //垂直结构元素 c_line = getStructuringElement(MORPH_RECT, Size(1, img.rows / 16), Point(-1, -1)); kernel = getStructuringElement(MORPH_RECT, Size(4, 4), Point(-1, -1)); erode(img_bin, temp, kernel); dilate(temp, dst, kernel); //erode(img_bin, temp, r_line); //dilate(temp, dst, r_line); //morphologyEx(img_bin, dst, MORPH_OPEN, c_line); bitwise_not(dst, dst); //blur(dst, dst, Size(3, 3), Point(-1, -1)); imshow(DST_IMG, dst); waitKey(0); return 0; }
放几张效果图
原图
处理结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。