halcom中国吧 关注:11贴子:147
  • 0回复贴,共1

opencvsharp像素值操作

只看楼主收藏回复

对于3通道彩色图像
Vec3b black = new Vec3b(0, 0, 0);//3个char,对应BGR
src.Set(i, j, black);//行,列,vec3b(opencv好奇葩,好多都反着来)
对于单通道灰色图像
char black = (char)0;//
src.Set(i, j, black);//行,列,char
椒盐噪声
Mat panda = new Mat(@"滚滚.png", ImreadModes.GrayScale);//读取为灰度图
Random random = new Random();
for (int i = 0; i < 30000; i++)//30000个点
{
int row = random.Next(panda.Rows);//随机生成行
int col = random.Next(panda.Cols);//随机生成列
char white = (char)255;
panda.Set(row, col, white);
}
Cv2.ImShow("滚滚", panda);
Cv2.WaitKey();
彩色
Mat panda = new Mat(@"滚滚.png", ImreadModes.Color);//读取为彩图
Random random = new Random();
for (int i = 0; i < 30000; i++)//30000个点
{
int row = random.Next(panda.Rows);//随机生成行
int col = random.Next(panda.Cols);//随机生成列
//char white = (char)255;
Vec3b white = new Vec3b(255, 255, 255);
panda.Set(row, col, white);
}
Cv2.ImShow("滚滚", panda);
Cv2.WaitKey();
密恐福利。
读取像素点
Mat panda = new Mat(@"滚滚.png", ImreadModes.GrayScale);//读取为彩图
Mat gungun = new Mat();
panda.CopyTo(gungun);/作为对比
for (int i = 0; i < panda.Rows; i++)//操作像素点
{
for (int j = 0; j < panda.Cols; j++)
{
byte color = (byte)Math.Abs(panda.Get<byte>(i, j)-50);//读取原来的通道值并减50
panda .Set(i, j, color);
}
}
Cv2.ImShow("处理后",panda);
Cv2.ImShow("滚滚", gungun);
Cv2.WaitKey();
彩色
for (int i = 0; i < panda.Rows; i++)//操作像素点
{
for (int j = 0; j < panda.Cols; j++)
{
Vec3b color = new Vec3b();//新建vec3b的对象,
color.Item0= (byte)Math.Abs(panda.Get<Vec3b>(i, j).Item0 - 50);//读取原来的通道值并减50
color.Item1 = (byte)Math.Abs(panda.Get<Vec3b>(i, j).Item1 - 50);//读取原来的通道值并减50
color.Item2 = (byte)Math.Abs(panda.Get<Vec3b>(i, j).Item2 - 50);//读取原来的通道值并减50
panda.Set(i, j, color);
}
}
Cv2.ImShow("处理后",panda);
Cv2.ImShow("滚滚", gungun);
Cv2.WaitKey();
}
记得加abs否则!
2.重映射
//
// 摘要:
// Applies a generic geometrical transformation to an image.
//对图像应用一般几何变换。
// 参数:
// src:
// Source image.
//源图像。
// dst:
// Destination image. It has the same size as map1 and the same type as src
//目标图像。它的大小与 map1 相同, 类型与 src 相同。
// map1:
// The first map of either (x,y) points or just x values having the type CV_16SC2,
// CV_32FC1, or CV_32FC2.
//(x、y) 点的第一个映射或仅有类型为 CV_16SC2 的 x 值,
// map2:
// The second map of y values having the type CV_16UC1, CV_32FC1, or none (empty
// map if map1 is (x,y) points), respectively.
//y 值的第二个映射具有类型 CV_16UC1、CV_32FC1 或 none (如果 map1 为 (x、y) 点), 则分别为空映射。
// interpolation:
// Interpolation method. The method INTER_AREA is not supported by this function.
//插值方法。此函数不支持方法 INTER_AREA。
// borderMode:
// Pixel extrapolation method. When borderMode=BORDER_TRANSPARENT, it means that
// the pixels in the destination image that corresponds to the "outliers" in the
// source image are not modified by the function.
//像素外推方法。当 borderMode = BORDER_TRANSPARENT 时, 它意味着目标图像中与 "异常值 " 对应的像素。该函数不修改源图像。
//
// borderValue:
// Value used in case of a constant border. By default, it is 0.在常量边框的情况下使用的值。默认情况下, 该值为0。
public static void Remap(InputArray src, OutputArray dst, InputArray map1, InputArray map2, InterpolationFlags interpolation = InterpolationFlags.Linear,
BorderTypes borderMode = BorderTypes.Constant, Scalar? borderValue = null);//微软翻译真牛批
简单来说就是把一个像素点的东西移动到另外一个像素点,直接上例程。
Mat panda = new Mat(@"滚滚.png", ImreadModes.Color);//读取为彩图
Mat gungun = new Mat();
panda.CopyTo(gungun);//作为对比
Mat pandax = new Mat(panda.Size(), MatType.CV_32FC1);//存放位置
Mat panday = new Mat(panda.Size(), MatType.CV_32FC1);
for (int i = 0; i < panda.Rows; i++)//遍历所有像素点
{
for (int j = 0; j < panda.Cols; j++)
{
pandax.Set(i, j, (float)j);//x不动,转成float是必须的,参数不一样重载的函数也不一样
panday.Set(i, j, (float)(panda.Rows - i));//y上下对换
}
}
Cv2.Remap(panda, panda, pandax, panday);
Cv2.ImShow("处理后",panda);
Cv2.ImShow("滚滚", gungun);
Cv2.WaitKey();
//来个复杂点的,
for (int i = 0; i < panda.Rows; i++)//遍历所有像素点
{
for (int j = 0; j < panda.Cols; j++)
{
pandax.Set(i, j, (float)j);//x不动,转成float是必须的,参数不一样重载的函数也不一样
// panday.Set(i, j, (float)(panda.Rows - i));//y上下对换
panday.Set(i, j, (float)(i + 5 * Math.Sin(j / 1.0)));//波浪
}
}


IP属地:广东1楼2023-09-15 22:17回复