0

I want to subtract green background from webcam video. bellow shows how I get the video from webcam

public static void main(String[] args) {

    CvCapture capture =cvCreateCameraCapture(CV_CAP_ANY);  //
    IplImage frame;

    IplImage grayimg = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);

    cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);

    for(;;)
    {
        frame = cvQueryFrame(capture);


        if(frame == null) 
            {
                System.out.println("ERROR: NO Video File"); 
                break;
            }

        cvShowImage("Video",hsvThreshold(frame));
        char c = (char) cvWaitKey(30);

        if(c==27) break;
    }

    cvReleaseCapture(capture);
    cvDestroyWindow("Video");
}

My Image Mask is retun IplImage of hsvThreshold(frame) this function.At that time original frame is frame

so I want to extrac the mask hsvThreshold(frame) is like this

     public static IplImage hsvThreshold(IplImage orgImg) {
    // 8-bit, 3- color =(RGB)
    IplImage imgHSV = cvCreateImage(cvGetSize(orgImg), 8, 3);
    System.out.println(cvGetSize(orgImg));
    cvCvtColor(orgImg, imgHSV, CV_BGR2HSV);
    // 8-bit 1- color = monochrome
    IplImage imgThreshold = cvCreateImage(cvGetSize(orgImg), 8, 1);

    cvInRangeS(imgHSV, cvScalar(70, 50, 50, 0), cvScalar(150, 200, 200, 0), imgThreshold);
    cvReleaseImage(imgHSV);
    cvSmooth(imgThreshold, imgThreshold, CV_GAUSSIAN, 13);
    cvNot(imgThreshold,imgThreshold);
    return imgThreshold;
}
Suravi
  • 301
  • 1
  • 7
  • 21
  • What is your question exactly? Do you get an error message? Is the mask not as expected? – diip_thomas Jul 24 '14 at 09:15
  • I want find a method to subtract the mask from the original image ? Mask is returned in the imgThreshold and original image is frame – Suravi Jul 24 '14 at 09:30

1 Answers1

1

If you want to copy your image without the pixels in the mask you can use the function cvCopy(origImage,destImage,mask);. See this SO post for more info.

I do recommend you to switch to the C++ API though, it is much easier to understand and less concern about releasing images and memory leaks.

EDIT: You can also copy one image onto another without cvCopy. You can fid the code for that in this post: Copying a portion of an IplImage into another Iplimage (that is of same size is the source)

Community
  • 1
  • 1
diip_thomas
  • 1,531
  • 13
  • 25
  • My originalFrame and Mask type is IplImage and originalFrame channel 3 and mask channel 1.so cvcopy is not working – Suravi Jul 24 '14 at 10:08
  • A mask is supposed to be only 1 channel and a binary image. Your original and destination images need to be the same amount of channels. Did you create destImage with the function `cvCreateImage()` first? – diip_thomas Jul 24 '14 at 11:09
  • Yes.I want to get in resulting image with background transpaternt...is it possible – Suravi Jul 24 '14 at 11:28
  • If you want to copy one image with a mask on another existing image you might have to set an ROI with `cvSetImageROI`. Here you can find the documentation: http://docs.opencv.org/trunk/modules/core/doc/old_basic_structures.html#setimageroi Again this is much simpler using the new C++ API. – diip_thomas Jul 24 '14 at 13:03