本文实例为大家分享了OpenCV实现直线检测并消除的具体代码,供大家参考,具体内容如下 很简单,代码如下 #include<iOStream> #include<
本文实例为大家分享了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;
}
放几张效果图
原图
处理结果
--结束END--
本文标题: OpenCV实现直线检测并消除
本文链接: https://lsjlt.com/news/128881.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0