后续将不定期补充在使用EmguCV过程中的一些笔记,如何调用api实现相应功能。
1、查找对应的轮廓
IntPtr Dyncontour = new IntPtr();//存放检测到的图像块的首地址
IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);//开辟内存区域
int m = 88;
int n = CvInvoke.cvFindContours(image, Dynstorage, ref Dyncontour, m, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, new Point(1, 1));
Seq<Point> header = new Seq<Point>(Dyncontour, null);
Seq<Point> contourTemp = header;
MemStorage stor = new MemStorage();
while (contourTemp != null)// 获取符合条件的轮廓
{
image.Draw(contourTemp.BoundingRectangle, new Gray(255), 2);
contourTemp = contourTemp.HNext;
}
2、斑点检测
Emgu.CV.Cvb.CvBlobs resultingImgBlobs = new Emgu.CV.Cvb.CvBlobs();
Emgu.CV.Cvb.CvBlobDetector bDetect = new Emgu.CV.Cvb.CvBlobDetector();
uint numWebcamBlobsFound = bDetect.Detect(img, resultingImgBlobs);
foreach (Emgu.CV.Cvb.CvBlob targetBlob in resultingImgBlobs.Values)
{
img.Draw(...);
}
3、判断是否为圆
public int checkIfCircle(Emgu.CV.Cvb.CvBlob blob, double ratio)
{
int ret = 0;
IntPtr Dyncontour = new IntPtr();
MemStorage Dynstorage = new MemStorage();
Dyncontour = blob.GetContour(Dynstorage);
//通过拟合圆方法判断检测到的斑点是否为圆。
PointF pf = new PointF(0, 0);
float radius = 0;
CvInvoke.cvMinEnclosingCircle(Dyncontour, out pf, out radius);
double cArea = 3.14 * radius * radius;
if (cArea - blob.Area < cArea * ratio) // 如果拟合圆的面积跟斑点面积相差不大
{// 圆
ret = 1;
}
else
{
ret = 0;
}
Dynstorage.Dispose();
return ret;
}
houghline detection
Image<Gray,byte> imgGray = img.Convert<Gray,byte>();
MemStorage storage = new MemStorage();
IntPtr intPtrHoughLines = CvInvoke.cvHoughLines2(imgGray, storage, HOUGH_TYPE.CV_HOUGH_PROBABILISTIC, 1, Math.PI / 180, 50, 100, 10);
Seq<LineSegment2D> lineSegment = new Seq<LineSegment2D>(intPtrHoughLines, storage);
Seq<LineSegment2D> lineSegmentTemp = lineSegment;
//画出检测结果
unsafe
{
for (int i = 0; i < lineSegment.Total; ++i)
{
Point* point = (Point*)CvInvoke.cvGetSeqElem(intPtrHoughLines, i);
CvInvoke.cvLine(img, point[0], point[1], new MCvScalar(255, 0, 0), 2, LINE_TYPE.CV_AA, 0);//在原图像中画线
}
}
saveImg(TAG, img);