C#?OpenCvSharp?顏色反轉(zhuǎn)實(shí)例詳解
1、什么是OpenCVSharp
為了解決在Csharp下編寫(xiě)OpenCV程序的問(wèn)題,我做過(guò)比較深入的研究,并且實(shí)現(xiàn)了高效可用的方法(GOCW);這幾天在搜集資料的時(shí)候,偶爾看見(jiàn)了OpenCVSharp,從時(shí)間上來(lái)看,它已經(jīng)經(jīng)過(guò)了更久的發(fā)展,應(yīng)該有許多直接借鑒、或者直接使用的地方。
OpenCVSharp有一名日本工程師開(kāi)發(fā),項(xiàng)目地址為:https://github.com/shimat/opencvsharp。其是OpenCV的.NET wrapper,它比Emgucv更接近于原始的OpenCV,并且有很多的樣例參考,其采用LGPL發(fā)行,對(duì)商業(yè)應(yīng)用友好(基本上相當(dāng)于BSD)。
2、OpenCVSharp有什么特點(diǎn)
- 直接封裝了更多的OpenCV方法,降低了學(xué)習(xí)的難度,比EmguCV更便于使用
- 大部分繼承了IDisposable接口,方便使用using語(yǔ)句
- 可以直接調(diào)用原始風(fēng)格的OpenCV方法
- 可以將圖像對(duì)象直接轉(zhuǎn)換成GDI使用的Bitmap和WPF的WriteBitmap
- 支持Mono。
在C#中使用OpenCV(使用OpenCVSharp)的實(shí)現(xiàn)
效果
灰度圖

黑白色反轉(zhuǎn)

彩色反轉(zhuǎn)

項(xiàng)目

代碼
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_顏色反轉(zhuǎn)
{
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);
}
}
}下載
到此這篇關(guān)于C# OpenCvSharp 顏色反轉(zhuǎn)的文章就介紹到這了,更多相關(guān)C# OpenCvSharp 顏色反轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用EF連接PGSql數(shù)據(jù)庫(kù)的完整步驟
這篇文章主要給大家介紹了關(guān)于C#使用EF連接PGSql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
使用C#解決Excel自動(dòng)適應(yīng)列寬的問(wèn)題
這篇文章主要介紹了如何使用C#解決Excel自動(dòng)適應(yīng)列寬的問(wèn)題,通過(guò) COM 操作 Excel 自動(dòng)適應(yīng)列寬的方法是 AutoFit 方法,該方法適于自動(dòng)適應(yīng)列寬或行高,文中通過(guò)代碼示例和圖文講解的非常詳細(xì),需要的朋友可以參考下2024-06-06
C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法
下面小編就為大家?guī)?lái)一篇C# ping網(wǎng)絡(luò)IP 實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測(cè)的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
C#實(shí)現(xiàn)簡(jiǎn)單的汽車(chē)租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)汽車(chē)租賃系統(tǒng)的具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
WindowsForm移動(dòng)一個(gè)沒(méi)有標(biāo)題欄的窗口的方法
這篇文章主要介紹了WindowsForm移動(dòng)一個(gè)沒(méi)有標(biāo)題欄的窗口的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#實(shí)現(xiàn)的二維數(shù)組排序算法示例
這篇文章主要介紹了C#實(shí)現(xiàn)的二維數(shù)組排序算法,涉及C#針對(duì)二維數(shù)組的遍歷、判斷、排序等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

