forked from Alaa-Fouad22/Computer-Vision-Edge_Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhoughlinedetector.cpp
More file actions
136 lines (119 loc) · 4.99 KB
/
Copy pathhoughlinedetector.cpp
File metadata and controls
136 lines (119 loc) · 4.99 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "HoughLineDetector.h"
#include <cmath>
HoughLineDetector::HoughLineDetector() {}
std::vector<cv::Vec2f> HoughLineDetector::detectLines(const cv::Mat& edges, int threshold) {
int width = edges.cols;
int height = edges.rows;
int max_rho = std::ceil(std::sqrt(width * width + height * height));
int num_thetas = 180;
cv::Mat accumulator = cv::Mat::zeros(2 * max_rho + 1, num_thetas, CV_32S);
std::vector<double> sin_t(num_thetas), cos_t(num_thetas);
for(int t = 0; t < num_thetas; t++) {
double rad = t * CV_PI / 180.0;
sin_t[t] = std::sin(rad);
cos_t[t] = std::cos(rad);
}
// 1. Voting Process
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
if(edges.at<uchar>(y, x) == 255) {
for(int t = 0; t < num_thetas; t++) {
int rho = std::round(x * cos_t[t] + y * sin_t[t]);
accumulator.at<int>(rho + max_rho, t)++;
}
}
}
}
// 2. إيجاد القمم (Peaks)
std::vector<cv::Vec2f> lines;
for(int r = 0; r < accumulator.rows; r++) {
for(int t = 0; t < accumulator.cols; t++) {
if(accumulator.at<int>(r, t) >= threshold) {
bool isMax = true;
for(int i = -1; i <= 1; i++) {
for(int j = -1; j <= 1; j++) {
int nr = r + i, nt = t + j;
if(nr >= 0 && nr < accumulator.rows && nt >= 0 && nt < accumulator.cols) {
if(accumulator.at<int>(nr, nt) > accumulator.at<int>(r, t)) {
isMax = false;
}
}
}
}
if(isMax) {
lines.push_back(cv::Vec2f(r - max_rho, t * CV_PI / 180.0));
}
}
}
}
return lines;
}
cv::Mat HoughLineDetector::drawLines(const cv::Mat& image, const cv::Mat& edges, const std::vector<cv::Vec2f>& lines) {
cv::Mat result;
if (image.channels() == 1) cv::cvtColor(image, result, cv::COLOR_GRAY2BGR);
else result = image.clone();
// قللنا الفراغ المسموح بيه عشان يقف أسرع عند الزوايا
int maxGap = 5;
int minLength = 20;
for(size_t i = 0; i < lines.size(); i++) {
float rho = lines[i][0], theta = lines[i][1];
double a = cos(theta), b = sin(theta);
double x0 = a * rho, y0 = b * rho;
cv::Point pt1(cvRound(x0 + 3000 * (-b)), cvRound(y0 + 3000 * (a)));
cv::Point pt2(cvRound(x0 - 3000 * (-b)), cvRound(y0 - 3000 * (a)));
cv::LineIterator it(edges, pt1, pt2, 8);
bool inSegment = false;
cv::Point segStart(-1, -1), segEnd(-1, -1);
int currentGap = 0;
for(int j = 0; j < it.count; j++, ++it) {
cv::Point p = it.pos();
if(p.x >= 0 && p.x < edges.cols && p.y >= 0 && p.y < edges.rows) {
// خلينا البحث Strict أكتر: بنشوف البيكسل نفسه أو اللي لازق فيه مباشرة بدون حواف واسعة
bool isEdge = false;
if(edges.at<uchar>(p.y, p.x) == 255) {
isEdge = true;
} else {
// لو مش في البيكسل نفسه، ندور في 4 اتجاهات بس مش 8 عشان نقلل التداخل في الزوايا
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
for(int k = 0; k < 4; k++) {
int ny = p.y + dy[k];
int nx = p.x + dx[k];
if(ny >= 0 && ny < edges.rows && nx >= 0 && nx < edges.cols) {
if(edges.at<uchar>(ny, nx) == 255) {
isEdge = true;
break;
}
}
}
}
if(isEdge) {
if(!inSegment) {
inSegment = true;
segStart = p;
}
segEnd = p;
currentGap = 0;
} else {
if(inSegment) {
currentGap++;
if(currentGap > maxGap) {
double len = cv::norm(segStart - segEnd);
if(len >= minLength) {
cv::line(result, segStart, segEnd, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
}
inSegment = false;
}
}
}
}
}
if(inSegment) {
double len = cv::norm(segStart - segEnd);
if(len >= minLength) {
cv::line(result, segStart, segEnd, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
}
}
}
return result;
}