2013-05-12 48 views
1

調整圖像的飽和度我有以下方法,該方法的像素數據的陣列,然後調整到飽和目前this.adjustmentAmount它的工作原理!但我想通過只調整這些顏色的飽和度來進一步考慮:
red, green, blue, yellow, cyan, magenta通過顏色類型

這怎麼辦?

public int[] filter(String keep){ 
    for(int i = 0; i < this.pixels.length; i++){ 
     int pixel = this.pixels[i]; 
     int red = Colors.red(pixel); 
     int green = Colors.green(pixel); 
     int blue = Colors.blue(pixel); 

     float[] hsv = new float[3]; 
     Color.RGBtoHSB(red, green, blue, hsv); 
     hsv[1] += (float)(this.adjustmentAmount * 0.01); 
     if(hsv[1] > 1){ 
      hsv[1] = 1; 
     }else if(hsv[1] < 0){ 
      hsv[1] = 0; 
     } 

     int newpixel = Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]); 



     red = Colors.red(newpixel); 
     green = Colors.green(newpixel); 
     blue = Colors.blue(newpixel); 

     this.pixels[i] = Colors.rgba(red, green, blue); 
    } 
    return this.pixels; 
} 

這裏是一個工作SSCCE(只需修改文件名):

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package sscce; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

/** 
* 
* @author Ryan 
*/ 
public class Sscce extends JFrame{ 

    public Layer layer; 

    public Sscce(){ 
    try{ 
      this.setSize(800, 600); 
      this.setResizable(false); 
      this.setLocationRelativeTo(null); 
      this.setVisible(true); 
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      layer = new Layer(); 
      layer.setImage("http://www.prikol.ru/wp-content/gallery/october-2012/datacenter-01.jpg"); 
      this.add(layer); 
      this.revalidate(); 
      Timer timer = new Timer(); 
      timer.schedule(new TimerTask(){ 
       @Override 
       public void run(){ 
        applySat(); 
       } 
      }, 500); 
     }catch(IOException ex){ 
      Logger.getLogger(Sscce.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    protected void applySat(){ 
     Saturation sat = new Saturation(); 
     sat.setAmount(-100); 
     BufferedImage img = this.layer.getImage(); 
     int[] pixels = new int[img.getWidth() * img.getHeight()]; 
     this.layer.getImage().getRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth()); 
     sat.setPixels(pixels); 
     pixels = sat.filter("yellow"); 
     img.setRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth()); 
     this.layer.setImage(img); 
    } 
    public static void main(String[] args){ 
     new Sscce(); 
    } 
} 


class Layer extends JPanel{ 

    protected BufferedImage image; 

    public BufferedImage getImage(){ 
     return this.image; 
    } 

    public void setImage(BufferedImage image){ 
     this.image = image; 
     this.repaint(); 
    } 

    public void setImage(String filename) throws IOException{ 
     this.image = ImageIO.read(new URL(filename)); 
     this.setSize(this.image.getWidth(), this.image.getHeight()); 
     this.repaint(); 
    } 

    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.drawImage(this.image, 0, 0, this.image.getWidth(), this.image.getHeight(), Color.black, null); 
    } 
} 



class Saturation{ 

    protected volatile int[] pixels; 
    protected int adjustmentAmount = 0; 

    public void setPixels(int[] pixels){ 
     this.pixels = pixels; 
    } 

    public void setAmount(int amount){ 
     this.adjustmentAmount = amount; 
    } 

    public int[] filter(String keep){ 
     for(int i = 0; i < this.pixels.length; i++){ 
      int pixel = this.pixels[i]; 
      int red = Colors.red(pixel); 
      int green = Colors.green(pixel); 
      int blue = Colors.blue(pixel); 

      float[] hsv = new float[3]; 
      Color.RGBtoHSB(red, green, blue, hsv); 
      hsv[1] += (float)(this.adjustmentAmount * 0.01); 
      if(hsv[1] > 1){ 
       hsv[1] = 1; 
      }else if(hsv[1] < 0){ 
       hsv[1] = 0; 
      } 

      int newpixel = Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]); 
      red = Colors.red(newpixel); 
      green = Colors.green(newpixel); 
      blue = Colors.blue(newpixel); 

      this.pixels[i] = Colors.rgba(red, green, blue); 
     } 
     return this.pixels; 
    } 
} 


class Colors{ 

    public static int rgba(int red, int green, int blue, Integer alpha){ 
     int rgba = alpha; 
     rgba = (rgba << 8) + red; 
     rgba = (rgba << 8) + green; 
     rgba = (rgba << 8) + blue; 
     return rgba; 
    } 

    public static int rgba(int red, int green, int blue){ 
     int rgba = 255; 
     rgba = (rgba << 8) + red; 
     rgba = (rgba << 8) + green; 
     rgba = (rgba << 8) + blue; 
     return rgba; 
    } 
    public static int[] getrgba(int color){ 
     int[] colors = new int[3]; 
     colors[0] = Colors.red(color); 
     colors[1] = Colors.green(color); 
     colors[2] = Colors.blue(color); 
     return colors; 
    } 

    public static int alpha(int color){ 
     return color >> 24 & 0x0FF; 
    } 

    public static int red(int color){ 
     return color >> 16 & 0x0FF; 
    } 

    public static int green(int color){ 
     return color >> 8 & 0x0FF; 
    } 

    public static int blue(int color){ 
     return color & 0x0FF; 
    } 
} 
+0

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-05-12 06:26:21

+0

@AndrewThompson好,我加入SSCCE – 2013-05-12 16:34:14

+0

'layer.setImage(「C:\\用戶\\瑞恩\\桌面\\谷歌服務器room5.jpg」);'熱鏈接到圖像或在代碼生成它,除非你希望我來你家看這個。 – 2013-05-12 16:36:27

回答

0

你應該對你所提到的顏色RGB通道設置的閾值。

例如默認的黃色顏色定義爲

RED: 255 
GREEN : 255 
BLUE : 0 

但你可能要接受有點暗者爲黃色。那麼你應該上述三個通道黃色設定的範圍上,如下:

RED : [240 - 255] 
GREEN : [240 - 255] 
BLUE : [0 - 30] 

你可以把這個方法對所有顏色,並決定調整飽和與否。

+0

好吧......我想我明白你在說什麼...... – 2013-05-12 01:11:43

+0

這裏是我如何捕捉黃色:'if(red> = 50 && green> = 50 && blue <= 100){'然後我將它移除設置飽和度爲'0' – 2013-05-12 19:43:24

+0

我認爲你說的範圍不屬於黃色,你可以看到http://www.colorpicker.com/ 它看起來像深藍色。 – 2013-05-12 20:40:02