OpenCV4入门132:直线检测

索引地址:系列索引

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
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
Mat image = imread("Test.png");
Mat imgGray;
cvtColor(image, imgGray, COLOR_BGR2GRAY);

Ptr<LineSegmentDetector> detector = createLineSegmentDetector();

vector<Vec4f> lines;
detector->detect(imgGray,
lines);

Mat result(image.size(),
CV_8UC3,
Scalar(0, 0, 0));

// detector->drawSegments(result,
// lines);

// or the following

Scalar color(0,0,255);
int thickness = 2;
for(int i=0; i<lines.size(); i++)
{
line(result,
Point(lines.at(i)[0],
lines.at(i)[1]),
Point(lines.at(i)[2],
lines.at(i)[3]),
color,
thickness);
}

imshow("image", image);
imshow("result", result);

waitKey();

return 0;
}