2016-09-14 93 views
2

欲检测图像(更准确地说其角部)上的显示。 我段在显示彩色图像和不显示颜色:检测显示角与Emgu

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 (RAN上segmentedImage,然后ApproxPolyDP FindContours,并提请发现轮廓的原始灰度图像)
我不能让它找到的轮廓更加精确...

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)); 
       } 
      } 
     } 
    } 
} 
+0

为什么不检测的轮廓在二值图像,做近似和检查长方形? – PSchn

+0

@PSchn参见编辑。 – pythonimus

+0

mhmm多数民众赞成在一个坏的结果。我会自己尝试,我不相信。那里出了点问题。 – PSchn

回答

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分的轮廓或传递了门槛提高近似度提高你的代码。只需围绕approxPolyDP包装一个for循环。你可以为你的近似值定义一个范围,并且如果你的对象与矩形的差别太大,就可以防止你的代码失败。

+0

不适用于我,请参阅编辑。 – pythonimus