#region 图像像素的读写操作
static void Main(string[] args)
{
Mat src = Cv2.ImRead("lenna.png", ImreadModes.AnyColor);
if (src.Empty())
{
Console.WriteLine("图像未成功加载...");
return;
}
Cv2.ImShow("src image", src);
int W = src.Cols;
int H = src.Rows;
int dims = src.Channels();
for (int row = 0; row < H; row++)
{
for (int col = 0; col < W; col++)
{
if (dims == 1)//灰色图像
{
byte pv = src.Get<byte>(row, col);
src.Set<byte>(row, col, (byte)(255 - pv));
}
else//彩色图像
{
Vec3b color = src.Get<Vec3b>(row, col);
color.Item0 = (byte)(255 - color.Item0);
color.Item1 = (byte)(255 - color.Item1);
color.Item2 = (byte)(255 - color.Item2);
src.Set<Vec3b>(row, col, color);
}
}
}
Cv2.ImShow("output image", src);
Cv2.WaitKey();
Cv2.DestroyAllWindows();
}
#endregion
OpenCVSharp 笔记7 图像像素的读写操作
最新推荐文章于 2023-12-03 22:03:20 发布