gpt4 book ai didi

c# - 如何在 OpenCV 和 C# 中进行霍夫变换?

转载 作者:行者123 更新时间:2023-11-28 00:02:49 27 4
gpt4 key购买 nike

关于这个Opencv Tutorial ,以下 C++ 代码片段:

vector<Vec4i> lines;
// Find hough lines
HoughLinesP(edges, lines, 1, CV_PI / 180, 100, 100, 10);

// Prepare blank mat with same sizes as image
Mat Blank(image.rows, image.cols, CV_8UC3, Scalar(0, 0, 0));

// Draw lines into image and Blank images
for (size_t i = 0; i < lines.size(); i++)
{
Vec4i l = lines[i];

line(image, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 0), 2, CV_AA);

line(Blank, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255, 255, 255), 2, CV_AA);

}

被翻译成 C# 是这样的:

private void openFileDialogButton_Click(object sender, EventArgs e)
{
try
{
const string filename = @"E:\___MSc in Computer Systems & Network\EMSC1,2,3\lena.png";

Mat image = Cv2.ImRead(filename, LoadMode.GrayScale);

Mat edges = new Mat();

Cv2.Canny(image, edges, 95, 100);

Cv2.ImWrite("edges.jpg", edges);

Mat dx = new Mat();
Mat dy = new Mat();

Cv2.Sobel(edges, dx, MatType.CV_32F, 1, 0);
Cv2.Sobel(edges, dy, MatType.CV_32F, 0, 1);

Cv2.ImWrite("dx.jpg", dx);
Cv2.ImWrite("dy.jpg", dy);

Mat linesssssss = new Mat();

Cv.HoughLines2((CvArr)linesssssss.ToIplImage(),
(CvMat)edges,
HoughLinesMethod.Standard,
1,
Math.PI / 180,
100, 100, 10);
//Cv2.HoughLinesP(edges, lines, 1, Math.PI / 180, 100, 100, 10);

List<Vec4i> lines = IntPtrToList(linesssssss.Data);

Mat Blank = new Mat(image.Rows, image.Cols, MatType.CV_8UC3, new Scalar(0, 0, 0));

for (int i = 0; i < lines.Count; i++)
{
Vec4i l = lines[i];

Cv2.Line(image, new OpenCvSharp.CPlusPlus.Point(l[0], l[1]), new OpenCvSharp.CPlusPlus.Point(l[2], l[3]), new Scalar(0, 0, 0), 2, Cv.AA);

Cv2.Line(Blank, new OpenCvSharp.CPlusPlus.Point(l[0], l[1]), new OpenCvSharp.CPlusPlus.Point(l[2], l[3]), new Scalar(255, 255, 255), 2, Cv.AA);

}

//Cv2.ImWrite("houg.jpg", image);
//Cv2.ImShow("Edges", image);
//Cv2.ImWrite("houg2.jpg", Blank);
//Cv2.ImShow("Edges Structure", Blank);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

不幸的是,它似乎不起作用。它给出了以下异常:

Nonpositive cols or rows

这是文章的原始图片:

enter image description here

最佳答案

基于 HoughLinesP 的 Github sample code:

    static void Main(string[] args)
{
string filename = "Alliance.jpg";

Mat imageIn = Cv2.ImRead(filename, ImreadModes.GrayScale).Resize(new Size(800, 600));
Mat edges = new Mat();

Cv2.Canny(imageIn, edges, 95, 100);

//HoughLinesP
LineSegmentPoint[] segHoughP = Cv2.HoughLinesP(edges, 1, Math.PI / 180, 100, 100, 10);

Mat imageOutP = imageIn.EmptyClone();

foreach (LineSegmentPoint s in segHoughP)
imageOutP.Line(s.P1, s.P2, Scalar.White, 1, LineTypes.AntiAlias, 0);

using (new Window("Edges", WindowMode.AutoSize, edges))
using (new Window("HoughLinesP", WindowMode.AutoSize, imageOutP))
{
Window.WaitKey(0);
}
}

enter image description here enter image description here

关于c# - 如何在 OpenCV 和 C# 中进行霍夫变换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com