gpt4 book ai didi

c# - 使用 Emgu 检测显示角

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:42 24 4
gpt4 key购买 nike

我想检测图像上的显示(更准确地说是它的角)。我以显示颜色分割图像而不显示颜色:

Image<Gray, byte> segmentedImage = greyImage.InRange(new Gray(180), new Gray(255));

enter image description here

然后我使用 corner Harris 找到角点:

Emgu.CV.Image<Emgu.CV.Structure.Gray, Byte> harrisImage = new Image<Emgu.CV.Structure.Gray, Byte>(greyImage.Size);
CvInvoke.CornerHarris(segmentedImage, harrisImage, 2);
CvInvoke.Normalize(harrisImage, harrisImage, 0, 255, NormType.MinMax, DepthType.Cv32F);

enter image description here

角落里现在有白色像素,但我无法访问它们:

for (int j = 0; j < harrisImage.Rows; j++)
{
for (int i = 0; i < harrisImage.Cols; i++)
{
Console.WriteLine(harrisImage[j, i].Intensity);
}
}

它只写 0。我怎样才能访问它们?如果我可以访问它们,我怎样才能在哈里斯图像中找到屏幕的 4 个角?是否有从点中找到透视变换矩形的函数?

编辑:
在 OpenCV IRC 上,他们说 FindContours 不是那么精确。当我尝试在 segmentedImage 上运行它时,我得到了这个: enter image description here(在 segmentedImage 上运行 FindContours,然后运行 ​​ApproxPolyDP 并在原始灰度图像上绘制找到的轮廓)
我无法找到更精确的轮廓...

编辑2:我无法让它为我工作。即使使用您的代码,我也会得到完全相同的结果...这是我的完整 Emgu 代码:

Emgu.CV.Image<Emgu.CV.Structure.Gray, Byte> imageFrameGrey = new Image<Emgu.CV.Structure.Gray, Byte>(bitmap);
Image<Gray, byte> segmentedImage = imageFrameGrey.InRange(new Gray(180), new Gray(255));
// get rid of small objects
int morph_size = 2;
Mat element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new System.Drawing.Size(2 * morph_size + 1, 2 * morph_size + 1), new System.Drawing.Point(morph_size, morph_size));
CvInvoke.MorphologyEx(segmentedImage, segmentedImage, Emgu.CV.CvEnum.MorphOp.Open, element, new System.Drawing.Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar());

// Find edges that form rectangles
List<RotatedRect> boxList = new List<RotatedRect>();
using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
{
CvInvoke.FindContours(segmentedImage, contours, null, Emgu.CV.CvEnum.RetrType.External, ChainApproxMethod.ChainApproxSimple);
int count = contours.Size;
for (int i = 0; i < count; i++)
{
using (VectorOfPoint contour = contours[i])
using (VectorOfPoint approxContour = new VectorOfPoint())
{
CvInvoke.ApproxPolyDP(contour, approxContour, CvInvoke.ArcLength(contour, true) * 0.01, true);
if (CvInvoke.ContourArea(approxContour, false) > 10000)
{
if (approxContour.Size == 4)
{
bool isRectangle = true;
System.Drawing.Point[] pts = approxContour.ToArray();
LineSegment2D[] edges = Emgu.CV.PointCollection.PolyLine(pts, true);
for (int j = 0; j < edges.Length; j++)
{
double angle = Math.Abs(edges[(j + 1) % edges.Length].GetExteriorAngleDegree(edges[j]));
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
}

if (isRectangle)
boxList.Add(CvInvoke.MinAreaRect(approxContour));
}
}
}
}
}

最佳答案

因此,正如 promise 的那样,我自己尝试了。在 C++ 中,但您应该很容易地采用 Emgu。首先,我用开口去除了分割图像中的小物体:

int morph_elem = CV_SHAPE_RECT;
int morph_size = 2;
Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));
// Apply the opening
morphologyEx(segmentedImage, segmentedImage_open, CV_MOP_OPEN, element);

然后检测所有轮廓并取大的轮廓并检查矩形形状:

vector< vector<Point>> contours;
findContours(segmentedImage_open, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

for each (vector<Point> var in contours)
{
double area = contourArea(var);
if (area> 30000)
{
vector<Point> approx;
approxPolyDP(var, approx, 0.01*arcLength(var, true), true);

if (4 == approx.size()) //rectangular shape
{
// do something
}
}
}

这是红色轮廓和绿色近似曲线的结果:

enter image description here

编辑:

您可以通过增加近似因子来改进您的代码,直到您获得具有 4 个点的轮廓或您通过阈值。只需围绕 approxPolyDP 包装一个 for 循环。您可以为您的近似值定义一个范围,并防止您的代码在您的对象与矩形差异太大时失败。

关于c# - 使用 Emgu 检测显示角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39484274/

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