今天学着试了试高斯噪声
Mat dst = new Mat();
int width = src.cols();
int height = src.rows();
int dims = src.channels();
byte[] data = new byte[width * height * dims];
src.get(0, 0, data);
int r = 0,g = 0,b = 0;
Random random = new Random();
for(int row = 0; row < height; row++) {
for(int col = 0; col < width; col++) {
double rf = random.nextGaussian() * 30;
double gf = random.nextGaussian() * 30;
double bf = random.nextGaussian() * 30;
b = data[row * width *dims + col * dims]&0xff; //put rgb into pixel for one row
g = data[row * width *dims + col * dims + 1]&0xff;
r = data[row * width *dims + col * dims + 2]&0xff;
b = clamp(b + bf);
g = clamp(g + gf);
r = clamp(r + rf);
data[row * width * dims + col * dims] = (byte)b;
data[row * width * dims + col * dims + 1] = (byte)g;
data[row * width * dims + col * dims + 2] = (byte)r;
}
}
src.put(0, 0, data);
ImageUI resultwin = new ImageUI();
resultwin.imshow("gaussian noise", src);
/* Imgproc.GaussianBlur(src, dst, new Size(11, 11), 4, 4);
ImageUI resultwin1 = new ImageUI();
resultwin1.imshow("gaussian1 noise", dst);*/
}
public static int clamp(double d) {
if(d > 255)
return 255;
else if(d < 0)
return 0;
else
return (int)d;
}
输出:
高斯模糊
其实也就一个API的事情,主要是明白参数含义
由于基于Java的opencv资料相对较少,下面这是C++的GaussianBlur()高斯模糊参数的表格
出自:@duwangthefirst同学的opencv之GaussianBlur()函数,感谢大佬
Imgproc.GaussianBlur(src, dst, new Size(11, 11), 4, 4);
输出: