亲宝软件园·资讯

展开

OpenCV直线检测并消除 OpenCV实现直线检测并消除

Leonwenbin 人气:0
想了解OpenCV实现直线检测并消除的相关内容吗,Leonwenbin在本文为您仔细讲解OpenCV直线检测并消除的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:OpenCV直线检测,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;
}

放几张效果图

原图

处理结果

加载全部内容

相关教程
猜你喜欢
用户评论