首先需要得到边缘的图像,FindContours是对边缘图像进行处理,边缘图像可以用数据点
Image<Gray, Byte> currentImage = new Image<Gray, byte>(@"F:\MyDesktop\1.jpg");
Image<Gray, Byte> res = new Image<Gray, byte>(currentImage.Width, currentImage.Height, new Gray(0));
VectorOfVectorOfPoint vvp = new VectorOfVectorOfPoint();
Image<Bgr, Byte> disp = new Image<Bgr, byte>(currentImage.Width, currentImage.Height);
Image<Bgr, Byte> edges = new Image<Bgr, byte>(currentImage.Width, currentImage.Height);
Mat b1 = new Mat();
CvInvoke.Canny(currentImage, edges, 100, 200);
CvInvoke.FindContours(edges, vvp, b1, RetrType.Ccomp, ChainApproxMethod.ChainApproxNone);
for (int i = 0; i < vvp.Size; i++)
{
CvInvoke.DrawContours(disp, vvp, i, new MCvScalar(255,255,255), 1);
}
for (int i = 0; i < vvp.Size; i++)
{
for (int j = 0; j < vvp[i].Size; j++)
{
res.Data[vvp[i][j].Y, vvp[i][j].X, 0] = 255;
}
}
CvInvoke.Imshow("res", res);
CvInvoke.Imshow("disp", disp);
有两种方法可以显示提取的轮廓
- 自带的drawContours
- VectorOfVectorOfPoint中存放的数据点
原图和显示的图像如下: