0

I have a code in Java that detects my gloved hand (I wear a black glove) and moves the cursor according to my hand movements. But the issue is that it detects all the black objects in the background too. I want to use background subtraction technique to isolate my hand. I have a VideoCapture handle called webcam. Inside a while loop it retrieves frames from the video and passes these frames as parameters to other methods to perform contour detection etc. I want to add background subtraction method as well. I looked up a few answers but I couldn't find anything that helped me. And do I need to install opencv processing libraries for this (background subtraction) ? If yes, how do I do that?

I'm new to Java and OpenCV.

I'm posting the main method:

public static void main(String[] args) throws InterruptedException, AWTException {

    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    video v=new video();
    VideoCapture webcam=new VideoCapture(0);
    webcam.set(Videoio.CV_CAP_PROP_FRAME_HEIGHT,768);
    webcam.set(Videoio.CV_CAP_PROP_FRAME_WIDTH,1024);
    v.setframe(webcam);
    Robot r=new Robot();
    Mat frame = new Mat();
    Mat modification=new Mat();
    Point center=new Point();   
    Point finger=new Point();           
    List<Point> buffer=new LinkedList<Point>();
    List<Point> bufferfingers=new LinkedList<Point>();
    List<Point> fingers=new LinkedList<Point>();    
    long temp=0;


    while(true && !close){

        if(!webcam.isOpened() && !close){
            System.out.println("Camera Error");
        }
        else{
            List<Point> defects=new LinkedList<Point>();    
            if(!close){
                temp=System.currentTimeMillis();
                webcam.retrieve(frame);     
                modification = v.filtromorfologic(2, 7, v.filtrocolorhsv(0, 0, 0, 180, 255, 40,frame));

                defects=v.envelopdefects(frame,v.searchcontours(frame, modification, false, false, 450), false, 5); 

                if(buffer.size()<7){
                    buffer.add(v.centrpalm(frame, defects));
                }
                else
                {
                    center=v.filtrmediamobile(buffer, v.centrpalm(frame,defects));
                }

                fingers=v.fingers(frame, v.listcontours(modification, 200), center);

                if(finger.size()==1 && bufferfingers.size()<5){
                    bufferfingers.add(fingers.get(0));
                    finger=fingers.get(0);
                }
                else
                {
                    if(fingers.size()==1){
                        finger=v.filtrmediamobile(bufferfingers, fingers.get(0)); 

                    }
                }

                v.drawfingercenterpalm(frame, center, finger, fingers);



                v.mousetrack(fingers,finger,center, r,true,frame,temp);

                v.frametolabel(frame);

            }
        }

    }


}

}

I want to know how to do the background subtraction method and where in the main method to call it.

NeonBlue
  • 1
  • 2

0 Answers0