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.
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.