使用Emgu检测显示角

时间:2016-09-14 07:00:07

标签: c# opencv emgucv

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

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

enter image description here

然后我用角落哈里斯寻找角落:

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。我怎样才能访问它们?如果我可以访问它们,我怎样才能在哈里斯图像中找到屏幕的四个角?是否有从点中找到透视变换矩形的函数?

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

EDIT2: 我不能让这个为我工作。即使你的代码,我得到完全相同的结果...... 这是我的完整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));
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

所以我承诺自己尝试过。在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点的轮廓或超过阈值。只需在aboutPolyDP周围换一个for循环。您可以为近似值定义范围,并在对象与矩形的区别太大时防止代码失败。