当前位置: 首页 > article >正文

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

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;


http://www.kler.cn/a/588123.html

相关文章:

  • PostreSQL指南-内幕探索-学习笔记-01-数据库集簇的逻辑与物理结构
  • 【Linux】五种 IO 模型与非阻塞 IO
  • cursor使用
  • MyBatis相关的面试题
  • 【Qt】qrc机制介绍
  • 记一次小程序爬虫(反编译-自动化字体映射生成)
  • ctf-web: 简单java反序列化示例
  • FakeApp 技术浅析(四):卷积神经网络
  • 线程-进阶
  • C语言之共用体
  • ETL与ELT核心技术解析:如何选择最优数据集成方案
  • 树莓科技(成都)集团:如何铸就第五代产业园标杆
  • BUG日志:Maven项目启动报错(文件名或者文件扩展名过长)
  • 2.2[frontEnd]ESLint
  • android 通过action启动Activity拦截,Activity应用组件添加intent-filter priority(优先级)不生效
  • AF3 make_pseudo_beta函数解读
  • 【菜鸟飞】Conda安装部署与vscode的结合使用
  • 技术解析:基于AI+云计算的智能呼叫中心系统如何重构零售行业服务生态?
  • 数据结构与算法-图论-欧拉路径和欧拉回路(有向图和无向图,骑马修栅栏,单词游戏 play on words)详细代码注解
  • 实践 PyTorch 手写数字识别