程序运行结果
一、生成6个圆环
private void uiButton1_Click(object sender, EventArgs e)
{
Scalar s = new Scalar(155, 155, 155);//创建一个颜色对象
OpenCvSharp.Scalar scalar = new OpenCvSharp.Scalar(0, 0,0);
Mat mm = new Mat(1000, 2000, MatType.CV_8UC3, s);
for(int i=0;i<6;i++)
{ Cv2.Circle(mm, 200+i*300, 500, 50, scalar, 30);
Cv2.PutText(mm, (i+1).ToString(), new OpenCvSharp.Point(190 + i * 300, 430), 0, 1.25, new OpenCvSharp.Scalar(0, 0, 255), 2);
}
picShowOri.Image = mm.ToBitmap();
src_img = new Mat();
mm.CopyTo(src_img);
}
二、结构定义
- 结构定义及结构数组变量的申明;
- 保存检测圆的序号,圆心坐标,半径信息。
public struct CircleSegment0
{ public Point2f Center;
public float Radius;
} ;
CircleSegment0[] cs0 = new CircleSegment0[10];
//------
cs0[i].Center = cs[i].Center;
cs0[i].Radius = cs[i].Radius;
int x0 = (int)cs[i].Center.X;
int y0 = (int)cs[i].Center.Y;
//显示圆心坐标
Cv2.PutText(dst, i.ToString()+"(" + x0.ToString() + "," + y0.ToString() + ")", new OpenCvSharp.Point(x0-100, y0 + 200), 0, 1.25, new OpenCvSharp.Scalar(0, 0, 255), 2);
三、切割
- 选择感兴趣的区域进行切割,识别圆的序号有些不同,改进时需要排序。
private void uiButton3_Click(object sender, EventArgs e)
{
int k0= uiComboBox1.Text.ToInt();
int x0 = (int)cs0[k0].Center.X;
int y0= (int)cs0[k0].Center.Y;
int r0 = (int)cs0[k0].Radius;
OpenCvSharp.Rect roi = new OpenCvSharp.Rect(x0 - r0 - 50,y0 - r0 - 50, 2*(r0 + 50), 2 * (r0 + 50));
Mat ImageROI = new Mat(src_img, roi);//新建一个mat,把roi内的图像加载到里面去
pictureBox1.Image = ImageROI.ToBitmap();
}
网友评论