处理透明度是否支持删除背景?

时间:2020-06-15 01:31:18

标签: java image image-processing processing

enter image description here

我有一个绿色背景的图像。

我使用此功能删除了背景。

public void removeBackground(PImage img, color c, float thres) {
  colorMode(HSB);
  for(int i = 0; i < img.width; i++){
    for(int j = 0; j < img.height; j++){
      color cp = img.get(i, j);
      //threshold accounts for compression
      if(abs(hue(cp) - hue(c)) < thres){
        img.set(i, j, color(0,0,0,0));
      }
    }
  }
}

使用这些参数,我尝试删除背景。

PImage FBSprite;
public void settings() {
  size(500, 500);
}

void setup(){
  FBSprite = loadImage("FlappyBirdSprite.jpg");
  FBSprite.resize(50, 0);
  removeBackground(FBSprite, FBSprite.get(0,0), 0.5);
  image(FBSprite, 0, 0);
}

void draw(){
  background(200);
  image(FBSprite, 0, 0);
}

背景变为黑色而不是透明。

ARGB结果: enter image description here

HSB结果为1个alpha: enter image description here

2 个答案:

答案 0 :(得分:2)

您会喜欢的:更改此行

colorMode(HSB);

为此:

colorMode(ARGB);

它应该可以解决问题。刚刚发生了什么?除非您需要在那个非常特定的时间点在HSB中工作,否则没有任何意义。 ARGB与RGB一样,但是具有 ALPHA 设置,或者根据需要设置透明度。自从您这样设置color(0,0,0,0)以来,您似乎已经准备好在ARGB中工作,因此这可能是一个copy and paste programming问题。也许您只是没有注意到。它一直在我身上发生。但是,如果这是第一件事,请尝试在将来密切注意此类问题。除了所有人都知道的意大利面条式代码外,大多数人都不会谈论其他不良的设计模式,但是cut and paste programming是真实的,而且会带来很多伤害,特别是如果您使用的是内部平台且文档不存在。并非发生在我身上。

玩得开心!

答案 1 :(得分:0)

我没有将要删除的像素的alpha设置为0,而是将alpha设置为1。alpha太小了,您看不到边框。