影醉阏轩窗

衣带渐宽终不悔,为伊消得人憔悴。
扩大
缩小

《图像处理实例》 之 提取特殊背景的直线

目标:提取图像中的直线

直接运用霍夫变换行不通,需要经过形态学处理之后再霍夫提取!

自动化阈值OTSU算法:

形态学操作:

 这一步其实已经分离出我们需要的区域,可以用我们其他博客所说的方法进行操作--水漫算法,也可以用下面的霍夫变换!

霍夫变换:

水漫的好处是可以操作这个区域,霍夫的好处是可以得到这个位置!

 

上代码:

 

 1 #include<iostream>
 2 #include <opencv2/opencv.hpp>
 3 #include <math.h>
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int Threshold_Value = 176;
 8 const int Threshold_Max_value = 255;
 9 const int Threshold_type_value = 3;
10 double MaxWidth = 0, MaxHeight = 0;//找最大的矩形边
11 
12 RNG rng(12345);
13 
14 Mat input_image, threshold_image, output_image, Middle_image;
15 
16 void Threshold_Image_Bar(int, void *);
17 
18 int main(int argc, char**argv)
19 {
20     input_image = imread("1.jpg");
21     if (input_image.data == NULL) {
22         return -1; cout << "can't open image.../";
23     }
24     imshow("Sourse Image", input_image);
25     blur(input_image, Middle_image,Size(3,3),Point(-1,-1),4);
26     imshow("Blur Image", Middle_image);
27     cvtColor(Middle_image, Middle_image,COLOR_RGB2GRAY);
28     imshow("Gray Image", Middle_image);
29     namedWindow("Threshold Image",1);
30     createTrackbar("阈值调整", "Threshold Image",&Threshold_Value,255,Threshold_Image_Bar);
31     Threshold_Image_Bar(0,0);
32     waitKey(0);
33     return 0;
34 }
35 
36 void Threshold_Image_Bar(int, void *)
37 {
38     threshold(Middle_image, threshold_image, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
39     //Canny(threshold_image,threshold_image, Threshold_Value, Threshold_Value*3);
40     //bitwise_not(threshold_image, threshold_image,Mat());
41     imshow("Threshold Image", threshold_image);
42     Mat kernel = getStructuringElement(MORPH_RECT,Size(threshold_image.cols/20,1),Point(-1,-1));
43     erode(threshold_image,threshold_image,kernel);
44     dilate(threshold_image, threshold_image,kernel);
45     imshow("Erode Image", threshold_image);
46     vector<Vec4i> tline;
47     HoughLinesP(threshold_image,tline,1,CV_PI/360,5,0,0);
48     Mat LineImage = Mat::zeros(threshold_image.size(), threshold_image.type());
49     for (size_t i = 0; i < tline.size(); i++)
50     {
51         line(input_image, Point(tline[i][0],tline[i][1]), Point(tline[i][2],tline[i][3]), Scalar(200, 55, 205),3,8,0);
52     }
53     imshow("LineImage", input_image);
54 }

 

参考:贾老师opencv系列

posted on 2017-04-25 20:34  影醉阏轩窗  阅读(913)  评论(0编辑  收藏  举报

导航

/* 线条鼠标集合 */ /* 鼠标点击求赞文字特效 */ //带头像评论