0

成功地使用強大的OpenCV從視頻流中檢測到方形對象。一切都很好,除了由於主線程的計算負擔造成的視頻滯後幀。OpenCV:背景隊列中的視頻處理

正如你可以看到下面的代碼片段,我加了NSOperationQueueCvVideoCameraDelegate委託方法processImage:(cv::Mat&)image當我試圖在後臺操作隊列和UIImageFromCVMat主隊列運行findSquaresInImage

- (void)processImage:(cv::Mat&)image 

NSOperationQueue *videoProcessQueue = [[NSOperationQueue alloc] init]; 

[videoProcessQueue addOperationWithBlock:^{ 
    // do some time consuming stuff in the background 

    cv::Mat matResultImage = [self findSquaresInImage:image]; //---> Method will return the square object and takes more than 0.3 seconds 

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
     // update the UI here 

     self.imageView.image = [UtilityClass UIImageFromCVMat:matResultImage]; //---> Updates the UI 

    }]; 
}]; 

} 

但我得到的異常在此行​​cv::pyrDown()

- (cv::Mat)findSquaresInImage:(cv::Mat)_image 
{ 

    std::vector<std::vector<cv::Point> > squares; 
    cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray; 
    int thresh = 20, N = 2; 
    cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2)); //----> HERE I GOT EXCEPTION 
    cv::pyrUp(pyr, timg, _image.size()); 
    std::vector<std::vector<cv::Point> > contours; 
    //remaining logic goes here........ ...... ...... ... 
} 

enter image description here

這裏有什麼問題?任何替代方法來改善?

// UPDATE:

即使GCD塊同樣的問題:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     cv::Mat matResultImage = [self findSquaresInImage:image]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 

      self.imageView.image = [JKUtilityClass UIImageFromCVMat:matResultImage]; 
    }); 
}); 
+0

此主題無回覆http://answers.opencv.org/question/30959/ios-video-processing-is-very-slow/ – byJeevan

回答

0

最後能後2度大的變化來解決:

  • 直接映射從processImage:(cv::Mat&)image圖像CvVideoCamera對象。從未使用過已初始化的父視圖self.imageView

  • 加速過程中,包含OpenCVSquares檢測庫。