using OpenCvSharp;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
Mat img0 = Cv2.ImRead("../../../images/home.jpg");
Mat img = new Mat();
Cv2.CvtColor(img0, img, ColorConversionCodes.BGR2GRAY);
Cv2.ImShow("pokerQ", img0);
// Create default parametrization LSD
LineSegmentDetector lsd = LineSegmentDetector.Create(LineSegmentDetectorModes.RefineNone);
lsd.Detect(img, out Vec4f[] lines, out double[] width, out double[] prec, out double[] nfa);
for (int i = 0; i < lines.Length; i++)
{
int x0 = (int)lines[i].Item0;
int y0 = (int)lines[i].Item1;
int x1 = (int)lines[i].Item2;
int y1 = (int)lines[i].Item3;
Cv2.Line(img0, new Point(x0, y0), new Point(x1, y1), new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);
Cv2.ImShow("LSD", img0);
Cv2.WaitKey(10);
}
// 绘图线检测结果
Mat mats = new Mat { };
lsd.Detect(img, mats);
lsd.DrawSegments(img, mats);
Cv2.ImShow("LSD", img);
Cv2.WaitKey(0);
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img0 = cv::imread("../images/home.jpg");
Mat img;
cv::cvtColor(img0, img, cv::COLOR_BGR2GRAY);
cv::imshow("pokerQ", img0);
// Create default parametrization LSD
Ptr<LineSegmentDetector> lsd = createLineSegmentDetector(LSD_REFINE_NONE);
vector<Vec4f> lines;
lsd->detect(img, lines);
for (int i = 0; i < lines.size(); i++)
{
int x0 = lines[i][0];
int y0 = lines[i][1];
int x1 = lines[i][2];
int y1 = lines[i][3];
cv::line(img0, Point(x0, y0), Point(x1, y1), Scalar(0, 255, 0), 2, cv::LINE_AA);
cv::imshow("LSD", img0);
cv::waitKey(30);
}
// 绘图线检测结果
lsd->drawSegments(img, lines);
cv::imshow("LSD", img);
cv::waitKey(0);
}
Python版本代码如下:
import cv2
# Read gray image
img0 = cv2.imread("../images/home.jpg")
img = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
cv2.imshow('pokerQ', img0)
# Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)
# Detect lines in the image
dlines = lsd.detect(img) # TODO 返回什么?
lines = lsd.detect(img)[0] # Position 0 of the returned tuple are the detected lines
# Draw detected lines in the image
drawn_img = lsd.drawSegments(img, lines)
for dline in dlines[0]:
x0 = int(round(dline[0][0]))
y0 = int(round(dline[0][1]))
x1 = int(round(dline[0][2]))
y1 = int(round(dline[0][3]))
cv2.line(img0, (x0, y0), (x1, y1), (0, 255, 0), 2, cv2.LINE_AA)
cv2.imshow("LSD", img0)
cv2.waitKey(10)
# Show image
cv2.imshow("LSD", drawn_img)
# cv2.imwrite("LSD.jpg", img0)
cv2.waitKey(0)
cv2.destroyAllWindows()
using OpenCvSharp;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
Mat img0 = Cv2.ImRead("../../../images/home.jpg");
Mat img = new Mat();
Cv2.CvtColor(img0, img, ColorConversionCodes.BGR2GRAY);
Cv2.ImShow("pokerQ", img0);
// Create default parametrization LSD
LineSegmentDetector lsd = LineSegmentDetector.Create(LineSegmentDetectorModes.RefineNone);
lsd.Detect(img, out Vec4f[] lines, out double[] width, out double[] prec, out double[] nfa);
for (int i = 0; i < lines.Length; i++)
{
int x0 = (int)lines[i].Item0;
int y0 = (int)lines[i].Item1;
int x1 = (int)lines[i].Item2;
int y1 = (int)lines[i].Item3;
Cv2.Line(img0, new Point(x0, y0), new Point(x1, y1), new Scalar(0, 255, 0), 2, LineTypes.AntiAlias);
Cv2.ImShow("LSD", img0);
Cv2.WaitKey(10);
}
// 绘图线检测结果
Mat mats = new Mat { };
lsd.Detect(img, mats);
lsd.DrawSegments(img, mats);
Cv2.ImShow("LSD", img);
Cv2.WaitKey(0);
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img0 = cv::imread("../images/home.jpg");
Mat img;
cv::cvtColor(img0, img, cv::COLOR_BGR2GRAY);
cv::imshow("pokerQ", img0);
// Create default parametrization LSD
Ptr<LineSegmentDetector> lsd = createLineSegmentDetector(LSD_REFINE_NONE);
vector<Vec4f> lines;
lsd->detect(img, lines);
for (int i = 0; i < lines.size(); i++)
{
int x0 = lines[i][0];
int y0 = lines[i][1];
int x1 = lines[i][2];
int y1 = lines[i][3];
cv::line(img0, Point(x0, y0), Point(x1, y1), Scalar(0, 255, 0), 2, cv::LINE_AA);
cv::imshow("LSD", img0);
cv::waitKey(30);
}
// 绘图线检测结果
lsd->drawSegments(img, lines);
cv::imshow("LSD", img);
cv::waitKey(0);
}
Python版本代码如下:
import cv2
# Read gray image
img0 = cv2.imread("../images/home.jpg")
img = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)
cv2.imshow('pokerQ', img0)
# Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)
# Detect lines in the image
dlines = lsd.detect(img) # TODO 返回什么?
lines = lsd.detect(img)[0] # Position 0 of the returned tuple are the detected lines
# Draw detected lines in the image
drawn_img = lsd.drawSegments(img, lines)
for dline in dlines[0]:
x0 = int(round(dline[0][0]))
y0 = int(round(dline[0][1]))
x1 = int(round(dline[0][2]))
y1 = int(round(dline[0][3]))
cv2.line(img0, (x0, y0), (x1, y1), (0, 255, 0), 2, cv2.LINE_AA)
cv2.imshow("LSD", img0)
cv2.waitKey(10)
# Show image
cv2.imshow("LSD", drawn_img)
# cv2.imwrite("LSD.jpg", img0)
cv2.waitKey(0)
cv2.destroyAllWindows()