Our Feeds

Sunday, 4 December 2016

AJITH KP

Skin Detection Algorithm - Implementation in Java

Hi GuyZ,,,
          This is one of the partial solution of my Mini Project. This is an implementation of skin detection algorithm in Java. The algorithm implemented can be found in http://kilyos.ee.bilkent.edu.tr/~signal/defevent/papers/cr1214.pdf, https://arxiv.org/ftp/arxiv/papers/1008/1008.4206.pdf
          Three algorithms implemented in this code are,
  • RGB skin cluster
  • YCbCr skin cluster
  • HSV skin cluster
Skin detection algorithm implementation
Original Image

Skin detection algorithm implementation
After Process
Skin detection algorithm implementation
Original
Skin detection algorithm implementation
After Process


If you like this blog post, please share with your friends... Help us to grow...

Source Code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author Ajith Kp [fb.com/ajithkp560]
 * (c) _TERMINAL_CODERS_ (c) http://www.terminalcoders.blogspot.com
 */
public class SkinDetection extends JFrame{
    JButton openB = new JButton("Open");
    JPanel imagePanel = new JPanel();
    JPanel menuPanel = new JPanel();
    SkinDetection(){
        super("Skin Detection Application");
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        setMaximizedBounds(env.getMaximumWindowBounds());
        setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
        openB.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(SkinDetection.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        File imgf = fc.getSelectedFile();
                        imagePanel.removeAll();
                        setPic(imgf);
                    } catch (IOException ex) {
                    }
                }
            }
        });
        menuPanel.add(openB);
        add(menuPanel, BorderLayout.NORTH);
        add(imagePanel, BorderLayout.CENTER);
    }
    int max(int r, int g, int b){
        if(r>g && r>b)
            return r;
        if(g>r && g>b)
            return g;
        return b;
    }
    int min(int r, int g, int b){
        if(r<g && r<b)
            return r;
        if(g<r && g<b)
            return g;
        return b;
    }
    boolean isSkin(int r, int g, int b){
        if(r>95 && g>40 && b>20){
            if((max(r,g,b)-min(r,g,b))>15){
                if(Math.abs(r-g)>15 && r>g && r>b){
                    return true;
                }
            }
        }
        return false;
    }
    boolean YCbCr(int r, int g, int b){
        double Y = (0.257*r)+(0.504*g)+(0.098*b)+16; 
        double Cb = -(0.148*r)-(0.291*g)+(0.439*b)+128;
        double Cr = (0.439*r) - (0.368*g) - (0.071*b) + 128;
        
        if (Y > 80 && (Cb>85 && Cb<135) && (Cr>135 && Cr < 180))
            return true;
        return false;
    }
    boolean HSI(int r, int g, int b){
        int mx = max(r,g,b);
        int mn = min(r,g,b);
        double d = mx-mn;
        double h = 0;
        if(mx==r)
            h = (g-b)/d;
        else if(mx==g)
            h = 2+(b-r)/d;
        else
            h = 4+(r-g)/d;
        h = h*60;
        if(h<0)
            h+=360;
        if(h>4 && h<45)
            return true;
        return false;
    }
    public void setPic(File imgf) throws IOException
    {
        BufferedImage myImg = ImageIO.read(imgf);
        int w = myImg.getWidth();
        int h = myImg.getHeight();
        
        int r, g, b;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                Color c = new Color(myImg.getRGB(i, j));
                r=c.getRed();
                g=c.getGreen();
                b=c.getBlue();
                if(isSkin(r,g,b) && YCbCr(r,g,b) && HSI(r,g,b)){
                    //The skin detected
                }
                else{
                    myImg.setRGB(i, j, new Color(0, 0, 0, 0).getRGB());
                    //Set black color if not a skin part
                }
            }
        }
        JLabel picLabel = new JLabel(new ImageIcon(myImg));
        imagePanel.add(picLabel);
        imagePanel.revalidate();
        imagePanel.repaint();
    }
    public static void main(String[] args) {
        SkinDetection frm=new SkinDetection();
        frm.setSize(700, 500);
        frm.setVisible(true);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

6 comments

Write comments
28 December 2016 at 22:22 delete

Informative post of Skin Detection Algorithm - Implementation in Java.

Mcx Commodity Tips Free Trial

Reply
avatar
Anonymous
AUTHOR
9 February 2017 at 00:53 delete

Thanks for code. I was seeking code for skin detection.

Reply
avatar
Anonymous
AUTHOR
13 February 2017 at 07:44 delete

I'm looking for skin detection algorithm.

Reply
avatar
Anonymous
AUTHOR
14 February 2017 at 06:03 delete

Thanks for java code

Reply
avatar
Anonymous
AUTHOR
2 April 2017 at 09:09 delete

Thanks for code

Reply
avatar
22 September 2017 at 02:21 delete This comment has been removed by a blog administrator.
avatar