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

相关推荐
mabing9933 分钟前
Qt QMessageBox、QDialogButtonBox中英文翻译动态切换
开发语言·qt
Sagittarius_A*4 分钟前
【RCELABS】Level 17~18 —— PHP命令执行函数与环境变量注入
开发语言·安全·web安全·靶场·php·rce
冻柠檬飞冰走茶5 分钟前
PTA基础编程题目集 7-19 支票面额(C语言实现)
c语言·开发语言·数据结构·算法
破碎的南瓜18 分钟前
靶场中使用到的php函数以及每种漏洞的防御方式
开发语言·php
谁看我谁 狼家二丫38 分钟前
局域网文件共享实战:从“账户被禁用”到成功互传文件
开发语言·php
醉城夜风~1 小时前
手撕 C++ string:从零实现一个简易字符串类
开发语言·c++·算法
秋田君1 小时前
QT_QT布局常用类QSplitter窗口分割类与QDockWidget窗口停靠类
开发语言·数据库·qt
老王生涯1 小时前
rust开发环境配置-Windows & GNU
开发语言·windows·rust
Yeauty1 小时前
自建 HLS 第一问:fMP4 还是 TS?用 Rust 在进程内把两种都跑出来
开发语言·后端·rust
颜x小2 小时前
[C#]泛型类与泛型方法
开发语言·c++·c#