数学原理:
首先看两张图片,大小均为256 * 256个像素, 第一张是纯蓝色
图一:
第二张是加有随机噪声的蓝色
图二:
产生随机噪声的算法简单的不能再简单了
假设rgb的r与g颜色分量均为零, 则 blue = 255 * math.random() 随机数的取值范围在
[0, 1]之间, 程序的核心代码如下:
for(int row=0; row<256; row++) {
for(int col=0; col<256; col++) {
b = (int)(255.0d * math.random());
rgbdata[index]= ((clamp(a) & 0xff) << 24) |
((clamp(r)& 0xff) << 16) |
((clamp(g)& 0xff) << 8) |
((clamp(b)& 0xff));
index++;
}
}
上面显然不是我想要的结果,我想要的是下面两种:
图三:
图四:
对的,只要我们对上面的算法稍加改进,就可以实现这样漂亮的噪声效果
实现第二张图效果的算法缺点在于,它每次都产生一个新的随机数,假设[0,1] = 255,接着第
二点随机可以能为[0, 2] = 0 第三个点可能随机值为[0, 3] = 125, 毫无规律可言,而我希望是
假设第一点随机[0, 1] = 255则间隔n个点以后再产生下个随机颜色值[0,n+1] =125, 在下一
个点则为[0, 2n +1] = 209…..于是问题产生了, 我们怎么计算[1, n]的之间的每个像素点的值
哇,这个问题不正是关于图像放缩的插值问题嘛,一个最简单的选择是双线性插值算法,
有了算法选择,下面的问题就是我们怎么计算点值的问题,面临两个选择,一个值照搬双线
性插值中的计算方法,但是有点不自然,我们想要的是噪声,显然线性的计算结果不是最好
的最好的选择,cos(x)如何,在[0, pi]内是递减,在[pi,2pi]内是递增,而且值的范围在[-1, 1]
之间,而我们的随机数值要在[0, 1]之间于是综合上述考虑我们有cos(pi + (x-x0/x1-x0)* pi) + 1, 现
在计算出来的值是[0, 1]区间之内 根据插值公式最终有:
y= (y1-y0) * cos(pi + (x-x0/x1-x0) * pi) + 1 + y0
其中[x, y]代表要计算的点,周围四个采样点为:[x-n, y-n], [x+n,y-n], [x-n, y+n], [x+n, y+n ]
运用双线性插值原理即可计算出[1, n]个每个像素点的值。
关键代码实现及解释:
获取四个采样点,及其值,然后使用类似双线性算法计算出[x,y]的随机数值进而计算出像素值
的程序代码如下:
// bi-line interpolation algorithm here!!!
double getcolor(int x, int y, int m, int colortype)
{
int x0 = x - (x % m);
int x1 = x0 + m;
int y0 = y - (y % m);
int y1 = y0 + m;
double x0y0 = noise(x0,y0, colortype);
double x1y0 = noise(x1,y0, colortype);
double x0y1 = noise(x0,y1, colortype);
double x1y1 = noise(x1,y1, colortype);
double xx0 =interpolate(x0, x0y0, x1, x1y0, x);
double xx1 = interpolate(x0,x0y1, x1, x1y1, x);
double n =interpolate(y0, xx0, y1, xx1, y);
return n;
}
根据两个点计算插入值的公式代码如下:
return (1.0 + math.cos(math.pi + (math.pi / (x1-x0)) * (x-x0))) / 2.0
* (xx1-xx0) + xx0;
对一张图像实现随机噪声值得出像素值计算的代码如下:
for(int row=0; row<256; row++) {
for(int col=0; col<256; col++) {
// set random color value for each pixel
r = (int)(255.0d * getcolor(row, col, intervalpixels, 1));
g = (int)(255.0d * getcolor(row, col, intervalpixels, 2));
b = (int)(255.0d * getcolor(row, col, intervalpixels, 4));
rgbdata[index] = ((clamp(a) & 0xff) << 24) |
((clamp(r) & 0xff) << 16) |
((clamp(g) & 0xff) << 8) |
((clamp(b) & 0xff));
index++;
}
完全源代码如下:
import java.awt.borderlayout;
import java.awt.dimension;
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.renderinghints;
import java.awt.image.bufferedimage;
import java.util.random;
import javax.swing.jcomponent;
import javax.swing.jframe;
public class randomnoiseimage extends jcomponent {
/**
*
*/
private static final long serialversionuid = -2236160343614397287l;
private bufferedimage image = null;
private double[] blue_random;
private double[] red_random;
private double[] green_random;
private int intervalpixels = 40; // default
public randomnoiseimage() {
super();
this.setopaque(false);
protected void paintcomponent(graphics g) {
graphics2d g2 = (graphics2d)g;
g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);
g2.drawimage(getimage(), 5, 5, image.getwidth(), image.getheight(), null);
private bufferedimage getimage() {
if(image == null) {
image = new bufferedimage(256, 256, bufferedimage.type_int_argb);
int[] rgbdata = new int[256*256];
generatenoiseimage(rgbdata);
setrgb(image, 0, 0, 256, 256, rgbdata);
}
return image;
private void generatenoiseimage(int[] rgbdata) {
int index = 0;
int a = 255;
int r = 0;
int g = 0;
int b = 0;
int sum = 256 * 256;
blue_random = new double[sum];
red_random = new double[sum];
green_random = new double[sum];
random random = new random();
for(int i=0; i< sum; i++) {
blue_random[i] = random.nextdouble();
red_random[i] = random.nextdouble();
green_random[i] = random.nextdouble();
for(int row=0; row<256; row++) {
for(int col=0; col<256; col++) {
// set random color value for each pixel
r = (int)(255.0d * getcolor(row, col, intervalpixels, 1));
g = (int)(255.0d * getcolor(row, col, intervalpixels, 2));
b = (int)(255.0d * getcolor(row, col, intervalpixels, 4));
rgbdata[index] = ((clamp(a) & 0xff) << 24) |
((clamp(r) & 0xff) << 16) |
((clamp(g) & 0xff) << 8) |
((clamp(b) & 0xff));
index++;
}
private int clamp(int rgb) {
if(rgb > 255)
return 255;
if(rgb < 0)
return 0;
return rgb;
// bi-line interpolation algorithm here!!!
int x0 = x - (x % m);
int x1 = x0 + m;
int y0 = y - (y % m);
int y1 = y0 + m;
double x0y0 = noise(x0, y0, colortype);
double x1y0 = noise(x1, y0, colortype);
double x0y1 = noise(x0, y1, colortype);
double x1y1 = noise(x1, y1, colortype);
double xx0 = interpolate(x0, x0y0, x1, x1y0, x);
double xx1 = interpolate(x0, x0y1, x1, x1y1, x);
double n = interpolate(y0, xx0, y1, xx1, y);
// algorithm selection here !!!
private double interpolate(double x0, double xx0, double x1, double xx1, double x) {
return (1.0 + math.cos(math.pi +
(math.pi / (x1-x0)) * (x-x0))) / 2.0 * (xx1-xx0) + xx0;
double noise(int x, int y, int colortype)
if(colortype == 1) {
if (x < 256 && y < 256)
return red_random[y * 256 + x];
else
return 0.0;
} else if(colortype == 2) {
return green_random[y * 256 + x];
} else {
return blue_random[y * 256 + x];
public void setrgb( bufferedimage image, int x, int y, int width, int height, int[] pixels ) {
int type = image.gettype();
if ( type == bufferedimage.type_int_argb || type == bufferedimage.type_int_rgb )
image.getraster().setdataelements( x, y, width, height, pixels );
else
image.setrgb( x, y, width, height, pixels, 0, width );
public static void main(string[] args) {
jframe frame = new jframe("noise art panel");
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.getcontentpane().setlayout(new borderlayout());
// display the window.
frame.getcontentpane().add(new randomnoiseimage(), borderlayout.center);
frame.setpreferredsize(new dimension(280,305));
frame.pack();
frame.setvisible(true);