18 #include <mrpt/otherlibs/do_opencv_includes.h> 26 const cv::Mat& canny_image,
const std::vector<cv::Vec2f> lines,
27 std::vector<cv::Vec4i>& segments,
size_t threshold)
31 double cosTheta, sinTheta;
41 for (
size_t n(0);
n < lines.size(); ++
n)
48 theta = lines[
n][1] -
CV_PI;
56 if (rho == 0 && theta == 0)
64 if (fabs(theta) < 0.00001)
66 x = xF =
static_cast<int>(rho + 0.5);
68 yF = canny_image.rows - 1;
74 cosTheta = cos(theta);
75 sinTheta = sin(theta);
76 m = -cosTheta / sinTheta;
77 c = rho * (sinTheta - m * cosTheta);
83 if (
c < canny_image.rows)
87 y =
static_cast<int>(
c);
92 y = canny_image.rows - 1;
93 x =
static_cast<int>((
y -
c) / m);
99 x =
static_cast<int>(-
c / m);
103 cMax = m * (canny_image.cols - 1) +
c;
106 if (cMax < canny_image.rows)
109 xF = canny_image.cols - 1;
110 yF =
static_cast<int>(cMax);
115 yF = canny_image.rows - 1;
116 xF =
static_cast<int>((yF -
c) / m);
122 xF =
static_cast<int>(-
c / m);
130 bool onSegment =
false;
132 int memoryX = 0, memoryY = 0;
133 int xPrev = 0, yPrev = 0;
138 int dx1, dy1, dx2, dy2 = 0;
140 int longest, shortest;
169 if (longest <= shortest)
185 numerator = longest / 2;
188 for (
int i(0); i <= longest; ++i)
194 if (canny_image.at<
char>(
y,
x) == 0 || i == longest)
198 if (nbPixels >= threshold)
200 segments.emplace_back(memoryX, memoryY, xPrev, yPrev);
210 else if (canny_image.at<
char>(
y,
x) != 0)
226 numerator += shortest;
227 if (numerator >= longest)
229 numerator -= longest;
245 std::vector<std::array<int, 4>>& segments_out,
size_t threshold,
256 int lowThreshold = 150;
261 image, canny_img, lowThreshold, lowThreshold * ratio,
276 std::vector<cv::Vec2f> lines;
278 canny_img, lines, 1,
CV_PI / 180.0,
291 cv::Mat filteredCanny;
294 cv::dilate(canny_img, filteredCanny, cv::Mat());
302 std::vector<cv::Vec4i> segments;
306 for (
auto line =
begin(segments); line !=
end(segments); ++line)
307 if ((*line)[1] == (*line)[3])
309 if ((*line)[0] > (*line)[1])
311 cv::Vec4i((*line)[2], (*line)[3], (*line)[0], (*line)[1]);
313 else if ((*line)[1] < (*line)[3])
314 *line = cv::Vec4i((*line)[2], (*line)[3], (*line)[0], (*line)[1]);
317 segments_out.resize(segments.size());
318 for (
unsigned i = 0; i < segments.size(); i++)
319 for (
size_t k = 0; k < segments_out[i].size(); k++)
320 segments_out[i][k] = segments[i][k];
326 image.convertTo(image_lines, CV_8UC1, 1.0 / 2);
327 for (
auto line =
begin(segments); line !=
end(segments); ++line)
330 image_lines, cv::Point((*line)[0], (*line)[1]),
331 cv::Point((*line)[2], (*line)[3]),
cv::Scalar(255, 0, 255), 1);
333 image_lines, cv::Point((*line)[0], (*line)[1]), 3,
338 ((*line)[0] + (*line)[2]) / 2,
339 ((*line)[1] + (*line)[3]) / 2),
342 cv::imshow(
"lines", image_lines);
343 cv::moveWindow(
"lines", 20, 100 + 700);
Shallow copy: the copied object is a reference to the original one.
#define THROW_EXCEPTION(msg)
GLenum GLsizei GLenum GLenum const GLvoid * image
GLubyte GLubyte GLubyte GLubyte w
void asCvMat(cv::Mat &out_img, copy_type_t copy_type) const
Makes a shallow or deep copy of this image into the provided cv::Mat.
Classes for computer vision, detectors, features, etc.
const_iterator begin() const
void extractLines(const mrpt::img::CImage &image, std::vector< std::array< int, 4 >> &segments, size_t threshold, const bool display=false)
static void extractLines_CannyHough(const cv::Mat &canny_image, const std::vector< cv::Vec2f > lines, std::vector< cv::Vec4i > &segments, size_t threshold)
std::string std::string to_string(T v)
Just like std::to_string(), but with an overloaded version for std::string arguments.
double distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
A class for storing images as grayscale or RGB bitmaps.