Webcam Images and Transparency
Is that anyone know how to remove the images background to transparency??
The ideas I use is after click capture the images had been taken will just focus on the people faces and all the background will be transparent.
I had been success create a webcam code and display the images but I failed to remove the images background~~is that anyone can help me and gave me some hint???
How to turn the background to transparent???
Code:
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.Buffer;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
public class Webcaming extends JFrame {
private JPanel display = null;
private JButton capturing = new JButton("Images Capture");
private Player player = null;
private Buffer buf = null;
private Image img = null;
private BufferToImage btoi = null;
public Webcaming(){
webCamComponent();
}//end WebCamTest()
private void webCamComponent(){
setLayout(new GridLayout(0,3,5,5));
JFrame jfmDisplay = new JFrame("Display");
JPanel displaying = new JPanel();
Container details = jfmDisplay.getContentPane();
jfmDisplay.setSize(1000,1000);
details.add(displaying);
jfmDisplay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel show = new JPanel();
show.setSize(400, 400);
display = new JPanel();
show.add(display, BorderLayout.CENTER);
add(show, BorderLayout.CENTER);
getContentPane().add(display, BorderLayout.CENTER);
JPanel buttonCap = new JPanel();
//buttonCap.setLayout(new GridLayout(0,3,5,5));
buttonCap.add(capturing, BorderLayout.NORTH);
add(buttonCap, BorderLayout.CENTER);
capturing.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent capBtn){
jButtonCapActionPerformed(capBtn);
}
});
pack();
setVisible(true);
}//end webCamComponent
private void jButtonCapActionPerformed(ActionEvent capBtn){
if(capBtn.getActionCommand().equals("Images Capture")){
JFrame jfmWebcam = new JFrame("Webcam");
jfmWebcam.setSize(320, 320);
JButton capture = new JButton("Capture");
jfmWebcam.add(capture, BorderLayout.NORTH);
//Button Event
capture.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent captureBtn){
jButtonCaptureActionPerformed(captureBtn);
}//end actionPerformed
});
try{
MediaLocator webcamera = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(webcamera);
if(player.getVisualComponent()!=null){
jfmWebcam.getContentPane().add(player.getVisualComponent());
}//end if
player.start();
jfmWebcam.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
jfmWebcam.pack();
jfmWebcam.setVisible(true);
}//end try
catch (Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}//end catch
}//end if
}//end jButtonCapActionPerformed
private void jButtonCaptureActionPerformed(ActionEvent captureBtn){
if(captureBtn.getActionCommand().equals("Capture")){
if(player != null){
FrameGrabbingControl fgcFrame = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgcFrame.grabFrame();
//convert it to an image
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
img = new ImageIcon(img).getImage();
//display.setIconImage(img);
}//end if
}//end if
}//end jButtonCaptureActionPerformed
public void paint(Graphics g){
g.drawImage(img, 0, 0, null);
g.dispose();
}
public static void main(String[] args) {
Webcaming wct = new Webcaming();
}//end main
}//end class
Re: Webcam Images and Transparency
So you've captured the image, and have you identified which parts of the image you want to keep?
If so, and the image type allows for Alpha then set the alpha for all pixels that aren't your face.
Re: Webcam Images and Transparency
emmm~~any example??
I have captured the image but how shd I identified the part of the image???
Re: Webcam Images and Transparency
Oh.
Read up on facial recognition?
Google might throw up some useable packages for Java.