C#opencv 遍历图像中全部点 不在圆范围内的点变为玄色,在圆范围内的保持原色

[复制链接]
发表于 2025-10-14 10:43:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
C#opencv 遍历图像中全部点 不在圆范围内的点变为玄色,在圆范围内的保持原色
安装 Install-Package OpenCvSharp4
Install-Package OpenCvSharp4.Windows

平凡实现
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using OpenCvSharp; // 添加OpenCV引用
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         // 原有代码...
  10.         
  11.         // 添加圆形遮罩处理图像的示例
  12.         ProcessImageWithCircleMask("input.jpg", "output.jpg", 300, 300, 200);
  13.     }
  14.    
  15.     // 处理图像,将不在圆范围内的点变为黑色
  16.     static void ProcessImageWithCircleMask(string inputPath, string outputPath, int centerX, int centerY, int radius)
  17.     {
  18.         try
  19.         {
  20.             // 读取图像
  21.             using (Mat src = Cv2.ImRead(inputPath))
  22.             {
  23.                 if (src.Empty())
  24.                 {
  25.                     Console.WriteLine("无法读取图像!");
  26.                     return;
  27.                 }
  28.                
  29.                 // 创建一个与原图像大小相同的黑色图像
  30.                 using (Mat result = Mat.Zeros(src.Size(), src.Type()))
  31.                 {
  32.                     // 遍历图像中的每个像素
  33.                     for (int y = 0; y < src.Height; y++)
  34.                     {
  35.                         for (int x = 0; x < src.Width; x++)
  36.                         {
  37.                             // 计算点到圆心的距离
  38.                             double distance = Math.Sqrt(Math.Pow(x - centerX, 2) + Math.Pow(y - centerY, 2));
  39.                            
  40.                             // 如果点在圆内,保持原色;否则为黑色
  41.                             if (distance <= radius)
  42.                             {
  43.                                 // 获取原图像的像素值并设置到结果图像中
  44.                                 Vec3b color = src.Get<Vec3b>(y, x);
  45.                                 result.Set(y, x, color);
  46.                             }
  47.                         }
  48.                     }
  49.                     
  50.                     // 保存结果图像
  51.                     Cv2.ImWrite(outputPath, result);
  52.                     Console.WriteLine($"处理完成,结果已保存到 {outputPath}");
  53.                 }
  54.             }
  55.         }
  56.         catch (Exception ex)
  57.         {
  58.             Console.WriteLine($"处理图像时出错: {ex.Message}");
  59.         }
  60.     }
  61.     // 生成随机坐标点
  62.     static List<Point> GenerateRandomPoints(int count, int minX, int maxX, int minY, int maxY)
  63.     {
  64.         // 原有代码...
  65.     }
  66.    
  67.     // 其他原有方法...
  68. }
  69. // 原有类定义...
复制代码
如果你想要更高效的实现,可以使用OpenCV的内置函数:
  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace resistanceWelding
  8. {
  9.     class testOpencv
  10.     {
  11.         public static void ProcessImageWithCircleMaskEfficient(string inputPath, string outputPath, int centerX, int centerY, int radius)
  12.         {
  13.             try
  14.             {
  15.                 // 读取图像
  16.                 using (Mat src = Cv2.ImRead(inputPath))
  17.                 {
  18.                     if (src.Empty())
  19.                     {
  20.                         Console.WriteLine("无法读取图像!");
  21.                         return;
  22.                     }
  23.                     // 创建一个与原图像大小相同的黑色掩码
  24.                     using (Mat mask = Mat.Zeros(src.Size(), MatType.CV_8UC1))
  25.                     {
  26.                         // 在掩码上绘制白色圆形
  27.                         Cv2.Circle(mask, new Point(centerX, centerY), radius, Scalar.White, -1);
  28.                         // 创建结果图像
  29.                         using (Mat result = new Mat(src.Size(), src.Type(), Scalar.Black))
  30.                         {
  31.                             // 使用掩码将原图像复制到结果图像
  32.                             src.CopyTo(result, mask);
  33.                             // 保存结果图像
  34.                             Cv2.ImWrite(outputPath, result);
  35.                             Console.WriteLine($"处理完成,结果已保存到 {outputPath}");
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.             catch (Exception ex)
  41.             {
  42.                 Console.WriteLine($"处理图像时出错: {ex.Message}");
  43.             }
  44.         }
  45.     }
  46. }
复制代码
范例转换
必要安装   OpenCvSharp4.Extensions
  1. using Cognex.VisionPro;
  2. using Cognex.VisionPro.ImageFile;
  3. using OpenCvSharp;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace resistanceWelding
  10. {
  11.     class testOpencv
  12.     {
  13.         public static void ProcessImageWithCircleMaskEfficient(string inputPath, string outputPath, int centerX, int centerY, int radius)
  14.         {
  15.             try
  16.             {
  17.                 int dd = 2;
  18.                 Mat src;
  19.                 if(dd == 22)
  20.                 {
  21.                     // 读取图像
  22.                     src = Cv2.ImRead(inputPath);
  23.                 }
  24.                 //else if(dd == 1)
  25.                 //{
  26.                 //    CogImageFile cogImage = new CogImageFile();
  27.                 //    cogImage.Open(inputPath, CogImageFileModeConstants.Read);
  28.                   
  29.                 //}
  30.                 else
  31.                 {
  32.                     var originalBitmap = new System.Drawing.Bitmap(inputPath);
  33.                     src = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalBitmap);
  34.                  
  35.                 }
  36.    
  37.                 if (src.Empty())
  38.                 {
  39.                     Console.WriteLine("无法读取图像!");
  40.                     return;
  41.                 }
  42.                 // 创建一个与原图像大小相同的黑色掩码
  43.                 Mat mask = Mat.Zeros(src.Size(), MatType.CV_8UC1);
  44.                 // 在掩码上绘制白色圆形
  45.                 Cv2.Circle(mask, new Point(centerX, centerY), radius, Scalar.White, -1);
  46.                 // 创建结果图像
  47.                 Mat result = new Mat(src.Size(), src.Type(), Scalar.Black);
  48.                 // 使用掩码将原图像复制到结果图像
  49.                 src.CopyTo(result, mask);
  50.                 // 保存结果图像
  51.                 Cv2.ImWrite(outputPath, result);
  52.                 Console.WriteLine($"处理完成,结果已保存到 {outputPath}");
  53.             }
  54.             catch (Exception ex)
  55.             {
  56.                 Console.WriteLine($"处理图像时出错: {ex.Message}");
  57.             }
  58.         }
  59.     }
  60. }
复制代码


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;




回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表