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

相关推荐
波力海苔夹心脆6751 小时前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
不会算法的笨小孩1 小时前
异常的使用以及会出现的问题
开发语言·c++·c++11·异常
AI导出鸭PC端3 小时前
Perplexity批量导出没有PDF模式?AI导出鸭插件补上最后一块拼图
人工智能·pdf·c#·ai导出鸭
Hhy_11074 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
starzy19904 小时前
深入理解Scala函数定义:从方法到函数的类型跨越
开发语言·scala
starzy19904 小时前
深入理解Scala递归函数:从栈溢出到尾调用优化
开发语言·scala
三*一4 小时前
Mapbox‑GL JS 多边形折点编辑工具:实现原理、功能拆解与踩坑总结
开发语言·javascript·ecmascript
YSL0701244 小时前
C++基础
开发语言·c++
若亦_4 小时前
Java中关于this的三种用法
java·开发语言·intellij-idea
Risehuxyc4 小时前
C# 选用更流行的语言版本
c#