【EmguCV】EmguCV各种调用

本文介绍了使用EmguCV进行图像处理的方法,包括轮廓检测、斑点检测、圆形判断及霍夫直线检测等核心功能,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后续将不定期补充在使用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);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值