利用opencvsharp,编写C#读取图像的像素与操作图像像素的程序
时间: 2023-07-08 11:34:12 浏览: 61
下面是一个使用OpenCvSharp读取和操作图像像素的C#示例程序:
```csharp
using OpenCvSharp;
class Program
{
static void Main(string[] args)
{
// 读取图像
Mat image = Cv2.ImRead("test.jpg");
// 获取图像的宽和高
int width = image.Width;
int height = image.Height;
// 遍历图像像素
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// 获取像素值
Vec3b color = image.At<Vec3b>(y, x);
// 操作像素值
color.Item0 = (byte)(255 - color.Item0);
color.Item1 = (byte)(255 - color.Item1);
color.Item2 = (byte)(255 - color.Item2);
// 设置像素值
image.Set(y, x, color);
}
}
// 保存图像
Cv2.ImWrite("result.jpg", image);
}
}
```
这个程序可以读取名为"test.jpg"的图像,并将图像中的每个像素的RGB值取反,最后将修改后的图像保存为"result.jpg"。在这个程序中,使用了OpenCvSharp的Mat类来表示图像,At方法来访问像素值,Set方法来设置像素值,ImRead方法来读取图像,ImWrite方法来保存图像。