OpenCvSharp圖像的修改和保存以及掩膜操作
一 :圖像的顏色空間轉(zhuǎn)換
在OpenCvSharp中顏色轉(zhuǎn)換函數(shù)為:Cv2.CvtColor()
參數(shù):
參數(shù) | 說明 |
---|---|
src: | 源圖像,8位無符號(hào),16位無符號(hào)或單精度浮點(diǎn) |
dst: | 輸出圖像,具有與src相同的大小和深度 |
code: | 顏色空間轉(zhuǎn)換代碼:(ColorConversionCodes)枚舉類型 |
代碼:
static void Main(string[] args) { Mat src = new Mat(@"C:\Users\whx\Desktop\opcvImage\s1.jpg ", ImreadModes.Color); if (src == null) //上面的加載方式如果找不到指定的文件也會(huì)報(bào)錯(cuò) { Console.WriteLine("加載圖像失敗"); return; } Mat outImage = new Mat(); //聲明一個(gè)容器,裝載改變后的圖像 //參數(shù):1 原圖矩陣容器 2:保存圖像的矩陣容器 3:顏色轉(zhuǎn)換通道(很多,查手冊(cè)) Cv2.CvtColor(src, outImage, ColorConversionCodes.RGB2GRAY); //轉(zhuǎn)為灰度空間圖像, //參數(shù):1 要保存圖片的路徑 2:圖像的矩陣容器 ,(圖片保存格式個(gè)根據(jù)自定義的后綴名) Cv2.ImWrite(@"C:\Users\whx\Desktop\out.png", outImage);//保存到桌面 using (new Window("src", WindowMode.Normal, src)) using (new Window("out", WindowMode.Normal, outImage)) { Cv2.WaitKey(); } }
左邊是源圖像,右邊是轉(zhuǎn)為灰度空間的圖像,保存路徑在桌面。
轉(zhuǎn)為灰度空間的類型在 OpenCvSharp 中的 ColorConversionCodes.RGB2GRAY 與 ColorConversionCodes.BRR2GRAY 都能實(shí)現(xiàn),OpenCvSharp 加載進(jìn)來的圖像是哪一種?
代碼
static void Main(string[] args) { #region //自定義一張全紅色的圖片 Mat src = new Mat(100,100,MatType.CV_8UC3,new Scalar(0,0,255)); Vec3b vec3B = new Vec3b(); //獲取第一個(gè)像素的三通道像素值 vec3B.Item0 = src.At<Vec3b>(0, 0)[0]; vec3B.Item1 = src.At<Vec3b>(0, 0)[1]; vec3B.Item2 = src.At<Vec3b>(0, 0)[2]; Console.WriteLine("0 :"+vec3B.Item0); //控制臺(tái)輸出 Console.WriteLine("1 :"+vec3B.Item1); Console.WriteLine("2 :"+vec3B.Item2); using (new Window("src image", WindowMode.FreeRatio, src)) //創(chuàng)建一個(gè)新窗口顯示圖像 { Cv2.WaitKey(); } #endregion }
根據(jù)輸出像素值(0,0,255)可以看出 OpenCvSharp 三通道像素值排列為:BGR
二: 掩膜操作,提高圖像對(duì)比度
使用Cv2.Filter2D函數(shù):
參數(shù) | 說明 |
---|---|
src: | 輸入的源圖像 |
dst: | 輸出圖像,一個(gè)Mat 對(duì)象,與原圖圖像具有相同的大小和圖像深度 |
ddepth: | 目標(biāo)圖像的所需深度。如果它是負(fù)的,它就是與src.depth()相同,不確定時(shí)就填 -1 |
kernel: | 卷積核 |
anchor: | 內(nèi)核的錨點(diǎn),表示經(jīng)過過濾的點(diǎn)的相對(duì)位置. (- 1,-1)表示錨位于內(nèi)核中心 |
delta: | 在卷積過程中,該值會(huì)加到每個(gè)像素上。默認(rèn)情況下,這個(gè)值為 0 。相當(dāng)于一個(gè)增益值 |
borderType: | 指定邊緣處理的方法,比較復(fù)雜,選擇默認(rèn)值即可。是枚舉類型 |
代碼:
static void Main(string[] args) { using (Mat src = new Mat(@"C:\Users\whx\Desktop\opcvImage\m4.jpg", ImreadModes.AnyColor | ImreadModes.AnyDepth)) using (Mat dst = new Mat()) { //定義一個(gè)掩膜矩陣 InputArray kernel = InputArray.Create<int>(new int[3, 3] { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } }); //進(jìn)行掩膜操作,提高圖片亮度 Cv2.Filter2D(src, dst, -1, kernel, new Point(-1, 1), 0, BorderTypes.Default); using (new Window("OutputImage", WindowMode.Normal, dst)) using (new Window("InputImage",WindowMode.Normal,src)) { Cv2.WaitKey(0); } } }
從上圖可以看出,OutputImage 比 Inputimage 的亮度明顯增強(qiáng)。
三:利用指針修改圖像像素值,進(jìn)行圖像對(duì)比度處理
代碼:
unsafe static void Main(string[] args) { Mat src, dst; src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //讀取圖片 if (src.Data == null) { Console.WriteLine("加載圖像失敗"); return; } #region /* * 兩種判斷方法都可以 */ //if(src.Empty()) //如果數(shù)組沒有元素,則返回true。 //{ // Console.WriteLine("加載圖像失敗"); // return; //} //顯示方式2 //new Window("Input Image", WindowMode.FreeRatio); //Cv2.ImShow("Input Image",src); //Cv2.WaitKey(0); #endregion #region 指針操作增加飽和度 int clos = (src.Cols - 1) * src.Channels(); //RGB有三個(gè)通道,(圖像的列(長度) * 圖像的通道數(shù)) int rows = src.Rows; //行(高度) int offsetx = src.Channels(); dst = new Mat(src.Size(), src.Type()); //初始化 for (int row = 1; row < rows - 1; row++) { IntPtr current = src.Ptr(row); //當(dāng)前行 byte* curr = (byte*)current.ToPointer(); IntPtr upRow = src.Ptr(row - 1);//上一行 byte* up = (byte*)upRow.ToPointer(); IntPtr nextRow = src.Ptr(row + 1);//下一行 byte* next = (byte*)nextRow.ToPointer(); IntPtr outPut = dst.Ptr(row); //輸出 byte* opt = (byte*)outPut.ToPointer(); for (int col = offsetx; col < clos; col++) { opt[col] = (byte)Saturate.Saturate_cast((5 * curr[col] - (curr[col - offsetx] + curr[col + offsetx] + up[col] + next[col]))); } } #endregion using (new Window("OutputImage", WindowMode.FreeRatio, dst)) using (new Window("InputImage", WindowMode.FreeRatio, src)) { Cv2.WaitKey(0); } }
unsafe static void Main(string[] args) { Mat src, dst; src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //讀取圖片 if (src.Data == null) { Console.WriteLine("加載圖像失敗"); return; } #region /* * 兩種判斷方法都可以 */ //if(src.Empty()) //如果數(shù)組沒有元素,則返回true。 //{ // Console.WriteLine("加載圖像失敗"); // return; //} //顯示方式2 //new Window("Input Image", WindowMode.FreeRatio); //Cv2.ImShow("Input Image",src); //Cv2.WaitKey(0); #endregion #region 指針操作增加飽和度 int clos = (src.Cols - 1) * src.Channels(); //RGB有三個(gè)通道,(圖像的列(長度) * 圖像的通道數(shù)) int rows = src.Rows; //行(高度) int offsetx = src.Channels(); dst = new Mat(src.Size(), src.Type()); //初始化 for (int row = 1; row < rows - 1; row++) { IntPtr current = src.Ptr(row); //當(dāng)前行 byte* curr = (byte*)current.ToPointer(); IntPtr upRow = src.Ptr(row - 1);//上一行 byte* up = (byte*)upRow.ToPointer(); IntPtr nextRow = src.Ptr(row + 1);//下一行 byte* next = (byte*)nextRow.ToPointer(); IntPtr outPut = dst.Ptr(row); //輸出 byte* opt = (byte*)outPut.ToPointer(); for (int col = offsetx; col < clos; col++) { opt[col] = (byte)Saturate.Saturate_cast((5 * curr[col] - (curr[col - offsetx] + curr[col + offsetx] + up[col] + next[col]))); } } #endregion using (new Window("OutputImage", WindowMode.FreeRatio, dst)) using (new Window("InputImage", WindowMode.FreeRatio, src)) { Cv2.WaitKey(0); } }
效果與上面使用API操作基本一致。這里是由一個(gè)計(jì)算公式:
對(duì)應(yīng)這行代碼:
opt[col] = (byte)Saturate.Saturate_cast((5 * curr[col] - (curr[col - offsetx] + curr[col + offsetx] + up[col] + next[col])));
四:減少圖像亮度
代碼:
static void Main(string[] args) { Mat src, dst; src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //讀取圖片 dst = new Mat(src.Size(), src.Type()); //初始化 src.CopyTo(dst); //把原圖像拷貝到 dst 中 for (int i = 0; i < src.Rows; i++) { for (int j = 0; j < src.Cols; j++) { Vec3b color = new Vec3b();//新建Vec3b對(duì)象(字節(jié)的三元組(System.Byte)) color.Item0 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item0 - 50));// B 讀取原來的通道值并減50 color.Item1 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item1 - 50)); // G color.Item2 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item2 - 50)); // R src.Set(i, j, color); } } using (new Window("Input", WindowMode.FreeRatio, dst)) using (new Window("Output", WindowMode.FreeRatio, src)) { Cv2.WaitKey(0); } }
輸出圖像明顯比輸入的亮度下降。
代碼:
static void Main(string[] args) { Mat src, dst; src = Cv2.ImRead(@"C:\Users\whx\Desktop\opcvImage\m4.jpg"); //讀取圖片 dst = new Mat(src.Size(), src.Type()); //初始化 src.CopyTo(dst); //把原圖像拷貝到 dst 中 for (int i = 0; i < src.Rows; i++) { for (int j = 0; j < src.Cols; j++) { Vec3b color = new Vec3b();//新建Vec3b對(duì)象(字節(jié)的三元組(System.Byte)) color.Item0 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item0 + 50));//讀取原來的通道值并加50 color.Item1 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item1 + 50)); color.Item2 = (byte)Saturate.Saturate_cast((src.Get<Vec3b>(i, j).Item2 + 50)); src.Set(i, j, color); } } using (new Window("Input", WindowMode.FreeRatio, dst)) using (new Window("Output", WindowMode.FreeRatio, src)) { Cv2.WaitKey(0); } }
輸出圖像明顯比輸入的亮度提高很多。
到此這篇關(guān)于OpenCvSharp圖像的修改和保存以及掩膜操作的文章就介紹到這了,更多相關(guān)OpenCvSharp圖像掩膜內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#操作數(shù)據(jù)庫總結(jié)(vs2005+sql2005)
C#操作數(shù)據(jù)庫總結(jié),每次做項(xiàng)目都會(huì)用到數(shù)據(jù)庫,對(duì)數(shù)據(jù)庫的操作都是糊里糊涂從書里找代碼用。通過昨天晚上與今天早上的努力,把數(shù)據(jù)庫的操作整理了一下,下面把整理結(jié)果做個(gè)小結(jié)2012-09-09Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例
本篇文章主要介紹了Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05C#使用csvhelper實(shí)現(xiàn)csv的基本操作
CsvHelper 是一個(gè)用于讀寫 CSV 文件的.NET庫,極其快速,靈活且易于使用,CsvHelper 建立在.NET Standard 2.0 之上,幾乎可以在任何地方運(yùn)行,本文給大家介紹了C#使用csvhelper實(shí)現(xiàn)csv的基本操作,需要的朋友可以參考下2024-07-07