• <tfoot id='xUeD4'></tfoot>

    <legend id='xUeD4'><style id='xUeD4'><dir id='xUeD4'><q id='xUeD4'></q></dir></style></legend>

    1. <small id='xUeD4'></small><noframes id='xUeD4'>

        <bdo id='xUeD4'></bdo><ul id='xUeD4'></ul>
    2. <i id='xUeD4'><tr id='xUeD4'><dt id='xUeD4'><q id='xUeD4'><span id='xUeD4'><b id='xUeD4'><form id='xUeD4'><ins id='xUeD4'></ins><ul id='xUeD4'></ul><sub id='xUeD4'></sub></form><legend id='xUeD4'></legend><bdo id='xUeD4'><pre id='xUeD4'><center id='xUeD4'></center></pre></bdo></b><th id='xUeD4'></th></span></q></dt></tr></i><div id='xUeD4'><tfoot id='xUeD4'></tfoot><dl id='xUeD4'><fieldset id='xUeD4'></fieldset></dl></div>

        在 xcode 中安装 OpenCV

        时间:2023-10-23
          <legend id='Xh3WZ'><style id='Xh3WZ'><dir id='Xh3WZ'><q id='Xh3WZ'></q></dir></style></legend>

            <small id='Xh3WZ'></small><noframes id='Xh3WZ'>

            <tfoot id='Xh3WZ'></tfoot>
              <tbody id='Xh3WZ'></tbody>
            • <i id='Xh3WZ'><tr id='Xh3WZ'><dt id='Xh3WZ'><q id='Xh3WZ'><span id='Xh3WZ'><b id='Xh3WZ'><form id='Xh3WZ'><ins id='Xh3WZ'></ins><ul id='Xh3WZ'></ul><sub id='Xh3WZ'></sub></form><legend id='Xh3WZ'></legend><bdo id='Xh3WZ'><pre id='Xh3WZ'><center id='Xh3WZ'></center></pre></bdo></b><th id='Xh3WZ'></th></span></q></dt></tr></i><div id='Xh3WZ'><tfoot id='Xh3WZ'></tfoot><dl id='Xh3WZ'><fieldset id='Xh3WZ'></fieldset></dl></div>
                <bdo id='Xh3WZ'></bdo><ul id='Xh3WZ'></ul>
                  本文介绍了在 xcode 中安装 OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用下面的链接在我的项目中安装 opencv,但是我们如何在终端中生成命令我不知道有人可以帮助我吗?http://aptogo.co.uk/2011/09/opencv-ios 框架/

                  I use bellow link for install opencv in my project but how we generate command in terminal I don`t know can anybody help me?? http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

                  推荐答案

                  如果你想在 iOS 上使用 OpenCV,你应该使用 OpenCV 提供的官方框架(从 2.4.2 版本开始).

                  If you want to use OpenCV on iOS you should go with the official framework provided by OpenCV (as of version 2.4.2).

                  在此处获取最新版本:OpenCV for iOS,将其放入您的项目中并将其包含在您的项目前缀中:

                  Get the lastest version here: OpenCV for iOS, drop it into your project and include this into your project prefixes:

                  ExampleApp-Prefix.pch:

                  #ifdef __cplusplus
                      #import <opencv2/opencv.hpp>
                  #endif
                  

                  您还必须将 UIImage转换"为 cv::Mat 才能与 OpenCV 一起使用.

                  You'll also have to "convert" an UIImage to a cv::Mat to use it with OpenCV.

                  UIImageCVmatConverter.h:

                  //
                  //  UIImageCVMatConverter.h
                  //
                  
                  #import <Foundation/Foundation.h>
                  
                  @interface UIImageCVMatConverter : NSObject {
                  
                  }
                  
                  + (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat;
                  + (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat withUIImage:(UIImage*)image;
                  + (cv::Mat)cvMatFromUIImage:(UIImage *)image;
                  + (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image;
                  + (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image;
                  + (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image;
                  
                  @end
                  

                  UIImageCVmatConverter.mm:

                  //
                  //  UIImageCVMatConverter.mm
                  //
                  
                  #import "UIImageCVMatConverter.h"
                  
                  @implementation UIImageCVMatConverter
                  
                  + (UIImage *)UIImageFromCVMat:(cv::Mat)cvMat withUIImage:(UIImage*)image;
                  {
                    CGColorSpaceRef colorSpace = CGImageGetColorSpace( image.CGImage );
                      CGFloat cols = image.size.width;
                      CGFloat rows = image.size.height;
                      CGFloat widthStep = image.size.width;
                      CGContextRef contextRef = CGBitmapContextCreate( NULL, cols, rows, 8, widthStep*4, colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault );
                      CGContextDrawImage( contextRef, CGRectMake(0, 0, cols, rows), image.CGImage );
                      CGContextSetRGBStrokeColor( contextRef, 1, 0, 0, 1 );
                      CGImageRef cgImage = CGBitmapContextCreateImage( contextRef );
                      UIImage* result = [UIImage imageWithCGImage:cgImage];
                      CGImageRelease( cgImage );
                      CGContextRelease( contextRef );
                      CGColorSpaceRelease( colorSpace );
                      return result;
                  }
                  
                  +(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
                  {
                      NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
                      CGColorSpaceRef colorSpace;
                      if ( cvMat.elemSize() == 1 ) {
                          colorSpace = CGColorSpaceCreateDeviceGray();
                      }
                      else {
                          colorSpace = CGColorSpaceCreateDeviceRGB();
                      }
                      CGDataProviderRef provider = CGDataProviderCreateWithCFData( (__bridge CFDataRef)data );
                      CGImageRef imageRef = CGImageCreate( cvMat.cols, cvMat.rows, 8, 8 * cvMat.elemSize(), cvMat.step[0], colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault );
                      UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
                      CGImageRelease( imageRef );
                      CGDataProviderRelease( provider );
                      CGColorSpaceRelease( colorSpace );
                      return finalImage;
                  }
                  
                  + (cv::Mat)cvMatFromUIImage:(UIImage *)image
                  {
                      CGColorSpaceRef colorSpace = CGImageGetColorSpace( image.CGImage );
                      CGFloat cols = image.size.width;
                      CGFloat rows = image.size.height;
                      cv::Mat cvMat( rows, cols, CV_8UC4 );
                      CGContextRef contextRef = CGBitmapContextCreate( cvMat.data, cols, rows, 8, cvMat.step[0], colorSpace, kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault );
                      CGContextDrawImage( contextRef, CGRectMake(0, 0, cols, rows), image.CGImage );
                      CGContextRelease( contextRef );
                      CGColorSpaceRelease( colorSpace );
                      return cvMat;
                  }
                  
                  + (cv::Mat)cvMatGrayFromUIImage:(UIImage *)image
                  {
                    cv::Mat cvMat = [UIImageCVMatConverter cvMatFromUIImage:image];
                    cv::Mat grayMat;
                      if ( cvMat.channels() == 1 ) {
                          grayMat = cvMat;
                    }
                      else {
                          grayMat = cv :: Mat( cvMat.rows,cvMat.cols, CV_8UC1 );
                          cv::cvtColor( cvMat, grayMat, CV_BGR2GRAY );
                      }
                    return grayMat;
                  }
                  
                  + (UIImage *)scaleAndRotateImageBackCamera:(UIImage *)image
                  {
                    static int kMaxResolution = 640;
                    CGImageRef imgRef = image.CGImage;
                    CGFloat width = CGImageGetWidth( imgRef );
                    CGFloat height = CGImageGetHeight( imgRef );
                    CGAffineTransform transform = CGAffineTransformIdentity;
                    CGRect bounds = CGRectMake( 0, 0, width, height );
                    if ( width > kMaxResolution || height > kMaxResolution ) {
                      CGFloat ratio = width/height;
                      if ( ratio > 1 ) {
                        bounds.size.width = kMaxResolution;
                        bounds.size.height = bounds.size.width / ratio;
                      }
                          else {
                        bounds.size.height = kMaxResolution;
                        bounds.size.width = bounds.size.height * ratio;
                      }
                    }
                    CGFloat scaleRatio = bounds.size.width / width;
                    CGSize imageSize = CGSizeMake( CGImageGetWidth(imgRef), CGImageGetHeight(imgRef) );
                    CGFloat boundHeight;
                    UIImageOrientation orient = image.imageOrientation;
                    switch( orient ) {
                      case UIImageOrientationUp:
                        transform = CGAffineTransformIdentity;
                        break;
                      case UIImageOrientationUpMirrored:
                        transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
                        transform = CGAffineTransformScale(transform, -1.0, 1.0);
                        break;
                      case UIImageOrientationDown:
                        transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
                        transform = CGAffineTransformRotate(transform, M_PI);
                        break;
                      case UIImageOrientationDownMirrored:
                        transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
                        transform = CGAffineTransformScale(transform, 1.0, -1.0);
                        break;
                      case UIImageOrientationLeftMirrored:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
                        transform = CGAffineTransformScale(transform, -1.0, 1.0);
                        transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                        break;
                      case UIImageOrientationLeft:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
                        transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                        break;
                      case UIImageOrientationRightMirrored:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeScale(-1.0, 1.0);
                        transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                        break;
                      case UIImageOrientationRight:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
                        transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                        break;
                      default:
                        [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
                    }
                    UIGraphicsBeginImageContext( bounds.size );
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    if ( orient == UIImageOrientationRight || orient == UIImageOrientationLeft ) {
                      CGContextScaleCTM( context, -scaleRatio, scaleRatio );
                      CGContextTranslateCTM( context, -height, 0 );
                    }
                      else {
                      CGContextScaleCTM( context, scaleRatio, -scaleRatio );
                      CGContextTranslateCTM( context, 0, -height );
                    }
                    CGContextConcatCTM( context, transform );
                    CGContextDrawImage( UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef );
                    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    return returnImage;
                  }
                  
                  + (UIImage *)scaleAndRotateImageFrontCamera:(UIImage *)image
                  {
                    static int kMaxResolution = 640;
                    CGImageRef imgRef = image.CGImage;
                    CGFloat width = CGImageGetWidth(imgRef);
                    CGFloat height = CGImageGetHeight(imgRef);
                    CGAffineTransform transform = CGAffineTransformIdentity;
                    CGRect bounds = CGRectMake( 0, 0, width, height);
                    if (width > kMaxResolution || height > kMaxResolution) {
                      CGFloat ratio = width/height;
                      if (ratio > 1) {
                        bounds.size.width = kMaxResolution;
                        bounds.size.height = bounds.size.width / ratio;
                      } else {
                        bounds.size.height = kMaxResolution;
                        bounds.size.width = bounds.size.height * ratio;
                      }
                    }
                  
                    CGFloat scaleRatio = bounds.size.width / width;
                    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
                    CGFloat boundHeight;
                    UIImageOrientation orient = image.imageOrientation;
                    switch(orient) {
                      case UIImageOrientationUp:
                        transform = CGAffineTransformIdentity;
                        break;
                      case UIImageOrientationUpMirrored:
                        transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
                        transform = CGAffineTransformScale(transform, -1.0, 1.0);
                        break;
                      case UIImageOrientationDown:
                        transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
                        transform = CGAffineTransformRotate(transform, M_PI);
                        break;
                      case UIImageOrientationDownMirrored:
                        transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
                        transform = CGAffineTransformScale(transform, 1.0, -1.0);
                        break;
                      case UIImageOrientationLeftMirrored:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
                        transform = CGAffineTransformScale(transform, -1.0, 1.0);
                        transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                        break;
                      case UIImageOrientationLeft:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
                        transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                        break;
                          case UIImageOrientationRight:
                      case UIImageOrientationRightMirrored:
                        boundHeight = bounds.size.height;
                        bounds.size.height = bounds.size.width;
                        bounds.size.width = boundHeight;
                        transform = CGAffineTransformMakeScale(-1.0, 1.0);
                        transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                        break;
                          default:
                        [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
                    }
                    UIGraphicsBeginImageContext( bounds.size );
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    if ( orient == UIImageOrientationRight || orient == UIImageOrientationLeft ) {
                      CGContextScaleCTM(context, -scaleRatio, scaleRatio);
                      CGContextTranslateCTM(context, -height, 0);
                    }
                      else {
                      CGContextScaleCTM(context, scaleRatio, -scaleRatio);
                      CGContextTranslateCTM(context, 0, -height);
                    }
                    CGContextConcatCTM( context, transform );
                    CGContextDrawImage( UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef );
                    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    return returnImage;
                  }
                  
                  @end
                  

                  将视图控制器实现文件重命名为 *.mm

                  Rename your view controller implementation file to *.mm

                  MyViewController.m -> MyViewController.mm
                  

                  并在您的视图控制器中导入 UIImageCVMatConverter:

                  And import the UIImageCVMatConverter in your view controller:

                  #import "UIImageCVMatConverter.h"
                  

                  现在您可以在视图控制器中混合使用 Objective-C 和 C++ OpenCV 代码:

                  Now you can mix Objective-C and C++ OpenCV code inside your view controller:

                  cv::Mat img = [UIImageCVMatConverter cvMatFromUIImage:[UIImage imageNamed:@"my_image.png"]];
                  ...
                  

                  玩得开心!

                  这篇关于在 xcode 中安装 OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 AVFoundation 切换相机时视频冻结 下一篇:MPMoviePlayerController 添加 UIButton 以查看淡入淡出的控件

                  相关文章

                    <small id='ZOoeI'></small><noframes id='ZOoeI'>

                    <tfoot id='ZOoeI'></tfoot>

                  1. <legend id='ZOoeI'><style id='ZOoeI'><dir id='ZOoeI'><q id='ZOoeI'></q></dir></style></legend>
                    <i id='ZOoeI'><tr id='ZOoeI'><dt id='ZOoeI'><q id='ZOoeI'><span id='ZOoeI'><b id='ZOoeI'><form id='ZOoeI'><ins id='ZOoeI'></ins><ul id='ZOoeI'></ul><sub id='ZOoeI'></sub></form><legend id='ZOoeI'></legend><bdo id='ZOoeI'><pre id='ZOoeI'><center id='ZOoeI'></center></pre></bdo></b><th id='ZOoeI'></th></span></q></dt></tr></i><div id='ZOoeI'><tfoot id='ZOoeI'></tfoot><dl id='ZOoeI'><fieldset id='ZOoeI'></fieldset></dl></div>

                        <bdo id='ZOoeI'></bdo><ul id='ZOoeI'></ul>