【问题标题】:OpenCV VideoCapture not opens file iosOpenCV VideoCapture 不打开文件 ios
【发布时间】:2012-09-08 03:15:36
【问题描述】:

我已使用以下方法将视频保存到文件中:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"test9.avi"];

NSLog(@"Open to: %@", documentsDirectory);

std::string fileName([documentsDirectory cString], [documentsDirectory cStringLength]);
videoWriter = new cv::VideoWriter(fileName, CV_FOURCC('H', 'F', 'Y', 'U'), 25, cvSize(288,352),1);

但我无法使用 VideoCapture 打开它:

NSLog(@"Load the video");
videoCapture = new cv::VideoCapture();
NSLog(@"Video loaded");
if (videoCapture->open(fileName))
{
    NSLog(@"Opened");
}
else {
    NSLog(@"Failed to open");
}

VideoCapture 本身没有错误,只是我的日志“无法打开”... 我能以某种方式获得更详细的错误日志吗?

【问题讨论】:

  • 您要处理视频中的单个帧还是整个文件?
  • 我想处理整个文件。我是记录文件的人,因此我可以将其记录为任何格式。现在我正在实现一个简单的转储 cv::Mat 到二进制文件的方法并以同样的方式读回它......这样我就不必使用 VideoCapture。

标签: ios opencv video-capture


【解决方案1】:
cv::VideoCapture capture;
NSString* pathToInputFile = [NSString documentPathByAppendingPath:@"Video/sample_iTunes.mov"];
if(capture.open(std::string([pathToFile UTF8String]))){
    NSLog(@"Opened");
}else{
    NSLog(@"Failed to open");
}

适合我

【讨论】:

    【解决方案2】:

    虽然基里尔的答案在发布时会起作用,但现在情况已不再如此。

    以下适用于 Xcode 8.2.1,替换不再可用的 documentPathByAppendingPath 函数。

    cv::VideoCapture cap;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyVideo" ofType:@"MOV"];
    if ( cap.open ( std::string( path.UTF8String ) ) ) {
        NSLog(@"Opened");
    } else {
        NSLog(@"Failed to open");
    }
    

    【讨论】:

      【解决方案3】:

      我在使用 Swift 3 时遇到了这个问题。 感谢KirillCodeBender 的回答,我解决了这个问题。

      let resourceURL = Bundle.main.url(forResource: "test", withExtension: "MOV")
      let openCVVideoCapture = OpenCVVideoCaptureWrapper.init(file: resourceURL?.path)
      

      以下是我的OpenCVVideoCaptureWrapper.mm

      @implementation OpenCVVideoCaptureWrapper {
      
          cv::VideoCapture video;
      }
      
      - (instancetype)initWithFile:(NSString*) filePath  {
      
          std::string cvFilePath = [filePath UTF8String];
          video = cv::VideoCapture(cvFilePath);
      
          if (!video.isOpened()) NSLog(@"Could not open file");
      
          return self;
      }
      @end
      

      【讨论】:

        【解决方案4】:

        这个 sn-p 在 Xcode 10、Swift 4 中为我工作。感谢@Nathan Tuggy。

        - (void)openVideoFile:(NSURL *)fileName {
        
            NSString *filename = fileName.path;
            std::string filePath = [filename UTF8String];
        
            cv::VideoCapture capture(filePath);
        
            if (!capture.isOpened()) {
                printf("@Error Opening video file");
                // failed, print error message
            }
            else {
                printf("File Opened");
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2018-07-26
          • 2013-01-22
          • 2014-09-02
          • 2012-07-13
          • 1970-01-01
          • 2017-09-28
          • 2012-04-30
          • 2017-11-08
          • 2017-11-12
          相关资源
          最近更新 更多