java水泡_JAVA图像处理系列(八)——艺术效果:水泡

艺术效果水泡

通过对图像进行变形或叠加其他图片,能够实现许多有趣的艺术效果,本文介绍类似水泡效果的实现方式,下面先看一下实现的效果。

第一张为原始图像,第二张为叠加水泡效果的图像。

211c7f7a62b3

cat.jpg

211c7f7a62b3

cat_belb.jpg

算法原理

图像中的水泡效果,是由两个算法叠加而成。第一个为计算水泡上反射光照的效果,第二个为图像通过水泡折射后的形变计算。

(1)光照效果实现

通过设定光源的位置,及水泡上个点与光源的位置关系进行计算,下面是实现代码:

public static void upLightPoint(BufferedImage image, int x, int y, double scale, double hall) {

Pixel pixel = new Pixel();

if (scale > 1)

scale = 1;

else if (scale < 0) {

if (hall == 0) {

scale = 0;

} else {

scale = scale / hall;

}

}

pixel.setRGB(image.getRGB(x, y));

if (scale >= 0) {

pixel.red = pixel.red + (int) ((255 - pixel.red) * scale);

pixel.green = pixel.green + (int) ((255 - pixel.green) * scale);

pixel.blue = pixel.blue + (int) ((255 - pixel.blue) * scale);

} else {

pixel.red = pixel.red + (int) (pixel.red * scale);

pixel.green = pixel.green + (int) (pixel.green * scale);

pixel.blue = pixel.blue + (int) (pixel.blue * scale);

}

image.setRGB(x, y, pixel.getRGB());

}

public static void drawBall(BufferedImage bimg, int x_center, int y_center, int radius, int x_lamp, int y_lamp) {

double z, z_lamp;

double distance;

for (int y = y_center - radius; y < y_center + radius; y++) {

for (int x = x_center - radius; x < x_center + radius; x++) {

distance = (x - x_center) * (x - x_center) + (y - y_center) * (y - y_center);

if (distance > radius * radius)

continue;

z = Math.sqrt(radius * radius - distance);

z_lamp = Math.sqrt(radius * radius - (x_lamp - x_center) * (x_lamp - x_center)

- (y_lamp - y_center) * (y_lamp - y_center));

distance = Math.sqrt(radius * radius * 2 - 2 * (x - x_center) * (x_lamp - x_center)

- 2 * (y - y_center) * (y_lamp - y_center) - 2 * z * z_lamp);

try {

upLightPoint(bimg, x, y, (1 - distance / radius / .5), 0);

} catch (Exception e) {

}

}

}

}

(2)折射效果实现

通过背景图像图水泡中心的位置关系,计算简单的折射效果,代码如下:

public static void belbImage(BufferedImage image, BufferedImage bimg, int x_center, int y_center, int radius) {

double scale = radius * Math.PI / 2;

double x1 = 0;

double y1 = 0;

for (int y = y_center - radius; y < y_center + radius; y++) {

for (int x = x_center - radius; x < x_center + radius; x++) {

double distance = Math.sqrt((x - x_center) * (x - x_center) + (y - y_center) * (y - y_center));

if (distance > radius)

continue;

double hu = Math.asin(distance / radius);

double v = hu * scale * 4.14 / 180.0;

x1 = x_center + (x - x_center) * v;

y1 = y_center + (y - y_center) * v;

try {

if (x1 > image.getWidth())

x1 = image.getWidth() * 2 - x1;

else if (x1 < 0)

x1 = -x1;

if (y1 > image.getHeight())

y1 = image.getHeight() * 2 - y1;

else if (y1 < 0)

y1 = -y1;

bimg.setRGB(x, y, image.getRGB((int) x1, (int) y1));

} catch (Exception e) {

}

}

}

}

(3)两种效果的叠加

将两种效果进行叠加,就产生了水泡的效果,叠加代码如下:

public static BufferedImage drawBallImage(BufferedImage image, int x, int y, int radius) {

BufferedImage bimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = bimg.createGraphics();

g2.drawImage(image, 0, 0, null);

int r = radius, x_lamp, y_lamp;

x_lamp = x - (int) (r / 2.0);

y_lamp = y - (int) (r / 2.0);

belbImage(image, bimg, x, y, r);

drawBall(bimg, x, y, r, x_lamp, y_lamp);

g2 = bimg.createGraphics();

g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f));

g2.drawImage(image, 0, 0, null);

g2.dispose();

return bimg;

}

代码中使用到的辅助类Pixel代码如下:

import java.awt.image.*;

public class Pixel {

public int red;

public int green;

public int blue;

public int alpha=0xFF;

public double hue;

public double saturation;

public double luminosity;

private int rgb;

public Pixel() {

}

public void setRGB(int rgb) {

red = (rgb & 0x00FF0000) / 0x00010000;

green = (rgb & 0x0000FF00) / 0x00000100;

blue = rgb & 0x000000FF;

alpha = (rgb >> 24)&0x0ff;

this.rgb = rgb;

}

public int getRGB() {

rgb = alpha<<24 | 0x00010000 * red | 0x00000100 * green | blue;

return this.rgb;

}

public static void setRgb(BufferedImage image, int x, int y, int red, int green, int blue) {

int rgb = 0xFF000000 | red * 0x00010000 | green * 0x00000100 | blue;

image.setRGB(x, y, rgb);

}

public static int pixelIntensity(int rgb) {

int red = (rgb&0x00FF0000)/0x00010000;

int green = (rgb&0x0000FF00)/0x00000100;

int blue = rgb&0x000000FF;

return (int) (0.299 * red + 0.587 * green + 0.114 * blue + 0.5);

}

}

测试代码

本文上的效果图片使用了下面的测试代码:

public static void main(String[] argv) throws IOException {

BufferedImage img = read(new File("cat.jpg"));

BufferedImage img2 =drawBallImage(img, 100, 300, 80);

ImageIO.write(img2, "jpeg", new File("cat_belb.jpg"));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值