diff --git a/samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp b/samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp index e0e53e370..33a8334c6 100644 --- a/samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp +++ b/samples/android/face-detection/jni/DetectionBasedTracker_jni.cpp @@ -26,7 +26,7 @@ public: Detector(detector) { LOGD("CascadeDetectorAdapter::Detect::Detect"); - CV_Assert(!detector.empty()); + CV_Assert(detector); } void detect(const cv::Mat &Image, std::vector &objects) @@ -57,11 +57,11 @@ struct DetectorAgregator mainDetector(_mainDetector), trackingDetector(_trackingDetector) { - CV_Assert(!_mainDetector.empty()); - CV_Assert(!_trackingDetector.empty()); + CV_Assert(_mainDetector); + CV_Assert(_trackingDetector); DetectionBasedTracker::Parameters DetectorParams; - tracker = new DetectionBasedTracker(mainDetector.ptr(), trackingDetector.ptr(), DetectorParams); + tracker = makePtr(mainDetector, trackingDetector, DetectorParams); } }; @@ -77,8 +77,10 @@ JNIEXPORT jlong JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker try { - cv::Ptr mainDetector = new CascadeDetectorAdapter(new CascadeClassifier(stdFileName)); - cv::Ptr trackingDetector = new CascadeDetectorAdapter(new CascadeClassifier(stdFileName)); + cv::Ptr mainDetector = makePtr( + makePtr(stdFileName)); + cv::Ptr trackingDetector = makePtr( + makePtr(stdFileName)); result = (jlong)new DetectorAgregator(mainDetector, trackingDetector); if (faceSize > 0) { diff --git a/samples/cpp/bagofwords_classification.cpp b/samples/cpp/bagofwords_classification.cpp index e24a770f8..4506e5b9d 100644 --- a/samples/cpp/bagofwords_classification.cpp +++ b/samples/cpp/bagofwords_classification.cpp @@ -2563,19 +2563,19 @@ int main(int argc, char** argv) Ptr featureDetector = FeatureDetector::create( ddmParams.detectorType ); Ptr descExtractor = DescriptorExtractor::create( ddmParams.descriptorType ); Ptr bowExtractor; - if( featureDetector.empty() || descExtractor.empty() ) + if( !featureDetector || !descExtractor ) { cout << "featureDetector or descExtractor was not created" << endl; return -1; } { Ptr descMatcher = DescriptorMatcher::create( ddmParams.matcherType ); - if( featureDetector.empty() || descExtractor.empty() || descMatcher.empty() ) + if( !featureDetector || !descExtractor || !descMatcher ) { cout << "descMatcher was not created" << endl; return -1; } - bowExtractor = new BOWImgDescriptorExtractor( descExtractor, descMatcher ); + bowExtractor = makePtr( descExtractor, descMatcher ); } // Print configuration to screen diff --git a/samples/cpp/bgfg_gmg.cpp b/samples/cpp/bgfg_gmg.cpp index ea1df98d0..226eea463 100644 --- a/samples/cpp/bgfg_gmg.cpp +++ b/samples/cpp/bgfg_gmg.cpp @@ -35,7 +35,7 @@ int main(int argc, char** argv) setNumThreads(8); Ptr fgbg = createBackgroundSubtractorGMG(20, 0.7); - if (fgbg.empty()) + if (!fgbg) { std::cerr << "Failed to create BackgroundSubtractor.GMG Algorithm." << std::endl; return -1; diff --git a/samples/cpp/dbt_face_detection.cpp b/samples/cpp/dbt_face_detection.cpp index c2e6d0a81..651eaff73 100644 --- a/samples/cpp/dbt_face_detection.cpp +++ b/samples/cpp/dbt_face_detection.cpp @@ -22,7 +22,7 @@ class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector IDetector(), Detector(detector) { - CV_Assert(!detector.empty()); + CV_Assert(detector); } void detect(const cv::Mat &Image, std::vector &objects) @@ -51,11 +51,11 @@ int main(int , char** ) } std::string cascadeFrontalfilename = "../../data/lbpcascades/lbpcascade_frontalface.xml"; - cv::Ptr cascade = new cv::CascadeClassifier(cascadeFrontalfilename); - cv::Ptr MainDetector = new CascadeDetectorAdapter(cascade); + cv::Ptr cascade = makePtr(cascadeFrontalfilename); + cv::Ptr MainDetector = makePtr(cascade); - cascade = new cv::CascadeClassifier(cascadeFrontalfilename); - cv::Ptr TrackingDetector = new CascadeDetectorAdapter(cascade); + cascade = makePtr(cascadeFrontalfilename); + cv::Ptr TrackingDetector = makePtr(cascade); DetectionBasedTracker::Parameters params; DetectionBasedTracker Detector(MainDetector, TrackingDetector, params); diff --git a/samples/cpp/descriptor_extractor_matcher.cpp b/samples/cpp/descriptor_extractor_matcher.cpp index 43baed2fe..7df3cd02b 100644 --- a/samples/cpp/descriptor_extractor_matcher.cpp +++ b/samples/cpp/descriptor_extractor_matcher.cpp @@ -153,7 +153,7 @@ static void doIteration( const Mat& img1, Mat& img2, bool isWarpPerspective, { cout << "< Evaluate descriptor matcher..." << endl; vector curve; - Ptr gdm = new VectorDescriptorMatcher( descriptorExtractor, descriptorMatcher ); + Ptr gdm = makePtr( descriptorExtractor, descriptorMatcher ); evaluateGenericDescriptorMatcher( img1, img2, H12, keypoints1, keypoints2, 0, 0, curve, gdm ); Point2f firstPoint = *curve.begin(); @@ -253,7 +253,7 @@ int main(int argc, char** argv) int mactherFilterType = getMatcherFilterType( argv[4] ); bool eval = !isWarpPerspective ? false : (atoi(argv[6]) == 0 ? false : true); cout << ">" << endl; - if( detector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() ) + if( !detector || !descriptorExtractor || !descriptorMatcher ) { cout << "Can not create detector or descriptor exstractor or descriptor matcher of given types" << endl; return -1; diff --git a/samples/cpp/detection_based_tracker_sample.cpp b/samples/cpp/detection_based_tracker_sample.cpp index 1debff7e3..81afa789c 100644 --- a/samples/cpp/detection_based_tracker_sample.cpp +++ b/samples/cpp/detection_based_tracker_sample.cpp @@ -67,7 +67,7 @@ class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector CascadeDetectorAdapter(cv::Ptr detector): Detector(detector) { - CV_Assert(!detector.empty()); + CV_Assert(detector); } void detect(const cv::Mat &Image, std::vector &objects) @@ -117,11 +117,11 @@ static int test_FaceDetector(int argc, char *argv[]) } std::string cascadeFrontalfilename=cascadefile; - cv::Ptr cascade = new cv::CascadeClassifier(cascadeFrontalfilename); - cv::Ptr MainDetector = new CascadeDetectorAdapter(cascade); + cv::Ptr cascade = makePtr(cascadeFrontalfilename); + cv::Ptr MainDetector = makePtr(cascade); - cascade = new cv::CascadeClassifier(cascadeFrontalfilename); - cv::Ptr TrackingDetector = new CascadeDetectorAdapter(cascade); + cascade = makePtr(cascadeFrontalfilename); + cv::Ptr TrackingDetector = makePtr(cascade); DetectionBasedTracker::Parameters params; DetectionBasedTracker fd(MainDetector, TrackingDetector, params); diff --git a/samples/cpp/detector_descriptor_evaluation.cpp b/samples/cpp/detector_descriptor_evaluation.cpp index ece735a50..dd3cd800e 100644 --- a/samples/cpp/detector_descriptor_evaluation.cpp +++ b/samples/cpp/detector_descriptor_evaluation.cpp @@ -535,7 +535,7 @@ void DetectorQualityEvaluator::readAlgorithm () { defaultDetector = FeatureDetector::create( algName ); specificDetector = FeatureDetector::create( algName ); - if( defaultDetector.empty() ) + if( !defaultDetector ) { printf( "Algorithm can not be read\n" ); exit(-1); @@ -769,14 +769,14 @@ void DescriptorQualityEvaluator::readAlgorithm( ) defaultDescMatcher = GenericDescriptorMatcher::create( algName ); specificDescMatcher = GenericDescriptorMatcher::create( algName ); - if( defaultDescMatcher.empty() ) + if( !defaultDescMatcher ) { Ptr extractor = DescriptorExtractor::create( algName ); Ptr matcher = DescriptorMatcher::create( matcherName ); - defaultDescMatcher = new VectorDescriptorMatch( extractor, matcher ); - specificDescMatcher = new VectorDescriptorMatch( extractor, matcher ); + defaultDescMatcher = makePtr( extractor, matcher ); + specificDescMatcher = makePtr( extractor, matcher ); - if( extractor.empty() || matcher.empty() ) + if( !extractor || !matcher ) { printf("Algorithm can not be read\n"); exit(-1); @@ -881,8 +881,9 @@ public: virtual void readAlgorithm( ) { string classifierFile = data_path + "/features2d/calonder_classifier.rtc"; - defaultDescMatcher = new VectorDescriptorMatch( new CalonderDescriptorExtractor( classifierFile ), - new BFMatcher(NORM_L2) ); + defaultDescMatcher = makePtr( + makePtr >( classifierFile ), + makePtr(int(NORM_L2))); specificDescMatcher = defaultDescMatcher; } }; @@ -922,10 +923,11 @@ void OneWayDescriptorQualityTest::processRunParamsFile () readAllDatasetsRunParams(); - OneWayDescriptorBase *base = new OneWayDescriptorBase(patchSize, poseCount, pcaFilename, - trainPath, trainImagesList); + Ptr base( + new OneWayDescriptorBase(patchSize, poseCount, pcaFilename, + trainPath, trainImagesList)); - OneWayDescriptorMatch *match = new OneWayDescriptorMatch (); + Ptr match = makePtr(); match->initialize( OneWayDescriptorMatch::Params (), base ); defaultDescMatcher = match; writeAllDatasetsRunParams(); @@ -958,18 +960,18 @@ int main( int argc, char** argv ) Ptr evals[] = { - new DetectorQualityEvaluator( "FAST", "quality-detector-fast" ), - new DetectorQualityEvaluator( "GFTT", "quality-detector-gftt" ), - new DetectorQualityEvaluator( "HARRIS", "quality-detector-harris" ), - new DetectorQualityEvaluator( "MSER", "quality-detector-mser" ), - new DetectorQualityEvaluator( "STAR", "quality-detector-star" ), - new DetectorQualityEvaluator( "SIFT", "quality-detector-sift" ), - new DetectorQualityEvaluator( "SURF", "quality-detector-surf" ), + makePtr( "FAST", "quality-detector-fast" ), + makePtr( "GFTT", "quality-detector-gftt" ), + makePtr( "HARRIS", "quality-detector-harris" ), + makePtr( "MSER", "quality-detector-mser" ), + makePtr( "STAR", "quality-detector-star" ), + makePtr( "SIFT", "quality-detector-sift" ), + makePtr( "SURF", "quality-detector-surf" ), - new DescriptorQualityEvaluator( "SIFT", "quality-descriptor-sift", "BruteForce" ), - new DescriptorQualityEvaluator( "SURF", "quality-descriptor-surf", "BruteForce" ), - new DescriptorQualityEvaluator( "FERN", "quality-descriptor-fern"), - new CalonderDescriptorQualityEvaluator() + makePtr( "SIFT", "quality-descriptor-sift", "BruteForce" ), + makePtr( "SURF", "quality-descriptor-surf", "BruteForce" ), + makePtr( "FERN", "quality-descriptor-fern"), + makePtr() }; for( size_t i = 0; i < sizeof(evals)/sizeof(evals[0]); i++ ) diff --git a/samples/cpp/fabmap_sample.cpp b/samples/cpp/fabmap_sample.cpp index 75febb134..cd06b55cb 100644 --- a/samples/cpp/fabmap_sample.cpp +++ b/samples/cpp/fabmap_sample.cpp @@ -131,11 +131,11 @@ int main(int argc, char * argv[]) { //generate test data cout << "Extracting Test Data from images" << endl << endl; - Ptr detector = + Ptr detector( new DynamicAdaptedFeatureDetector( - AdjusterAdapter::create("STAR"), 130, 150, 5); - Ptr extractor = - new SurfDescriptorExtractor(1000, 4, 2, false, true); + AdjusterAdapter::create("STAR"), 130, 150, 5)); + Ptr extractor( + new SurfDescriptorExtractor(1000, 4, 2, false, true)); Ptr matcher = DescriptorMatcher::create("FlannBased"); @@ -183,8 +183,8 @@ int main(int argc, char * argv[]) { endl; Ptr fabmap; - fabmap = new of2::FabMap2(tree, 0.39, 0, of2::FabMap::SAMPLED | - of2::FabMap::CHOW_LIU); + fabmap.reset(new of2::FabMap2(tree, 0.39, 0, of2::FabMap::SAMPLED | + of2::FabMap::CHOW_LIU)); fabmap->addTraining(trainData); vector matches; diff --git a/samples/cpp/generic_descriptor_match.cpp b/samples/cpp/generic_descriptor_match.cpp index 888c24f7e..359f3c08e 100644 --- a/samples/cpp/generic_descriptor_match.cpp +++ b/samples/cpp/generic_descriptor_match.cpp @@ -33,7 +33,7 @@ int main(int argc, char** argv) std::string params_filename = std::string(argv[4]); Ptr descriptorMatcher = GenericDescriptorMatcher::create(alg_name, params_filename); - if( descriptorMatcher.empty() ) + if( !descriptorMatcher ) { printf ("Cannot create descriptor\n"); return 0; diff --git a/samples/cpp/image.cpp b/samples/cpp/image.cpp index 806926b04..80f80c7af 100644 --- a/samples/cpp/image.cpp +++ b/samples/cpp/image.cpp @@ -31,8 +31,8 @@ int main( int argc, char** argv ) help(); const char* imagename = argc > 1 ? argv[1] : "lena.jpg"; #if DEMO_MIXED_API_USE - Ptr iplimg = cvLoadImage(imagename); // Ptr is safe ref-conting pointer class - if(iplimg.empty()) + Ptr iplimg(cvLoadImage(imagename)); // Ptr is safe ref-counting pointer class + if(!iplimg) { fprintf(stderr, "Can not load image %s\n", imagename); return -1; diff --git a/samples/cpp/linemod.cpp b/samples/cpp/linemod.cpp index 08d2a0035..4d11da36b 100644 --- a/samples/cpp/linemod.cpp +++ b/samples/cpp/linemod.cpp @@ -114,7 +114,7 @@ private: // Functions to store detector and templates in single XML/YAML file static cv::Ptr readLinemod(const std::string& filename) { - cv::Ptr detector = new cv::linemod::Detector; + cv::Ptr detector = cv::makePtr(); cv::FileStorage fs(filename, cv::FileStorage::READ); detector->read(fs.root()); diff --git a/samples/cpp/matching_to_many_images.cpp b/samples/cpp/matching_to_many_images.cpp index 7a346e3f4..152b40064 100644 --- a/samples/cpp/matching_to_many_images.cpp +++ b/samples/cpp/matching_to_many_images.cpp @@ -84,7 +84,7 @@ static bool createDetectorDescriptorMatcher( const string& detectorType, const s descriptorMatcher = DescriptorMatcher::create( matcherType ); cout << ">" << endl; - bool isCreated = !( featureDetector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() ); + bool isCreated = featureDetector && descriptorExtractor && descriptorMatcher; if( !isCreated ) cout << "Can not create feature detector or descriptor extractor or descriptor matcher of given types." << endl << ">" << endl; diff --git a/samples/cpp/stitching_detailed.cpp b/samples/cpp/stitching_detailed.cpp index ecf19b494..a576816d6 100644 --- a/samples/cpp/stitching_detailed.cpp +++ b/samples/cpp/stitching_detailed.cpp @@ -358,14 +358,14 @@ int main(int argc, char* argv[]) { #ifdef HAVE_OPENCV_NONFREE if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) - finder = new SurfFeaturesFinderGpu(); + finder = makePtr(); else #endif - finder = new SurfFeaturesFinder(); + finder = makePtr(); } else if (features_type == "orb") { - finder = new OrbFeaturesFinder(); + finder = makePtr(); } else { @@ -484,8 +484,8 @@ int main(int argc, char* argv[]) } Ptr adjuster; - if (ba_cost_func == "reproj") adjuster = new detail::BundleAdjusterReproj(); - else if (ba_cost_func == "ray") adjuster = new detail::BundleAdjusterRay(); + if (ba_cost_func == "reproj") adjuster = makePtr(); + else if (ba_cost_func == "ray") adjuster = makePtr(); else { cout << "Unknown bundle adjustment cost function: '" << ba_cost_func << "'.\n"; @@ -555,31 +555,49 @@ int main(int argc, char* argv[]) #ifdef HAVE_OPENCV_GPUWARPING if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) { - if (warp_type == "plane") warper_creator = new cv::PlaneWarperGpu(); - else if (warp_type == "cylindrical") warper_creator = new cv::CylindricalWarperGpu(); - else if (warp_type == "spherical") warper_creator = new cv::SphericalWarperGpu(); + if (warp_type == "plane") + warper_creator = makePtr(); + else if (warp_type == "cylindrical") + warper_creator = makePtr(); + else if (warp_type == "spherical") + warper_creator = makePtr(); } else #endif { - if (warp_type == "plane") warper_creator = new cv::PlaneWarper(); - else if (warp_type == "cylindrical") warper_creator = new cv::CylindricalWarper(); - else if (warp_type == "spherical") warper_creator = new cv::SphericalWarper(); - else if (warp_type == "fisheye") warper_creator = new cv::FisheyeWarper(); - else if (warp_type == "stereographic") warper_creator = new cv::StereographicWarper(); - else if (warp_type == "compressedPlaneA2B1") warper_creator = new cv::CompressedRectilinearWarper(2, 1); - else if (warp_type == "compressedPlaneA1.5B1") warper_creator = new cv::CompressedRectilinearWarper(1.5, 1); - else if (warp_type == "compressedPlanePortraitA2B1") warper_creator = new cv::CompressedRectilinearPortraitWarper(2, 1); - else if (warp_type == "compressedPlanePortraitA1.5B1") warper_creator = new cv::CompressedRectilinearPortraitWarper(1.5, 1); - else if (warp_type == "paniniA2B1") warper_creator = new cv::PaniniWarper(2, 1); - else if (warp_type == "paniniA1.5B1") warper_creator = new cv::PaniniWarper(1.5, 1); - else if (warp_type == "paniniPortraitA2B1") warper_creator = new cv::PaniniPortraitWarper(2, 1); - else if (warp_type == "paniniPortraitA1.5B1") warper_creator = new cv::PaniniPortraitWarper(1.5, 1); - else if (warp_type == "mercator") warper_creator = new cv::MercatorWarper(); - else if (warp_type == "transverseMercator") warper_creator = new cv::TransverseMercatorWarper(); + if (warp_type == "plane") + warper_creator = makePtr(); + else if (warp_type == "cylindrical") + warper_creator = makePtr(); + else if (warp_type == "spherical") + warper_creator = makePtr(); + else if (warp_type == "fisheye") + warper_creator = makePtr(); + else if (warp_type == "stereographic") + warper_creator = makePtr(); + else if (warp_type == "compressedPlaneA2B1") + warper_creator = makePtr(2.0f, 1.0f); + else if (warp_type == "compressedPlaneA1.5B1") + warper_creator = makePtr(1.5f, 1.0f); + else if (warp_type == "compressedPlanePortraitA2B1") + warper_creator = makePtr(2.0f, 1.0f); + else if (warp_type == "compressedPlanePortraitA1.5B1") + warper_creator = makePtr(1.5f, 1.0f); + else if (warp_type == "paniniA2B1") + warper_creator = makePtr(2.0f, 1.0f); + else if (warp_type == "paniniA1.5B1") + warper_creator = makePtr(1.5f, 1.0f); + else if (warp_type == "paniniPortraitA2B1") + warper_creator = makePtr(2.0f, 1.0f); + else if (warp_type == "paniniPortraitA1.5B1") + warper_creator = makePtr(1.5f, 1.0f); + else if (warp_type == "mercator") + warper_creator = makePtr(); + else if (warp_type == "transverseMercator") + warper_creator = makePtr(); } - if (warper_creator.empty()) + if (!warper_creator) { cout << "Can't create the following warper '" << warp_type << "'\n"; return 1; @@ -612,32 +630,32 @@ int main(int argc, char* argv[]) Ptr seam_finder; if (seam_find_type == "no") - seam_finder = new detail::NoSeamFinder(); + seam_finder = makePtr(); else if (seam_find_type == "voronoi") - seam_finder = new detail::VoronoiSeamFinder(); + seam_finder = makePtr(); else if (seam_find_type == "gc_color") { #ifdef HAVE_OPENCV_GPU if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) - seam_finder = new detail::GraphCutSeamFinderGpu(GraphCutSeamFinderBase::COST_COLOR); + seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR); else #endif - seam_finder = new detail::GraphCutSeamFinder(GraphCutSeamFinderBase::COST_COLOR); + seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR); } else if (seam_find_type == "gc_colorgrad") { #ifdef HAVE_OPENCV_GPU if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0) - seam_finder = new detail::GraphCutSeamFinderGpu(GraphCutSeamFinderBase::COST_COLOR_GRAD); + seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR_GRAD); else #endif - seam_finder = new detail::GraphCutSeamFinder(GraphCutSeamFinderBase::COST_COLOR_GRAD); + seam_finder = makePtr(GraphCutSeamFinderBase::COST_COLOR_GRAD); } else if (seam_find_type == "dp_color") - seam_finder = new detail::DpSeamFinder(DpSeamFinder::COLOR); + seam_finder = makePtr(DpSeamFinder::COLOR); else if (seam_find_type == "dp_colorgrad") - seam_finder = new detail::DpSeamFinder(DpSeamFinder::COLOR_GRAD); - if (seam_finder.empty()) + seam_finder = makePtr(DpSeamFinder::COLOR_GRAD); + if (!seam_finder) { cout << "Can't create the following seam finder '" << seam_find_type << "'\n"; return 1; @@ -735,7 +753,7 @@ int main(int argc, char* argv[]) resize(dilated_mask, seam_mask, mask_warped.size()); mask_warped = seam_mask & mask_warped; - if (blender.empty()) + if (!blender) { blender = Blender::createDefault(blend_type, try_gpu); Size dst_sz = resultRoi(corners, sizes).size(); @@ -744,13 +762,13 @@ int main(int argc, char* argv[]) blender = Blender::createDefault(Blender::NO, try_gpu); else if (blend_type == Blender::MULTI_BAND) { - MultiBandBlender* mb = dynamic_cast(static_cast(blender)); + MultiBandBlender* mb = dynamic_cast(blender.get()); mb->setNumBands(static_cast(ceil(log(blend_width)/log(2.)) - 1.)); LOGLN("Multi-band blender, number of bands: " << mb->numBands()); } else if (blend_type == Blender::FEATHER) { - FeatherBlender* fb = dynamic_cast(static_cast(blender)); + FeatherBlender* fb = dynamic_cast(blender.get()); fb->setSharpness(1.f/blend_width); LOGLN("Feather blender, sharpness: " << fb->sharpness()); } diff --git a/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp b/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp index 6c681d643..e13f2b696 100644 --- a/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp +++ b/samples/cpp/tutorial_code/core/interoperability_with_OpenCV_1/interoperability_with_OpenCV_1.cpp @@ -32,8 +32,8 @@ int main( int argc, char** argv ) const char* imagename = argc > 1 ? argv[1] : "lena.jpg"; #ifdef DEMO_MIXED_API_USE - Ptr IplI = cvLoadImage(imagename); // Ptr is safe ref-counting pointer class - if(IplI.empty()) + Ptr IplI(cvLoadImage(imagename)); // Ptr is a safe ref-counting pointer class + if(!IplI) { cerr << "Can not load image " << imagename << endl; return -1; diff --git a/samples/cpp/video_homography.cpp b/samples/cpp/video_homography.cpp index c8388007d..bf2559fba 100644 --- a/samples/cpp/video_homography.cpp +++ b/samples/cpp/video_homography.cpp @@ -152,7 +152,7 @@ int main(int ac, char ** av) Mat train_desc, query_desc; const int DESIRED_FTRS = 500; - GridAdaptedFeatureDetector detector(new FastFeatureDetector(10, true), DESIRED_FTRS, 4, 4); + GridAdaptedFeatureDetector detector(makePtr(10, true), DESIRED_FTRS, 4, 4); Mat H_prev = Mat::eye(3, 3, CV_32FC1); for (;;) diff --git a/samples/cpp/videostab.cpp b/samples/cpp/videostab.cpp index 21606d495..01cdcf37f 100644 --- a/samples/cpp/videostab.cpp +++ b/samples/cpp/videostab.cpp @@ -193,7 +193,7 @@ public: virtual Ptr build() { - MotionEstimatorRansacL2 *est = new MotionEstimatorRansacL2(motionModel(arg(prefix + "model"))); + Ptr est = makePtr(motionModel(arg(prefix + "model"))); RansacParams ransac = est->ransacParams(); if (arg(prefix + "subset") != "auto") @@ -205,10 +205,10 @@ public: est->setMinInlierRatio(argf(prefix + "min-inlier-ratio")); - Ptr outlierRejector = new NullOutlierRejector(); + Ptr outlierRejector = makePtr(); if (arg(prefix + "local-outlier-rejection") == "yes") { - TranslationBasedLocalOutlierRejector *tblor = new TranslationBasedLocalOutlierRejector(); + Ptr tblor = makePtr(); RansacParams ransacParams = tblor->ransacParams(); if (arg(prefix + "thresh") != "auto") ransacParams.thresh = argf(prefix + "thresh"); @@ -219,14 +219,14 @@ public: #if defined(HAVE_OPENCV_GPUIMGPROC) && defined(HAVE_OPENCV_GPU) && defined(HAVE_OPENCV_GPUOPTFLOW) if (gpu) { - KeypointBasedMotionEstimatorGpu *kbest = new KeypointBasedMotionEstimatorGpu(est); + Ptr kbest = makePtr(est); kbest->setOutlierRejector(outlierRejector); return kbest; } #endif - KeypointBasedMotionEstimator *kbest = new KeypointBasedMotionEstimator(est); - kbest->setDetector(new GoodFeaturesToTrackDetector(argi(prefix + "nkps"))); + Ptr kbest = makePtr(est); + kbest->setDetector(makePtr(argi(prefix + "nkps"))); kbest->setOutlierRejector(outlierRejector); return kbest; } @@ -244,12 +244,12 @@ public: virtual Ptr build() { - MotionEstimatorL1 *est = new MotionEstimatorL1(motionModel(arg(prefix + "model"))); + Ptr est = makePtr(motionModel(arg(prefix + "model"))); - Ptr outlierRejector = new NullOutlierRejector(); + Ptr outlierRejector = makePtr(); if (arg(prefix + "local-outlier-rejection") == "yes") { - TranslationBasedLocalOutlierRejector *tblor = new TranslationBasedLocalOutlierRejector(); + Ptr tblor = makePtr(); RansacParams ransacParams = tblor->ransacParams(); if (arg(prefix + "thresh") != "auto") ransacParams.thresh = argf(prefix + "thresh"); @@ -260,14 +260,14 @@ public: #if defined(HAVE_OPENCV_GPUIMGPROC) && defined(HAVE_OPENCV_GPU) && defined(HAVE_OPENCV_GPUOPTFLOW) if (gpu) { - KeypointBasedMotionEstimatorGpu *kbest = new KeypointBasedMotionEstimatorGpu(est); + Ptr kbest = makePtr(est); kbest->setOutlierRejector(outlierRejector); return kbest; } #endif - KeypointBasedMotionEstimator *kbest = new KeypointBasedMotionEstimator(est); - kbest->setDetector(new GoodFeaturesToTrackDetector(argi(prefix + "nkps"))); + Ptr kbest = makePtr(est); + kbest->setDetector(makePtr(argi(prefix + "nkps"))); kbest->setOutlierRejector(outlierRejector); return kbest; } @@ -363,7 +363,7 @@ int main(int argc, const char **argv) // get source video parameters - VideoFileSource *source = new VideoFileSource(inputPath); + Ptr source = makePtr(inputPath); cout << "frame count (rough): " << source->count() << endl; if (arg("fps") == "auto") outputFps = source->fps(); @@ -374,15 +374,15 @@ int main(int argc, const char **argv) Ptr motionEstBuilder; if (arg("lin-prog-motion-est") == "yes") - motionEstBuilder = new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes"); + motionEstBuilder.reset(new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes")); else - motionEstBuilder = new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes"); + motionEstBuilder.reset(new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes")); Ptr wsMotionEstBuilder; if (arg("ws-lp") == "yes") - wsMotionEstBuilder = new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes", "ws-"); + wsMotionEstBuilder.reset(new MotionEstimatorL1Builder(cmd, arg("gpu") == "yes", "ws-")); else - wsMotionEstBuilder = new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes", "ws-"); + wsMotionEstBuilder.reset(new MotionEstimatorRansacL2Builder(cmd, arg("gpu") == "yes", "ws-")); // determine whether we must use one pass or two pass stabilizer bool isTwoPass = @@ -400,7 +400,7 @@ int main(int argc, const char **argv) if (arg("lin-prog-stab") == "yes") { - LpMotionStabilizer *stab = new LpMotionStabilizer(); + Ptr stab = makePtr(); stab->setFrameSize(Size(source->width(), source->height())); stab->setTrimRatio(arg("lps-trim-ratio") == "auto" ? argf("trim-ratio") : argf("lps-trim-ratio")); stab->setWeight1(argf("lps-w1")); @@ -410,18 +410,18 @@ int main(int argc, const char **argv) twoPassStabilizer->setMotionStabilizer(stab); } else if (arg("stdev") == "auto") - twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"))); + twoPassStabilizer->setMotionStabilizer(makePtr(argi("radius"))); else - twoPassStabilizer->setMotionStabilizer(new GaussianMotionFilter(argi("radius"), argf("stdev"))); + twoPassStabilizer->setMotionStabilizer(makePtr(argi("radius"), argf("stdev"))); // init wobble suppressor if necessary if (arg("wobble-suppress") == "yes") { - MoreAccurateMotionWobbleSuppressorBase *ws = new MoreAccurateMotionWobbleSuppressor(); + Ptr ws = makePtr(); if (arg("gpu") == "yes") #ifdef HAVE_OPENCV_GPU - ws = new MoreAccurateMotionWobbleSuppressorGpu(); + ws = makePtr(); #else throw runtime_error("OpenCV is built without GPU support"); #endif @@ -433,12 +433,12 @@ int main(int argc, const char **argv) MotionModel model = ws->motionEstimator()->motionModel(); if (arg("load-motions2") != "no") { - ws->setMotionEstimator(new FromFileMotionReader(arg("load-motions2"))); + ws->setMotionEstimator(makePtr(arg("load-motions2"))); ws->motionEstimator()->setMotionModel(model); } if (arg("save-motions2") != "no") { - ws->setMotionEstimator(new ToFileMotionWriter(arg("save-motions2"), ws->motionEstimator())); + ws->setMotionEstimator(makePtr(arg("save-motions2"), ws->motionEstimator())); ws->motionEstimator()->setMotionModel(model); } } @@ -450,26 +450,26 @@ int main(int argc, const char **argv) OnePassStabilizer *onePassStabilizer = new OnePassStabilizer(); stabilizer = onePassStabilizer; if (arg("stdev") == "auto") - onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius"))); + onePassStabilizer->setMotionFilter(makePtr(argi("radius"))); else - onePassStabilizer->setMotionFilter(new GaussianMotionFilter(argi("radius"), argf("stdev"))); + onePassStabilizer->setMotionFilter(makePtr(argi("radius"), argf("stdev"))); } stabilizer->setFrameSource(source); stabilizer->setMotionEstimator(motionEstBuilder->build()); // cast stabilizer to simple frame source interface to read stabilized frames - stabilizedFrames = dynamic_cast(stabilizer); + stabilizedFrames.reset(dynamic_cast(stabilizer)); MotionModel model = stabilizer->motionEstimator()->motionModel(); if (arg("load-motions") != "no") { - stabilizer->setMotionEstimator(new FromFileMotionReader(arg("load-motions"))); + stabilizer->setMotionEstimator(makePtr(arg("load-motions"))); stabilizer->motionEstimator()->setMotionModel(model); } if (arg("save-motions") != "no") { - stabilizer->setMotionEstimator(new ToFileMotionWriter(arg("save-motions"), stabilizer->motionEstimator())); + stabilizer->setMotionEstimator(makePtr(arg("save-motions"), stabilizer->motionEstimator())); stabilizer->motionEstimator()->setMotionModel(model); } @@ -478,7 +478,7 @@ int main(int argc, const char **argv) // init deblurer if (arg("deblur") == "yes") { - WeightingDeblurer *deblurer = new WeightingDeblurer(); + Ptr deblurer = makePtr(); deblurer->setRadius(argi("radius")); deblurer->setSensitivity(argf("deblur-sens")); stabilizer->setDeblurer(deblurer); @@ -503,22 +503,22 @@ int main(int argc, const char **argv) Ptr inpainters_(inpainters); if (arg("mosaic") == "yes") { - ConsistentMosaicInpainter *inp = new ConsistentMosaicInpainter(); + Ptr inp = makePtr(); inp->setStdevThresh(argf("mosaic-stdev")); inpainters->pushBack(inp); } if (arg("motion-inpaint") == "yes") { - MotionInpainter *inp = new MotionInpainter(); + Ptr inp = makePtr(); inp->setDistThreshold(argf("mi-dist-thresh")); inpainters->pushBack(inp); } if (arg("color-inpaint") == "average") - inpainters->pushBack(new ColorAverageInpainter()); + inpainters->pushBack(makePtr()); else if (arg("color-inpaint") == "ns") - inpainters->pushBack(new ColorInpainter(INPAINT_NS, argd("ci-radius"))); + inpainters->pushBack(makePtr(int(INPAINT_NS), argd("ci-radius"))); else if (arg("color-inpaint") == "telea") - inpainters->pushBack(new ColorInpainter(INPAINT_TELEA, argd("ci-radius"))); + inpainters->pushBack(makePtr(int(INPAINT_TELEA), argd("ci-radius"))); else if (arg("color-inpaint") != "no") throw runtime_error("unknown color inpainting method: " + arg("color-inpaint")); if (!inpainters->empty()) diff --git a/samples/gpu/performance/tests.cpp b/samples/gpu/performance/tests.cpp index 136a4d52a..a3df43c6e 100644 --- a/samples/gpu/performance/tests.cpp +++ b/samples/gpu/performance/tests.cpp @@ -1261,7 +1261,7 @@ TEST(FarnebackOpticalFlow) namespace cv { - template<> void Ptr::delete_obj() + template<> void DefaultDeleter::operator ()(CvBGStatModel* obj) const { cvReleaseBGStatModel(&obj); } diff --git a/samples/gpu/stereo_multi.cpp b/samples/gpu/stereo_multi.cpp index 4ad8b2884..430e15bd7 100644 --- a/samples/gpu/stereo_multi.cpp +++ b/samples/gpu/stereo_multi.cpp @@ -291,11 +291,11 @@ StereoMultiGpuStream::StereoMultiGpuStream() { gpu::setDevice(0); d_algs[0] = gpu::createStereoBM(256); - streams[0] = new Stream; + streams[0] = makePtr(); gpu::setDevice(1); d_algs[1] = gpu::createStereoBM(256); - streams[1] = new Stream; + streams[1] = makePtr(); } StereoMultiGpuStream::~StereoMultiGpuStream() diff --git a/samples/gpu/super_resolution.cpp b/samples/gpu/super_resolution.cpp index 49b0a6332..e124b375e 100644 --- a/samples/gpu/super_resolution.cpp +++ b/samples/gpu/super_resolution.cpp @@ -53,7 +53,7 @@ static Ptr createOptFlow(const string& name, bool useGpu) { cerr << "Incorrect Optical Flow algorithm - " << name << endl; } - return 0; + return Ptr(); } #if defined(HAVE_OPENCV_OCL) static Ptr createOptFlow(const string& name) @@ -73,7 +73,7 @@ static Ptr createOptFlow(const string& name) else if (name == "brox") { std::cout<<"brox has not been implemented!\n"; - return NULL; + return Ptr(); } else if (name == "pyrlk") return createOptFlow_PyrLK_OCL(); @@ -81,7 +81,7 @@ static Ptr createOptFlow(const string& name) { cerr << "Incorrect Optical Flow algorithm - " << name << endl; } - return 0; + return Ptr(); } #endif int main(int argc, const char* argv[]) @@ -197,7 +197,7 @@ int main(int argc, const char* argv[]) frameSource.release(); } } - if (frameSource.empty()) + if (!frameSource) frameSource = createFrameSource_Video(inputVideoName); // skip first frame, it is usually corrupted