@@ -54,7 +54,7 @@ Ptr<cuda::BackgroundSubtractorFGD> cv::cuda::createBackgroundSubtractorFGD(const
5454#else
5555
5656#include " cuda/fgd.hpp"
57- #include " opencv2/imgproc/imgproc_c.h "
57+ #include " opencv2/imgproc.hpp "
5858
5959// ///////////////////////////////////////////////////////////////////////
6060// FGDParams
@@ -345,69 +345,35 @@ namespace
345345
346346namespace
347347{
348- void seqToContours (CvSeq* _ccontours, CvMemStorage* storage, OutputArrayOfArrays _contours)
349- {
350- Seq<CvSeq*> all_contours (cvTreeToNodeSeq (_ccontours, sizeof (CvSeq), storage));
351-
352- size_t total = all_contours.size ();
353-
354- _contours.create ((int ) total, 1 , 0 , -1 , true );
355-
356- SeqIterator<CvSeq*> it = all_contours.begin ();
357- for (size_t i = 0 ; i < total; ++i, ++it)
358- {
359- CvSeq* c = *it;
360- ((CvContour*)c)->color = (int )i;
361- _contours.create ((int )c->total , 1 , CV_32SC2, (int )i, true );
362- Mat ci = _contours.getMat ((int )i);
363- CV_Assert ( ci.isContinuous () );
364- cvCvtSeqToArray (c, ci.data );
365- }
366- }
367-
368348 int findForegroundRegions (GpuMat& d_foreground, Mat& h_foreground, std::vector< std::vector<Point> >& foreground_regions,
369- CvMemStorage* storage, const FGDParams& params)
349+ const FGDParams& params)
370350 {
371351 int region_count = 0 ;
372352
373353 // Discard under-size foreground regions:
374354
375355 d_foreground.download (h_foreground);
376- IplImage ipl_foreground = cvIplImage (h_foreground);
377- CvSeq* first_seq = 0 ;
378356
379- cvFindContours (&ipl_foreground, storage, &first_seq, sizeof (CvContour), CV_RETR_LIST);
357+ int mode = RETR_LIST;
358+ if (params.is_obj_without_holes ) mode |= RETR_EXTERNAL;
359+ findContours (h_foreground, foreground_regions, mode, CHAIN_APPROX_NONE);
380360
381- for (CvSeq* seq = first_seq; seq; seq = seq-> h_next )
361+ for (size_t i = 0 ; i < foreground_regions. size (); ++i )
382362 {
383- CvContour* cnt = reinterpret_cast <CvContour*>(seq);
363+ const std::vector<Point> &cnt = foreground_regions[i];
364+ const Rect rect = boundingRect (cnt);
384365
385- if (cnt-> rect .width * cnt-> rect .height < params.minArea || (params. is_obj_without_holes && CV_IS_SEQ_HOLE (seq)) )
366+ if (rect.width * rect.height < params.minArea )
386367 {
387368 // Delete under-size contour:
388- CvSeq* prev_seq = seq->h_prev ;
389- if (prev_seq)
390- {
391- prev_seq->h_next = seq->h_next ;
392-
393- if (seq->h_next )
394- seq->h_next ->h_prev = prev_seq;
395- }
396- else
397- {
398- first_seq = seq->h_next ;
399-
400- if (seq->h_next )
401- seq->h_next ->h_prev = NULL ;
402- }
403369 }
404370 else
405371 {
406- region_count++;
372+ foreground_regions[ region_count++] = foreground_regions[i] ;
407373 }
408374 }
409375
410- seqToContours (first_seq, storage, foreground_regions);
376+ foreground_regions. resize (region_count );
411377 h_foreground.setTo (0 );
412378
413379 drawContours (h_foreground, foreground_regions, -1 , Scalar::all (255 ), -1 );
@@ -612,19 +578,14 @@ namespace
612578 Ptr<cuda::Filter> dilateFilter_;
613579 Ptr<cuda::Filter> erodeFilter_;
614580#endif
615-
616- CvMemStorage* storage_;
617581 };
618582
619583 FGDImpl::FGDImpl (const FGDParams& params) : params_(params), frameSize_(0 , 0 )
620584 {
621- storage_ = cvCreateMemStorage ();
622- CV_Assert ( storage_ != 0 );
623585 }
624586
625587 FGDImpl::~FGDImpl ()
626588 {
627- cvReleaseMemStorage (&storage_);
628589 }
629590
630591 void FGDImpl::apply (InputArray _frame, OutputArray fgmask, double )
@@ -640,7 +601,6 @@ namespace
640601 CV_Assert ( curFrame.type () == CV_8UC3 || curFrame.type () == CV_8UC4 );
641602 CV_Assert ( curFrame.size () == prevFrame_.size () );
642603
643- cvClearMemStorage (storage_);
644604 foreground_regions_.clear ();
645605 foreground_.setTo (Scalar::all (0 ));
646606
@@ -655,7 +615,7 @@ namespace
655615#endif
656616
657617 if (params_.minArea > 0 || params_.is_obj_without_holes )
658- findForegroundRegions (foreground_, h_foreground_, foreground_regions_, storage_, params_);
618+ findForegroundRegions (foreground_, h_foreground_, foreground_regions_, params_);
659619
660620 // Check ALL BG update condition:
661621 const double BGFG_FGD_BG_UPDATE_TRESH = 0.5 ;
0 commit comments