首页 > 编程知识 正文

OpenCvSharp 学习笔记4 像素操作2

时间:2023-05-03 18:04:27 阅读:204652 作者:2273

一:API:

1: AT();获取像素值,4个重载,用法大同小异

public T At<T>(int i0, int i1) where T : struct; //返回指定数组元素的值。public T At<T>(int i0) where T : struct;public T At<T>(params int[] idx) where T : struct;public T At<T>(int i0, int i1, int i2) where T : struct; 参数:说明i0:Index along the dimension 0 (沿着空间维度0索引)i1:Index along the dimension 1 (沿着空间维度1索引)

2: Get();与 AT方法一样

public T Get<T>(int i0, int i1) where T : struct;public T Get<T>(int i0, int i1, int i2) where T : struct;public T Get<T>(int i0) where T : struct;public T Get<T>(params int[] idx) where T : struct;

3: Set();对指定位置的像素赋值 4个重载方法用法大同小异

参数:说明i0:Index along the dimension 0 (沿着空间维度0索引)i1:Index along the dimension 1 (沿着空间维度1索引)value:像素值,泛型//将值设置为指定的数组元素。 public void Set<T>(int i0, int i1, T value) where T : struct; public void Set<T>(int i0, int i1, int i2, T value) where T : struct; public void Set<T>(int[] idx, T value) where T : struct; public void Set<T>(int i0, T value) where T : struct; 二: 单通道图像像素处理

代码:

static void Main(string[] args) { string imagePath = @"d:ZG1900161.jpg"; ReadImageSingleChannelPixel(imagePath); } /// <summary> /// 单通道像素操作 /// </summary> public static void ReadImageSingleChannelPixel(string path) { using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth)) using (Mat dst = new Mat()) { //ColorConversionCodes 色彩空间转换枚举类型 BGRA2GRAY 转为灰度单通道 Cv2.CvtColor(src, dst, ColorConversionCodes.BGRA2GRAY); Mat gray = new Mat(); dst.CopyTo(gray); int height = dst.Rows; //行 获取图片的行叠加起来就是高度 int width = dst.Cols; //列 ... ... 宽度 for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { byte p= dst.At<byte>(row, col); //获对应矩阵坐标的取像素 byte value =byte.Parse( (255-p).ToString()); //反转像素值 dst.Set(row, col, value); //赋值 } } using(new Window("gray",WindowMode.FreeRatio,gray)) //单通道图像 using(new Window("dst",WindowMode.FreeRatio,dst)) //反转后的单通道图像 using (new Window("src", WindowMode.FreeRatio, src)) //源图 { Cv2.WaitKey(0); } } }

输出图片结果

二: 三通道图像像素处理

代码:

static void Main(string[] args) { string imagePath = @"d:ZG1900161.jpg"; ReadImageThreeChannelsPixel(imagePath); } /// <summary> /// 三通道像素操作 /// </summary> public static void ReadImageThreeChannelsPixel(string path) { using (Mat src = new Mat(path, ImreadModes.AnyColor | ImreadModes.AnyDepth)) using (Mat dst = new Mat(src.Size(), src.Type())) { int height = src.Rows; int width = src.Cols; int cn = src.Channels(); //获取通道数 #region 反转像素算法 for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { if (cn == 1) //如果是单通道 { byte p = dst.At<byte>(row, col); //获取像素 byte value = byte.Parse((255 - p).ToString()); //反转像素值 dst.Set(row, col, value); //赋值 } else if(cn==3) //如果是三通道 { //读取源图的像素 int b = src.At<Vec3b>(row, col)[0]; int g = src.At<Vec3b>(row, col)[1]; int r = src.At<Vec3b>(row, col)[2]; Vec3b color = new Vec3b { Item0 = (byte)(255 - b), //反转像素 (byte)( Math.Max(r, Math.Max(b, g))); Item1 = (byte)(255 - g), // (byte)(Math.Max(r, Math.Max(b, g))); Item2 = (byte)(255 - r) // (byte)(Math.Max(r, Math.Max(b, g))); }; /* Vec3b color = new Vec3b //反转像素 { Item0 = (byte)Math.Abs(src.Get<Vec3b>(row, col).Item0 - 255), Item1 = (byte)Math.Abs(src.Get<Vec3b>(row, col).Item1 - 255), Item2 = (byte)Math.Abs(src.Get<Vec3b>(row, col).Item2 - 255) }; */ //Math.Max(r, Math.Max(b, g));取灰度,min也可以,但是亮度不同 //赋值 dst.Set<Vec3b>(row,col,color ); } } } #endregion using (new Window("dst", WindowMode.FreeRatio, dst)) //反转 using (new Window("src", WindowMode.FreeRatio, src)) //源图 { Cv2.WaitKey(0); } } }

输出图片结果:

Vec3b: 通道顺序 blue grenn red 在C++中对应uchar类型 ,C#对应byte类型:
像素数据结构 BGR 包含三个像素值 R代表红,red; G代表绿,green; B代表蓝,blue。RGB模式就是,色彩数据模式,R在高位,G在中间,B在低位。BGR正好相反。例如,如果色彩数据是24位,对于RGB模式,就是高8位是R,中间8位是G,低8位是B。一个色彩数据共24位,3个字节。

Vec3f:对应三通道的float类型。
把CV_8UC1 转换到 CV32F1实现:

src.convertTo(dst,CV_32F1);

反转像素API:

Cv2.BitwiseNot(src, dst); //反转像素函数,不需要操作像素,达到的效果一样

~ 取反符号:输出同样效果:

using (new Window("dst", WindowMode.FreeRatio, ~src)) //反转 using (new Window("src", WindowMode.FreeRatio, src)) //源图 { Cv2.WaitKey(0); }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。