opencvsharp 图像的像素的操作

 Mat src = new Mat(@"1.jpg", ImreadModes.AnyColor);                            //三通道
            Mat grayImage = new Mat();            //单通道
           
            int height = src.Rows;
            int width = src.Cols;
            Console.WriteLine("height={0},width={1}", height, width);
            int channels = src.Channels();                                              //获取mat 的通道数 用于底下的判断
            Console.WriteLine("channels={0}", channels);
            Cv2.CvtColor(src, grayImage, ColorConversionCodes.BGR2GRAY);
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (channels == 1)
                    {
                        byte gray = grayImage.Get<byte>(i, j);        //这里AT函数和GET一样只能获取值
                        grayImage.Set<byte>(i, j, (byte)(byte.MaxValue - gray));  //赋值只能用set 这里和c++不同,蒙逼了半小时。(坑)         
                    }
                    else if (channels == 3)
                    {
                        byte b = src.At<Vec3b>(i, j).Item0;            //这里AT函数和GET一样只能获取值
                        byte g = src.At<Vec3b>(i, j).Item1;
                        byte r = src.At<Vec3b>(i, j).Item2;
                        src.Set<Vec3b>(i, j,new Vec3b((byte)(255-b),(byte)(255-g), (byte)(255 - r)));      
                    }
                }
            }
            Cv2.ImShow("src", src);
            Cv2.ImShow("grayImage", grayImage);
            Cv2.WaitKey();

opencvsharp 图像的像素的操作_第1张图片

你可能感兴趣的:(opencvsharp,opencvsharp)