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

相关推荐
沐知全栈开发6 小时前
HTML5 浏览器支持
开发语言
wasp5206 小时前
AgentScope Java 核心架构深度解析
java·开发语言·人工智能·架构·agentscope
WHOVENLY6 小时前
【javaScript】- 笔试题合集(长期更新,建议收藏,目前已更新至31题)
开发语言·前端·javascript
慌糖6 小时前
流-为序列化解释
开发语言
LXS_3576 小时前
Day 18 C++提高 之 STL常用容器(string、vector、deque)
开发语言·c++·笔记·学习方法·改行学it
王琦03187 小时前
Python 函数详解
开发语言·python
胡伯来了7 小时前
13. Python打包工具- setuptools
开发语言·python
小鸡吃米…7 小时前
Python 中的多层继承
开发语言·python
deng-c-f8 小时前
Linux C/C++ 学习日记(53):原子操作(二):实现shared_ptr
开发语言·c++·学习
wanghowie8 小时前
01.07 Java基础篇|函数式编程与语言新特性总览
java·开发语言·面试