C#opencv 遍历图像中所有点 不在圆范围内的点变为黑色,在圆范围内的保持原色

发布于:2025-03-17 ⋅ 阅读:(20) ⋅ 点赞:(0)

C#opencv 遍历图像中所有点 不在圆范围内的点变为黑色,在圆范围内的保持原色

安装 Install-Package OpenCvSharp4
Install-Package OpenCvSharp4.Windows

普通实现

using System;
using System.Collections.Generic;
using System.Linq;
using OpenCvSharp; // 添加OpenCV引用

class Program
{
    static void Main(string[] args)
    {
        // 原有代码...
        
        // 添加圆形遮罩处理图像的示例
        ProcessImageWithCircleMask("input.jpg", "output.jpg", 300, 300, 200);
    }
    
    // 处理图像,将不在圆范围内的点变为黑色
    static void ProcessImageWithCircleMask(string inputPath, string outputPath, int centerX, int centerY, int radius)
    {
        try
        {
            // 读取图像
            using (Mat src = Cv2.ImRead(inputPath))
            {
                if (src.Empty())
                {
                    Console.WriteLine("无法读取图像!");
                    return;
                }
                
                // 创建一个与原图像大小相同的黑色图像
                using (Mat result = Mat.Zeros(src.Size(), src.Type()))
                {
                    // 遍历图像中的每个像素
                    for (int y = 0; y < src.Height; y++)
                    {
                        for (int x = 0; x < src.Width; x++)
                        {
                            // 计算点到圆心的距离
                            double distance = Math.Sqrt(Math.Pow(x - centerX, 2) + Math.Pow(y - centerY, 2));
                            
                            // 如果点在圆内,保持原色;否则为黑色
                            if (distance <= radius)
                            {
                                // 获取原图像的像素值并设置到结果图像中
                                Vec3b color = src.Get<Vec3b>(y, x);
                                result.Set(y, x, color);
                            }
                        }
                    }
                    
                    // 保存结果图像
                    Cv2.ImWrite(outputPath, result);
                    Console.WriteLine($"处理完成,结果已保存到 {outputPath}");
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"处理图像时出错: {ex.Message}");
        }
    }

    // 生成随机坐标点
    static List<Point> GenerateRandomPoints(int count, int minX, int maxX, int minY, int maxY)
    {
        // 原有代码...
    }
    
    // 其他原有方法...
}

// 原有类定义...

如果你想要更高效的实现,可以使用OpenCV的内置函数:

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace resistanceWelding
{
    class testOpencv
    {
        public static void ProcessImageWithCircleMaskEfficient(string inputPath, string outputPath, int centerX, int centerY, int radius)
        {
            try
            {
                // 读取图像
                using (Mat src = Cv2.ImRead(inputPath))
                {
                    if (src.Empty())
                    {
                        Console.WriteLine("无法读取图像!");
                        return;
                    }

                    // 创建一个与原图像大小相同的黑色掩码
                    using (Mat mask = Mat.Zeros(src.Size(), MatType.CV_8UC1))
                    {
                        // 在掩码上绘制白色圆形
                        Cv2.Circle(mask, new Point(centerX, centerY), radius, Scalar.White, -1);

                        // 创建结果图像
                        using (Mat result = new Mat(src.Size(), src.Type(), Scalar.Black))
                        {
                            // 使用掩码将原图像复制到结果图像
                            src.CopyTo(result, mask);

                            // 保存结果图像
                            Cv2.ImWrite(outputPath, result);
                            Console.WriteLine($"处理完成,结果已保存到 {outputPath}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"处理图像时出错: {ex.Message}");
            }
        }



    }
}

类型转换

需要安装   OpenCvSharp4.Extensions

using Cognex.VisionPro;
using Cognex.VisionPro.ImageFile;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace resistanceWelding
{
    class testOpencv
    {
        public static void ProcessImageWithCircleMaskEfficient(string inputPath, string outputPath, int centerX, int centerY, int radius)
        {
            try
            {
                int dd = 2;
                Mat src;
                if(dd == 22)
                {
                    // 读取图像
                    src = Cv2.ImRead(inputPath);
                }
                //else if(dd == 1)
                //{
                //    CogImageFile cogImage = new CogImageFile();
                //    cogImage.Open(inputPath, CogImageFileModeConstants.Read);
                  

                //}
                else
                {
                    var originalBitmap = new System.Drawing.Bitmap(inputPath);
                    src = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalBitmap);

                 
                }
   
                if (src.Empty())
                {
                    Console.WriteLine("无法读取图像!");
                    return;
                }

                // 创建一个与原图像大小相同的黑色掩码
                Mat mask = Mat.Zeros(src.Size(), MatType.CV_8UC1);

                // 在掩码上绘制白色圆形
                Cv2.Circle(mask, new Point(centerX, centerY), radius, Scalar.White, -1);

                // 创建结果图像
                Mat result = new Mat(src.Size(), src.Type(), Scalar.Black);

                // 使用掩码将原图像复制到结果图像
                src.CopyTo(result, mask);

                // 保存结果图像
                Cv2.ImWrite(outputPath, result);
                Console.WriteLine($"处理完成,结果已保存到 {outputPath}");



            }
            catch (Exception ex)
            {
                Console.WriteLine($"处理图像时出错: {ex.Message}");
            }
        }



    }
}

C# 图像之间转换代码_c# bitmap.palette-CSDN博客

//转为 bitmap方法一:
Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
process_pictureBox.Image = map;

//转为 bitmap方法二:

Bitmap map = new Bitmap(mat.ToMemoryStream());
process_pictureBox.Image = map;


//Image img 转为Mat
Bitmap bitmap = new Bitmap(img);//Image img
OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);//用
 
//bitmap转换为mat
C#中Bitmap 与 Image 之间的转换
Image img = pictureBox1.Image;
Bitmap map = new Bitmap(img);
 
//而Bitmap直接可以赋值 给 Image 对象
 
Image img = Bitmap;