You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Emgu CV,如何分离轮廓中检测到的目标?

分离轮廓中检测到的目标方法

要分离检测到的每个轮廓目标,你可以为每个轮廓生成独立掩码,再提取对应区域,以下是修改后的实现代码:

// 二值化处理(保留你的原有代码)
Image<Gray, byte> imgOutput = imgInput.Convert<Gray, byte>().ThresholdBinary(new Gray(100), new Gray(255));
Emgu.CV.Util.VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat m = new Mat();

CvInvoke.FindContours(imgOutput, contours, m, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

if (contours.Size > 0)
{
    // 遍历所有检测到的轮廓
    for (int i = 0; i < contours.Size; i++)
    {
        // 1. 创建当前轮廓的掩码
        Image<Gray, byte> mask = new Image<Gray, byte>(imgInput.Width, imgInput.Height, new Gray(0));
        // 填充当前轮廓为白色,实现掩码分离
        CvInvoke.DrawContours(mask, contours, i, new MCvScalar(255), -1);

        // 2. 获取轮廓的外接矩形,提取目标ROI
        Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
        Image<Bgr, byte> target = imgInput.Copy(rect);

        // 这里可以将target保存或显示,比如添加到控件列表
        // pictureBoxList[i].Image = target.Bitmap;

        // 可选:在原图像上标记每个目标(保留你的绘制逻辑)
        CvInvoke.DrawContours(imgInput, contours, i, new MCvScalar(0, 0, 255), 2);
    }
    pictureBox2.Image = imgInput.Bitmap;
}

关键步骤说明:

  • 创建独立掩码:通过全黑图像填充单个轮廓为白色,得到仅包含当前目标的掩码,可用于后续的图像分割或提取。
  • 提取ROI区域:利用BoundingRectangle获取目标的外接矩形,直接从原图像中截取该区域,得到独立的目标图像。
  • 遍历所有轮廓:替换原代码中仅处理单个轮廓的逻辑,确保每个检测到的目标都被分离处理。

内容的提问来源于stack exchange,提问作者Cary Grant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:05:36