private void button17_Click(object sender, EventArgs e)
{
// 圖像的矩
var op = new OpenFileDialog();
if (op.ShowDialog()== DialogResult.OK)
{
Mat scr = new Mat(op.FileName);
Mat dst = new Mat(op.FileName);
Mat edges = new Mat();
CvInvoke.Canny(dst, edges, 120, 180);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(edges, contours, null,
Emgu.CV.CvEnum.RetrType.External,
Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone);
// 輪廓查找
int ksize = contours.Size;
double[] m00 = new double[ksize];
double[] m10 = new double[ksize];
double[] m01 = new double[ksize];
Point[] Gravity = new Point[ksize];
MCvMoments[] hu = new MCvMoments[ksize];
for (int i = 0 ;i<ksize;i++)
{
VectorOfPoint contour = contours[i];
hu[i] = CvInvoke.Moments(contour, false);
m00[i] = hu[i].M00;
m10[i] = hu[i].M10;
m01[i] = hu[i].M01;
double X = m10[i] / m00[i]; //get center X
double Y = m01[i] / m00[i]; //get center y
Gravity[i] = new Point((int)X, (int)Y);
}
foreach(Point cent in Gravity)
{
CvInvoke.Circle(dst, cent, 2, new MCvScalar(0, 0, 255), 2);
}
imageBox1.Image = scr;
imageBox2.Image = dst;
}
}
留言列表