温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# Bitmap图像轮廓提取方法

发布时间:2024-07-16 10:44:04 来源:亿速云 阅读:106 作者:小樊 栏目:编程语言

在C#中,可以使用OpenCV库来进行图像轮廓提取。以下是一个简单的示例代码,演示如何使用OpenCV提取图像的轮廓:

using System;
using System.Drawing;
using OpenCvSharp;

class Program
{
    static void Main()
    {
        // 读取图像
        Mat image = Cv2.ImRead("image.jpg", ImreadModes.GrayScale);

        // 阈值化处理
        Mat thresholded = new Mat();
        Cv2.Threshold(image, thresholded, 127, 255, ThresholdTypes.Binary);

        // 寻找图像轮廓
        Point[][] contours;
        HierarchyIndex[] hierarchy;
        Cv2.FindContours(thresholded, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);

        // 绘制轮廓
        Mat contourImage = new Mat(image.Size(), MatType.CV_8UC3, Scalar.White);
        Cv2.DrawContours(contourImage, contours, -1, Scalar.Red, thickness: 2);

        // 显示图像
        Cv2.ImShow("Contours", contourImage);
        Cv2.WaitKey(0);
    }
}

在这个示例中,我们首先读取一张灰度图像,然后对图像进行阈值化处理,找到图像的轮廓并将其绘制在一张新的图像上。最后显示这张包含轮廓的图像。

你需要在项目中引用OpenCvSharp库,可以通过NuGet包管理器来安装。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI