Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.m
- // OpenCV-Memory-Leak-Test
- //
- // Created by user on 1/21/19.
- // Copyright © 2019 Alex Ovechko. All rights reserved.
- //
- #import "ViewController.h"
- #include <opencv2/core.hpp>
- #include <opencv2/objdetect/objdetect.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <iostream>
- using namespace std;
- void doTest(const char *cascadePath, const char *imgPath);
- void runBackground(const std::function<void(void)> &bgTask);
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSString *cascadePath = [NSBundle.mainBundle pathForResource:@"haar-aGest" ofType:@"xml"];
- NSString *imgPath = [NSBundle.mainBundle pathForResource:@"hand" ofType:@"jpg"];
- doTest([cascadePath UTF8String],
- [imgPath UTF8String]);
- }
- @end
- #pragma mark Test
- void doTest(const char *cascadePath, const char *imgPath) {
- const string haarPath(cascadePath);
- const string imPath(imgPath);
- const auto task = [haarPath, imPath](void){
- cv::CascadeClassifier handCascade;
- if (haarPath.empty() || !handCascade.load(haarPath)) {
- cout << "Loading was failed, path = "
- << haarPath << endl;
- return;
- }
- if (imPath.empty()) {
- cout << "Image path is null" << endl;
- return;
- }
- cv::Mat inputImg = cv::imread(imPath);
- if (inputImg.empty()) {
- cout << "Image is empty at path " << imPath << endl;
- return;
- }
- cv::Mat frameGray;
- if (inputImg.channels() == 1) {
- frameGray = inputImg;
- } else if (inputImg.channels() == 3) {
- cvtColor(inputImg, frameGray, cv::COLOR_BGR2GRAY);
- } else if (inputImg.channels() == 4) {
- cvtColor(inputImg, frameGray, cv::COLOR_BGRA2GRAY);
- }
- cout << "Loaded image with size " << frameGray.size
- << ", from path " << imPath
- << endl;
- for (int i=0; i < 1e10; ++i) {
- std::vector<cv::Rect> objects;
- handCascade.detectMultiScale(frameGray,
- objects,
- 1.1, 2,
- 0|cv::CASCADE_SCALE_IMAGE,
- cv::Size(30, 30) );
- cout << "Found " << objects.size() << " objects, i = " << i << endl;
- }
- };
- runBackground(task);
- }
- void runBackground(const std::function<void()> &bgTask) {
- __block const std::function<void()> task = bgTask;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
- ^{
- task();
- });
- }
Add Comment
Please, Sign In to add comment