openCv IOS - 没有可行的重载'='

Posted

技术标签:

【中文标题】openCv IOS - 没有可行的重载\'=\'【英文标题】:openCv IOS -No viable Overloaded '='openCv IOS - 没有可行的重载'=' 【发布时间】:2015-03-05 21:59:17 【问题描述】:

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

这是 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);

【问题讨论】:

尝试 filter = Ptr(new RetroFilter(params); 而不是 filter = NULL;试试 filter = cv::Ptr::Ptr(); 第二条评论有效-但第一个无效-我尝试在它之前给出一个 cv:: ,然后它有效:尝试 filter = cv::Ptr(new RetroFilter(params) ;@KirkSpaziani 【参考方案1】:

第一个问题,分配指针:

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

第二期,空指针:

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

原因是 cv::Ptr 对象没有使这更简单的覆盖。标准库的智能指针类在易于使用方面做得更好。

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

Ptr& operator = (const Ptr& ptr);

这意味着你不能给它分配一个RetroFilter,只能分配另一个cv::Ptr,所以你需要已经包装了RetroFilter。

第二个问题,与第一个问题类似,没有覆盖 = 运算符接受 NULL。表达空 cv::Ptr 的最佳方式是

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

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

很高兴我能帮上忙!

【讨论】:

【参考方案2】:

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

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

【讨论】:

以上是关于openCv IOS - 没有可行的重载'='的主要内容,如果未能解决你的问题,请参考以下文章

即使我重载了赋值,也没有可行的重载'='错误

自定义类没有可行的重载运算符 []

类型匹配没有可行的重载运算符

没有可行的重载'='用于将std :: function回调赋值为成员函数

OpenCV iOS开发——安装(转)

没有注册 OpKernel 以在 iOS 上使用这些属性支持 Op'Switch'