C# OpenCvSharp 颜色反转

目录

效果

灰度图

黑白色反转

彩色反转

项目

代码

下载


效果

灰度图

黑白色反转

彩色反转

项目

代码

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace OpenCvSharp_颜色反转
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        Bitmap bmp;
        String imgPath = "";

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            imgPath = ofd.FileName;
            bmp = new Bitmap(imgPath);
            pictureBox1.Image = bmp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (imgPath == "")
            {
                return;
            }
            Mat mat = new Mat(imgPath);
            Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
            Mat dst = new Mat(mat.Height, mat.Width, mat.Type(), Scalar.White);

            byte grayPixel = 0;
            for (int r = 0; r < dst.Rows; r++)
            {
                for (int c = 0; c < dst.Cols; c++)
                {
                    grayPixel = mat.At<byte>(r, c);
                    dst.Set<byte>(r, c, (byte)(255 - grayPixel));
                }
            }
            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }
            pictureBox2.Image = BitmapConverter.ToBitmap(dst);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (imgPath == "")
            {
                return;
            }
            Mat mat = new Mat(imgPath);
            Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2GRAY);
            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }
            pictureBox2.Image = BitmapConverter.ToBitmap(mat);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (imgPath == "")
            {
                return;
            }
            Mat mat = new Mat(imgPath);
            Mat dst = new Mat(mat.Height, mat.Width, mat.Type(), Scalar.White);
            Vec3b vec3B;
            for (int r = 0; r < dst.Rows; r++)
            {
                for (int c = 0; c < dst.Cols; c++)
                {
                    vec3B = mat.At<Vec3b>(r, c);

                    vec3B.Item0 = (byte)(255 - vec3B.Item0);
                    vec3B.Item1 = (byte)(255 - vec3B.Item1);
                    vec3B.Item2 = (byte)(255 - vec3B.Item2);

                    dst.Set<Vec3b>(r, c, vec3B);
                }
            }
            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }
            pictureBox2.Image = BitmapConverter.ToBitmap(dst);
        }
    }
}

下载

Demo下载

相关推荐
Wang's Blog5 小时前
Java 项目实战: 交友项目:登录链路回顾与今日任务地图
java·开发语言·状态模式·交友
砚底藏山河6 小时前
python量化:行情数据回溯
java·开发语言·python·金融·maven
今年下半年6 小时前
AI智能体项目实战记录(02):初始化 Python 项目
开发语言·人工智能·python·fastapi
Williamtym6 小时前
PickGo Lab实践:用浏览器、Python和MCP串起机器人搬运仿真
开发语言·python·机器人
DongQiShanRen6 小时前
六根硬件映射(四):归属澄清、两处不一致与卷十一总表
开发语言·人工智能·算法·rust·哈希算法·集成学习
CS创新实验室6 小时前
Python 对象“死“了之后去了哪?——引用计数与分代垃圾回收
开发语言·jvm·python
quantdash_cc6 小时前
Pandas 处理分钟级股票行情:从原始数据清洗到 K 线聚合
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
yu_zhidun7 小时前
企业电脑监控软件怎么选:别只看功能清单,先看这 6 个底层能力
开发语言·电脑·php·域智盾系统
无忧.芙桃7 小时前
C++语言原理与实践(十六):多态机制讲解
开发语言·c++·多态
李纲明8 小时前
WordPress 白屏排查手册:关插件、换主题、开调试、查 PHP(附停手信号)
开发语言·php·wordpress·故障排查·白屏·插件冲突