gpt4 book ai didi

java - 在图像上查找透明像素并使相同像素在另一个图像上透明

转载 作者:行者123 更新时间:2023-12-04 03:30:35 25 4
gpt4 key购买 nike

我有两张图片。第二个是对第一个蒙版应用某种掩码的结果。我需要的是获得该蒙版并能够将其应用于其他图像。这是两张图片:normal , tattered .

如您所见,第二个有破烂的边缘,它们是透明的,不是白色的。(还有某种模糊发生,如果有办法我可以找出它到底是什么模糊,那就太好了,但这里真的没有必要)

我需要的是能够从第一个图像创建第二个图像。

从理论上讲,我应该创建一个 mask - 一个具有任何颜色的相同大小的图像,每个像素的透明度为 0 或 255,具体取决于上面第二张图像中相同像素的透明度值。然后我可以将任何输入图像的像素的 alpha 值设置为该掩码的 alpha 值。

但是,我不知道如何实际操作。我在 java 中使用 BufferedImage 尝试过它,但是它不起作用。当我尝试从所选像素的颜色中获取 Alpha 时,它始终为 255,即使对于应该透明的像素也是如此。我确实设法在 Processing 中获得了 alpha 值(它们实际上不仅仅是 0 或 255,中间有很多值),但是,当我尝试将此值应用于新图像并保存时,它保存为完全不透明的图像,当我加载它,alpha值都是255。

  PImage mask = loadImage("some/path/1.png");
PImage img = loadImage("some/path/2.png");

img.loadPixels();
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
color maskColor = mask.get(x, y);
if (alpha(maskColor) < 255) {
color imgColor = img.get(x, y);
img.pixels[y*img.width + x] = color(red(imgColor), green(imgColor), blue(imgColor), alpha(maskColor));
}
}
}
img.updatePixels();
img.save("some/path/3.png");

最佳答案

您也可以将破烂的图像用作其他图像的 mask 。您只需要掩码中的 alpha 信息。
使用 BufferedImage 创建破烂边框的实现:

public class Test {

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

BufferedImage mask = loadImage("e:\\mask.png");
BufferedImage img = loadImage("e:\\1.png");

int width = mask.getWidth();
int height = mask.getHeight();

BufferedImage processed = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);

for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int rgb = mask.getRGB(x,y);
int maskAlpha = alpha(rgb);
int imgColor = img.getRGB(x, y);
if (maskAlpha < 255) {
processed.setRGB(x,y,maskAlpha(imgColor, maskAlpha));
} else {
processed.setRGB(x,y,imgColor);
}
}
}

writeImage(processed, "e:\\2.png");
}

static BufferedImage loadImage(String imagePath) {
File file = new File(imagePath);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}

static void writeImage(BufferedImage img,String filePath){
String format = filePath.substring(filePath.indexOf('.')+1);
//Get Picture Format
System.out.println(format);
try {
ImageIO.write(img,format,new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
}

static int maskAlpha(int rgb, int alpha) {
//strip alpha from original color
int color = (0x00ffffff&rgb);
return color + ((alpha)<<24);
}

static int alpha(int rgb) {
return (0xff&(rgb>>24));
}

static int red(int rgb) {
return (0xff&rgb);
}
static int green(int rgb) {
return (0xff&(rgb>>8));
}
static int blue(int rgb) {
return (0xff&(rgb>>16));
}
}

在这里BufferedImage.TYPE_4BYTE_ABGR意味着

Represents an image with 8-bit RGBA color components with the colorsBlue, Green, and Red stored in 3 bytes and 1 byte of alpha. The imagehas a ComponentColorModel with alpha. The color data in this image isconsidered not to be premultiplied with alpha. The byte data isinterleaved in a single byte array in the order A, B, G, R from lowerto higher byte addresses within each pixel.

也就是说颜色整数是32位的,在java中是按照abgr的顺序存储的,即alpha是前8位,r是后8位,所以可以得到argb值如下:

int r = (0xff&rgb);
int g = (0xff&(rgb>>8));
int b = (0xff&(rgb>>16));
int a = (0xff&(rgb>>24));

关于java - 在图像上查找透明像素并使相同像素在另一个图像上透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66943187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com