0
成功地使用強大的OpenCV
從視頻流中檢測到方形對象。一切都很好,除了由於主線程的計算負擔造成的視頻滯後幀。OpenCV:背景隊列中的視頻處理
正如你可以看到下面的代碼片段,我加了NSOperationQueue
內CvVideoCameraDelegate
委託方法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........ ...... ...... ...
}
這裏有什麼問題?任何替代方法來改善?
// 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];
});
});
此主題無回覆http://answers.opencv.org/question/30959/ios-video-processing-is-very-slow/ – byJeevan