Why Objective-C doesn't show output frames of a video captured by OpenCV?

时间:2016-07-11 21:54:58

标签: ios objective-c opencv

I am new to Objective C and I would like to use Opencv to capture the video and process each frame. The output of each frame should be displayed on the screen (ios simulator).

The code in ViewController.h is:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    cv::Mat cvImage;
}

@property (nonatomic, weak) IBOutlet UIImageView* imageView;

@end

The code in ViewController.m is

#import "ViewController.h"
#import "opencv2/highgui/ios.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <iostream>
using namespace cv;
using namespace std;

@implementation ViewController

@synthesize imageView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    Mat frame;
    cv::VideoCapture cap(VIDEO_PATH);
    int count = 0;
    if(!cap.isOpened()){
        cout << "bad frame!!!!" << endl;
    }
    while(1){
         if(!cap.read(frame)){
            cout << "cannot read " << endl;
            exit(0);
         }

        cout << count++ << endl;
        imageView.image = MatToUIImage(frame);
        imageView.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
        [self.view addSubview:imageView];
    }
}
...
@end

If I remove while loop, the simulator will display the first frame from the video, which is reasonable. But if I have while loop, in the output console, the number of frames is counting, which means the program is reading frames from the video but in ios simulator, there is nothing....

Since I want to process every frame based on OpenCV, how can I display every frame after I captured using OpenCV and processed it?

Thank you!

1 个答案:

答案 0 :(得分:0)

不要在每个帧中添加新的子视图。只需将图像设置为现有视图即可。