強力なを使用してビデオストリームから四角いオブジェクトを検出することに成功しましたOpenCV
。メインスレッドの計算負担のため、ビデオのフレームが遅れることを除いて、すべてうまくいきました。OpenCV:バックグラウンドキューでのビデオ処理
コードスニペットの下に見ることができるように私はバックグラウンド動作キューとUIImageFromCVMat
メインキューにfindSquaresInImage
を実行しようとしたとき、私はCvVideoCameraDelegate
デリゲートメソッドprocessImage:(cv::Mat&)image
内NSOperationQueue
を追加しました。
- (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