本篇内容介绍了“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;
}
放几张效果图
原图
处理结果
“OpenCV怎么实现直线检测并消除”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!