【发布时间】:2016-09-14 07:00:06
【问题描述】:
我想检测图像上的显示(更准确地说是它的角)。 我将图像分割为显示颜色而不是显示颜色:
Image<Gray, byte> segmentedImage = greyImage.InRange(new Gray(180), new Gray(255));
然后我使用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);
现在角落里有白色像素,但我无法访问它们:
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 不是那么精确。当我尝试在分段图像上运行它时,我得到了这个:
(在分段图像上运行 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));
}
}
}
}
}
【问题讨论】:
-
为什么不检测二进制图像中的轮廓,进行近似并检查矩形形状?!
-
嗯,结果很糟糕。我会自己尝试,我不相信。那里出了点问题。