import java.applet.*; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; public class hw1_schoen extends Applet { BufferedImage img; public int t1=20; public int t2=30; public int i,j; public void init() { // Load image // image = getImage(getCodeBase(),"me.jpg"); // imgi=new ImageIcon(image); // resize(imgi.getIconWidth(),imgi.getIconHeight()); try { File f = new File("C:\\Users\\Matt\\workspace\\facedetect\\bin\\me.jpg"); img=ImageIO.read(f); // //File w = new File("C:\\Users\\Matt\\workspace\\facedetect\\bin\\proc3.jpg"); // //BufferedImage img1=colorImage(30,getSkinColor(30,img),img); // //ImageIO.write(img1, "jpeg", w); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("blah"); } resize(img.getWidth(),img.getHeight()); } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { // Draw image //Graphics2D g2 = (Graphics2D)g; //g.drawImage(img, 0, 0, null); //g.setColor(Color.black); //g.drawImage(colorImage(20,getSkinColor(30,img),img),0,0,this); //g.setFont(new Font("Helvetica",Font.ITALIC,600)); //g.fillRect(10, 10, 50, 50); for(i=15;i<45;i++) { try { File f = new File("C:\\Users\\Matt\\workspace\\facedetect\\bin\\me.jpg"); img=ImageIO.read(f); } catch (IOException e) { System.out.println("blah"); } g.drawImage(colorImage(i,getSkinColor(30,img),img),0,0,this); g.setColor(Color.white); //g.setColor(Color.black); g.drawString("Color Threshold: "+i, 10, 12); for(int j=0;j<5000;j++) { j++; j--; } //g.drawString("Color Threshold: "+j, 10, 24); //System.out.println("thresh1="+j+" thresh2="+i); } } public BufferedImage colorImage(int thresh1, Color skin, BufferedImage image) { for(int i=0;ithresh1)&&(Math.abs(col.getGreen()-skin.getGreen())>thresh1)&&(Math.abs(col.getBlue()-skin.getBlue())>thresh1)) { image.setRGB(i,j,Color.black.getRGB()); } } } return image; } public Color getSkinColor(int thresh, BufferedImage image) { int height=img.getHeight(); int width=img.getWidth(); int posx=(width/2)-2; int posy=(height/2)-2; //int[][] pixels = new int[5][5]; Color max=Color.black; Color avg=Color.black; for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { //pixels[i][j]=img.getRGB(posx,posy); Color col=new Color(img.getRGB(posx,posy)); if(col.getRed()>max.getRed()) max= new Color(col.getRed(),max.getGreen(),max.getBlue()); if(col.getGreen()>max.getGreen()) max= new Color(max.getRed(),col.getGreen(),max.getBlue()); if(col.getBlue()>max.getBlue()) max= new Color(max.getRed(),max.getGreen(),col.getBlue()); posy++; } posx++; } for(int i=0;i<5;i++) { for(int j=0;j<5;j++) { Color col=new Color(img.getRGB(posx,posy)); if((max.getRed()-col.getRed()<=thresh)&&(max.getGreen()-col.getGreen()<=thresh)&&(max.getBlue()-col.getBlue()<=thresh)) { if(avg.equals(Color.black)) avg=col; else avg=new Color((avg.getRed()+col.getRed())/2,(avg.getGreen()+col.getGreen())/2,(avg.getBlue()+col.getBlue())/2); } posy++; } posx++; } return avg; } }