OpenCV实现直线检测并消除

时间:2022-05-18 10:42:07

本文实例为大家分享了OpenCV实现直线检测并消除的具体代码,供大家参考,具体内容如下

很简单,代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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实现直线检测并消除

原图

OpenCV实现直线检测并消除

处理结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_43788499/article/details/84981528