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

openCv IOS -No viable Overloaded '='

本文关键字:IOS openCv      更新时间:2023-10-16

我正在运行一本书中的示例代码,它是关于在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 ": %.2fmsn", 
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<RetroFilter>(new RetroFilter(params));

第二个问题,清空指针:

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

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

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

Ptr& operator = (const Ptr& ptr);

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

第二个问题与第一个问题类似,没有采用NULL的override=运算符。表示空cv::Ptr的最佳方式是

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

作为cv::Ptr的实例,可以使用'='运算符进行赋值。

很高兴我能帮忙!

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

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