Reputation: 452
Got an other problem with my opencv project on iOS.
So I have a small project written in Ojective C with 2 methods written in C++ :
My ViewController.hh file :
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface ViewController : UIViewController
static UIImage* MatToUIImage(const cv::Mat& m);
static void UIImageToMat(const UIImage* image, cv::Mat& m);
@end
and my ViewController.mm file :
#import "ViewController.hh"
@interface ViewController ()
@end
@implementation ViewController
static UIImage* MatToUIImage(const cv::Mat& m) {
CV_Assert(m.depth() == CV_8U);
NSData *data = [NSData dataWithBytes:m.data length:m.elemSize()*m.total()];
CGColorSpaceRef colorSpace = m.channels() == 1 ?
CGColorSpaceCreateDeviceGray() : CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
// Creating CGImage from cv::Mat
CGImageRef imageRef = CGImageCreate(m.cols, m.cols, m.elemSize1()*8, m.elemSize()*8,
m.step[0], colorSpace, kCGImageAlphaNoneSkipLast|kCGBitmapByteOrderDefault,
provider, NULL, false, kCGRenderingIntentDefault);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef); CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace); return finalImage;
}
static void UIImageToMat(const UIImage* image, cv::Mat& m) {
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width, rows = image.size.height;
m.create(rows, cols, CV_8UC4); // 8 bits per component, 4 channels
CGContextRef contextRef = CGBitmapContextCreate(m.data, m.cols, m.rows, 8, m.step[0], colorSpace, kCGImageAlphaNoneSkipLast |kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef); CGColorSpaceRelease(colorSpace);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString* filename = [[NSBundle mainBundle] pathForResource:@"cmc7" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:filename];
//cv::Mat inputMat = self.UIImageToMat(image);
cv::Mat mat;
UIImageToMat(image, mat);
UIImage *newImage = MatToUIImage(mat);
self.view.backgroundColor = [UIColor colorWithPatternImage:newImage];
}
I included my opencv framework this way :
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
I precise that I have a similar project with the same includes, but with methods written in Objective C, and not in C++, and it works.
So here is my log error.
Undefined symbols for architecture i386:
"cv::Exception::Exception(int, std::string const&, std::string const&, std::string const&, int)", referenced from:
__ZL12MatToUIImageRKN2cv3MatE in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So what's the problem ? I really would prefer to have methods written in C++, because it's more common to use openCV using C++ language instead of Objective C.
Thank you very much.
Upvotes: 0
Views: 3194
Reputation: 327
Try changing the 'C++ standard library' and 'C++ language dialect' in build settings to Complier Default.
Upvotes: 1
Reputation: 194
Maybe your project is not linking against the C++ runtime library
Build Phases > Link Binary With Libraries > + button > choose "libc++.dylib" for OpenCV 2.4.3, or
"libstdc++.dylib" for OpenCV 2.4.2 and before.
and rebuild should work
Upvotes: 1