openCv IOS -No可行重载'='

时间:2015-03-05 21:59:17

标签: c++ ios objective-c swift opencv

我正在运行一本书的示例代码,它是关于在IOS设备上使用openCv进行视频处理。但我得到“没有可行的重载'='”错误,我确实搜索了StackOverFlow,发现了一些类似的帖子和答案,但所有解决方案对我都不起作用,所以我发布如下代码,希望任何人都能给出一些建议。真的很感激!

这是ViewController.h文件:

#import <UIKit/UIKit.h>
#import <opencv2/imgcodecs/ios.h>
#import "CvEffects/RetroFilter.hpp"
#import <opencv2/videoio/cap_ios.h>


@interface ViewController : UIViewController<CvVideoCameraDelegate>
{
   CvVideoCamera* videoCamera;
BOOL isCapturing;
RetroFilter::Parameters params;
cv::Ptr<RetroFilter> filter;
uint64_t prevTime;
}

@property (nonatomic, strong) CvVideoCamera* videoCamera;
@property (nonatomic, strong) IBOutlet UIImageView* imageView;
@property (nonatomic, strong) IBOutlet UIToolbar* toolbar;
@property (nonatomic, weak) IBOutlet
UIBarButtonItem* startCaptureButton;
@property (nonatomic, weak) IBOutlet
UIBarButtonItem* stopCaptureButton;

-(IBAction)startCaptureButtonPressed:(id)sender;
-(IBAction)stopCaptureButtonPressed:(id)sender;

@end

这是ViewController.m文件:

#import "ViewController.h"
#import <mach/mach_time.h> 

@interface ViewController ()

@end

@implementation ViewController

@synthesize imageView;
@synthesize startCaptureButton;
@synthesize toolbar;
@synthesize videoCamera;


- (void)viewDidLoad
{
    [super viewDidLoad];

    // Initialize camera
    videoCamera = [[CvVideoCamera alloc]
                   initWithParentView:imageView];
videoCamera.delegate = self;
videoCamera.defaultAVCaptureDevicePosition =
                            AVCaptureDevicePositionFront;
videoCamera.defaultAVCaptureSessionPreset =
                            AVCaptureSessionPreset352x288;
videoCamera.defaultAVCaptureVideoOrientation =
                            AVCaptureVideoOrientationPortrait;
videoCamera.defaultFPS = 30;

isCapturing = NO;

// Load textures
UIImage* resImage = [UIImage imageNamed:@"scratches.png"];
UIImageToMat(resImage, params.scratches);

resImage = [UIImage imageNamed:@"fuzzy_border.png"];
UIImageToMat(resImage, params.fuzzyBorder);


    filter = NULL;
    prevTime = mach_absolute_time();
}

- (NSInteger)supportedInterfaceOrientations
{
    // Only portrait orientation
return UIInterfaceOrientationMaskPortrait;
}

-(IBAction)startCaptureButtonPressed:(id)sender
{
    [videoCamera start];
    isCapturing = YES;

    params.frameSize = cv::Size(videoCamera.imageWidth,
                            videoCamera.imageHeight);

    if (!filter)
        filter = new RetroFilter(params);
}

-(IBAction)stopCaptureButtonPressed:(id)sender
{
    [videoCamera stop];
    isCapturing = NO;
}

//TODO: may be remove this code
static double machTimeToSecs(uint64_t time)
{
    mach_timebase_info_data_t timebase;
    mach_timebase_info(&timebase);
    return (double)time * (double)timebase.numer /
                      (double)timebase.denom / 1e9;
}

// Macros for time measurements
#if 1
#define TS(name) int64 t_##name = cv::getTickCount()
#define TE(name) printf("TIMER_" #name ": %.2fms\n", \
1000.*((cv::getTickCount() - t_##name) / cv::getTickFrequency()))
#else
#define TS(name)
#define TE(name)
#endif

- (void)processImage:(cv::Mat&)image
{
    cv::Mat inputFrame = image;

    BOOL isNeedRotation = image.size() != params.frameSize;
    if (isNeedRotation)
        inputFrame = image.t();

    // Apply filter
    cv::Mat finalFrame;
    TS(ApplyingFilter);
    filter->applyToVideo(inputFrame, finalFrame);
    TE(ApplyingFilter);

    if (isNeedRotation)
        finalFrame = finalFrame.t();

    // Add fps label to the frame
    uint64_t currTime = mach_absolute_time();
    double timeInSeconds = machTimeToSecs(currTime - prevTime);
    prevTime = currTime;
    double fps = 1.0 / timeInSeconds;
    NSString* fpsString =
                    [NSString stringWithFormat:@"FPS = %3.2f", fps];
    cv::putText(finalFrame, [fpsString UTF8String],
                cv::Point(30, 30), cv::FONT_HERSHEY_COMPLEX_SMALL,
                0.8, cv::Scalar::all(255));

    finalFrame.copyTo(image);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if (isCapturing)
    {
        [videoCamera stop];
    }
}

- (void)dealloc
{
    videoCamera.delegate = nil;
}

@end

我在两个陈述中得到了错误:

filter = NULL;

filter = new RetroFilter(params);

2 个答案:

答案 0 :(得分:0)

第一个问题,分配指针:

filter = Ptr<RetroFilter>(new RetroFilter(params));

第二个问题,取消指针:

filter = cv::Ptr<RetroFilter>::Ptr();

原因是cv :: Ptr对象没有覆盖使得这更简单。标准库的智能指针类可以更好地使用它。

第一个问题是提供的唯一=运算符是:

Ptr& operator = (const Ptr& ptr);

这意味着你不能为它分配一个RetroFilter,只能指定另一个cv :: Ptr,所以你需要包装RetroFilter。

第二个问题,类似于第一个问题,没有带有NULL的override =运算符。表达null cv :: Ptr的最佳方法是

cv::Ptr<RetroFilter>::Ptr();

其中,作为cv :: Ptr的实例可以使用'='运算符分配。

很高兴我能帮忙!

答案 1 :(得分:-1)

非常感谢@KirkSpaziani。 我认为以下代码有效,但不知道为什么?

Try filter = cv::Ptr<RetroFilter>(new RetroFilter(params);
filter = cv::Ptr<RetroFilter>::Ptr()