lsd直线检测c++
时间: 2023-11-18 08:52:36 浏览: 81
LSD直线检测算法的MATLAB代码
5星 · 资源好评率100%
LSD直线检测是一种基于边缘检测的直线检测算法,它可以在图像中检测出直线的位置和方向。在C++中,可以使用OpenCV库中的LSD算法实现直线检测。以下是一个简单的示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = imread("test.jpg", IMREAD_GRAYSCALE);
if (img.empty())
{
cout << "Failed to load image!" << endl;
return -1;
}
vector<Vec4f> lines;
Ptr<LineSegmentDetector> ls = createLineSegmentDetector(LSD_REFINE_STD);
ls->detect(img, lines);
for (size_t i = 0; i < lines.size(); i++)
{
Vec4f line = lines[i];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
float theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[2]);
line[3] = round(line[3]);
line[0] = max(0.0f, min(line[0], (float)img.cols - 1));
line[1] = max(0.0f, min(line[1], (float)img.rows - 1));
line[2] = max(0.0f, min(line[2], (float)img.cols - 1));
line[3] = max(0.0f, min(line[3], (float)img.rows - 1));
line[2] -= line[0];
line[3] -= line[1];
theta = atan2(line[3], line[2]) * 180 / CV_PI;
if (theta < 0)
theta += 180;
line[2] += line[0];
line[3] += line[1];
line[0] = round(line[0]);
line[1] = round(line[1]);
line[2] = round(line[
阅读全文