how to perform detection and segmentation of blobs using opencv

229 views
Skip to first unread message

Ravi Teja Alluri

unread,
Mar 26, 2014, 10:16:24 AM3/26/14
to jav...@googlegroups.com
Hi,
I am working on motion detection using Java CV using background/foreground segmentation.

how can i get the foreground image segmented and get the foreground image using contours and also i need to do detection and segmentation so that i can get the blob identified out of the foregroundimage.

public static void main(String args[])
{
 Loader.load(opencv_objdetect.class);
   contourStorage = CvMemStorage.create();
   System.out.println("Initializing frame grabber...");
   OpenCVFrameGrabber grabber = new OpenCVFrameGrabber("D:/motiondetection/motion videos/check1.mp4");//"D:/OpenCV Data/videos/realtimetracking.mp4"
   grabber.start();
   IplImage grab = grabber.grab();
   int width  = grab.width();
   int height = grab.height();

   IplImage fgMask = IplImage.create(width, height, IPL_DEPTH_8U, 1);

   IplImage background = IplImage.create(width, height, IPL_DEPTH_8U, 3);

   CanvasFrame grabCanvas = new CanvasFrame("Video");
   grabCanvas.setLocation(0, 0);
   CanvasFrame mogCanvas = new CanvasFrame("MOG");
   mogCanvas.setLocation(0, 0);

   BackgroundSubtractorMOG2 mog = new BackgroundSubtractorMOG2(3000,16,false); //frame history ,distance measure of each Gaussian cluster, shadow 3000 16
   mog.set("nmixtures",3);   //3 PARAMETERS ELIMINATING SHADOWS apply() 
   
/*    System.out.println("MOG num. mixtures: " + mog.getInt("nmixtures"));
   System.out.println("MOG shadow detection: " + mog.getBool("detectShadows"));
   try 
   {
     System.out.println("MOG background ratio: " + mog.getDouble("backgroundRatio"));
     System.out.println("MOG var threshold gen: " + mog.getDouble("varThresholdGen"));
     System.out.println("MOG fVar init: " + mog.getDouble("fVarInit") + ", min: " + mog.getDouble("fVarMin") + ", max: " + mog.getDouble("fVarMax") );
   }
   catch (RuntimeException e)
   { 
    e.printStackTrace();
    System.out.println("Inside of catch block");
   }*/

   while (grabCanvas.isVisible() && mogCanvas.isVisible())
   {
     long startTime = System.currentTimeMillis();
     
     grab = grabber.grab();
     if (grab == null)
     {
       System.out.println("Image grab failed");
       break;
     }
     IplImage grabcopy=grab.clone();
     mog.apply(grab, fgMask,0.00005);  // -1);   //learning rate ???  //grab, fgMask,0.00000001
     mog.getBackgroundImage(background);
     // cvCanny(fgMask, fgMask, 80, 100, null);  for Edges 
     cvErode(fgMask, fgMask, null, 5);
     cvDilate(fgMask, fgMask, null, 5);
     cvSmooth(fgMask, fgMask, CV_BLUR, 5);  
     cvThreshold(fgMask, fgMask, 0, 255, CV_THRESH_BINARY);
     opencv_highgui.cvShowImage("fgMask", fgMask);
     opencv_highgui.cvWaitKey(10);
     
      CvMemStorage storage=CvMemStorage.create();
       CvSeq contours = new CvSeq();
       CvSeq ptr = new CvSeq();
       cvFindContours(fgMask, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
       Random rand = new Random();
       for (ptr = contours; ptr != null; ptr = ptr.h_next())
       {
           Color randomColor = new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat());
           CvScalar color = CV_RGB( randomColor.getRed(), randomColor.getGreen(), randomColor.getBlue());
           cvDrawContours(grabcopy, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
       }
       mogCanvas.showImage(grabcopy);
}

can anyone suggest me with the way i need to do detection and segmentation of image using opencv.

Thanks in advance.

Samuel Audet

unread,
Mar 30, 2014, 8:54:26 AM3/30/14
to jav...@googlegroups.com
On 03/26/2014 11:16 PM, Ravi Teja Alluri wrote:
> Hi,
> I am working on motion detection using Java CV using
> background/foreground segmentation.
>
> how can i get the foreground image segmented and get the foreground
> image using contours and also i need to do detection and segmentation so
> that i can get the blob identified out of the foregroundimage.

I'm not sure I understand the question. Can you provide an example of
what you are trying to achieve? Are you still trying to make something
like this video?
https://www.youtube.com/watch?v=lzRGp3nc8Tw

If so, you'll need to adjust the parameters of the algorithms to make
them work well with your camera and your environment...

Samuel
Reply all
Reply to author
Forward
0 new messages