Guest User

Untitled

a guest
Jan 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // OpenCV-Memory-Leak-Test
  4. //
  5. // Created by user on 1/21/19.
  6. // Copyright © 2019 Alex Ovechko. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. #include <opencv2/core.hpp>
  12. #include <opencv2/objdetect/objdetect.hpp>
  13. #include <opencv2/highgui/highgui.hpp>
  14. #include <opencv2/imgproc/imgproc.hpp>
  15.  
  16. #include <iostream>
  17.  
  18. using namespace std;
  19.  
  20. void doTest(const char *cascadePath, const char *imgPath);
  21. void runBackground(const std::function<void(void)> &bgTask);
  22.  
  23.  
  24. @implementation ViewController
  25.  
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28.  
  29. NSString *cascadePath = [NSBundle.mainBundle pathForResource:@"haar-aGest" ofType:@"xml"];
  30. NSString *imgPath = [NSBundle.mainBundle pathForResource:@"hand" ofType:@"jpg"];
  31.  
  32. doTest([cascadePath UTF8String],
  33. [imgPath UTF8String]);
  34. }
  35.  
  36. @end
  37.  
  38.  
  39.  
  40. #pragma mark Test
  41.  
  42. void doTest(const char *cascadePath, const char *imgPath) {
  43. const string haarPath(cascadePath);
  44. const string imPath(imgPath);
  45.  
  46. const auto task = [haarPath, imPath](void){
  47. cv::CascadeClassifier handCascade;
  48. if (haarPath.empty() || !handCascade.load(haarPath)) {
  49. cout << "Loading was failed, path = "
  50. << haarPath << endl;
  51. return;
  52. }
  53. if (imPath.empty()) {
  54. cout << "Image path is null" << endl;
  55. return;
  56. }
  57. cv::Mat inputImg = cv::imread(imPath);
  58.  
  59. if (inputImg.empty()) {
  60. cout << "Image is empty at path " << imPath << endl;
  61. return;
  62. }
  63. cv::Mat frameGray;
  64.  
  65. if (inputImg.channels() == 1) {
  66. frameGray = inputImg;
  67. } else if (inputImg.channels() == 3) {
  68. cvtColor(inputImg, frameGray, cv::COLOR_BGR2GRAY);
  69. } else if (inputImg.channels() == 4) {
  70. cvtColor(inputImg, frameGray, cv::COLOR_BGRA2GRAY);
  71. }
  72. cout << "Loaded image with size " << frameGray.size
  73. << ", from path " << imPath
  74. << endl;
  75.  
  76. for (int i=0; i < 1e10; ++i) {
  77. std::vector<cv::Rect> objects;
  78. handCascade.detectMultiScale(frameGray,
  79. objects,
  80. 1.1, 2,
  81. 0|cv::CASCADE_SCALE_IMAGE,
  82. cv::Size(30, 30) );
  83. cout << "Found " << objects.size() << " objects, i = " << i << endl;
  84. }
  85. };
  86. runBackground(task);
  87. }
  88.  
  89. void runBackground(const std::function<void()> &bgTask) {
  90. __block const std::function<void()> task = bgTask;
  91. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
  92. ^{
  93. task();
  94. });
  95. }
Add Comment
Please, Sign In to add comment