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下载

相关推荐
Sylvia-girl2 小时前
Java——抽象类
java·开发语言
Yana.nice4 小时前
Bash函数详解
开发语言·chrome·bash
江沉晚呤时5 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
Oberon5 小时前
Avalonia硬配.NET Framework 4.8
c#·.net·avalonia·.net framework
tomorrow.hello6 小时前
Java并发测试工具
java·开发语言·测试工具
晓13137 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊7 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
她说人狗殊途8 小时前
java.net.InetAddress
java·开发语言
天使day8 小时前
Cursor的使用
java·开发语言·ai
喵叔哟8 小时前
3. 【Blazor全栈开发实战指南】--Blazor是什么?为什么选择Blazor?
c#·.netcore