2017-08-10 53 views
1

我在EMGU中提取了以下一段代码以提取连接组件: Mat connected_array = new Mat();在EMGU中提取连接组件的质心

  Mat stats = new Mat(); 
      Mat centroids = new Mat(); 
      Mat ImgMat = new Mat(); 
      CvInvoke.ConnectedComponentsWithStats(ImgThresh, connected_array, stats, centroids, LineType.EightConnected,DepthType.Cv32S); 

我找不到任何方法来访问质心。

回答

1

EMGU将大部分数组包装到Mat对象中,然后需要将其转换为数组以访问其内容(使用mat.CopyTo(array))。这不是简单的从文档 - 我不得不使用跟踪误差&找出它是如何工作:

Mat labels = new Mat(); 
Mat stats = new Mat(); 
Mat centroids = new Mat(); 
MCvPoint2D64f[] centroidPoints; 
double x, y; 
int n; 

n = CvInvoke.ConnectedComponentsWithStats(image, labels, stats, centroids, LineType.EightConnected, DepthType.Cv32S); 

centroidPoints = new MCvPoint2D64f[n]; 
centroids.CopyTo(centroidPoints); 

foreach (MCvPoint2D64f point in centroidPoints) 
{ 
    x = point.X; 
    y = point.Y; 
} 

另一种常见的方法是使用轮廓,类似的功能,它EMGU还提供。我已经使用它来获得更好的性能。我也包括一个例子:

VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); 
MCvMoments moments; 
double area; 
MCvPoint2D64f center; 
int n; 

CvInvoke.FindContours(image, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); 
n = contours.Size; 

for (int i = 0; i < n; i++) 
{ 
    area = CvInvoke.ContourArea(contours[i], false); 
    moments = CvInvoke.Moments(contours[i]); 
    center = moments.GravityCenter; 
}