objective c - I get the following error [NSImage CVMat]: unrecognized selector sent to instance -
first disclaimer: new cocoa , objective-c. trying learn work cocoa , opencv don't have deal qt. if there easier way achieve i'm trying do, i'd welcome tips on that. also, if question has been answered, i'd appreciate if point me answer.
i using code post: nsimage cv::mat , vice versa try , convert nsimage mat , back. problem is, end getting unrecognized selector sent instance error. far understand error, thrown when method doesn't exist called. method calling exist. i'm @ complete loss here , appreciate in dumbed down words. code same post linked above. error follows:
2013-08-15 17:24:52.554 espejismo[2368:303] -[nsimage cvmat]: unrecognized selector sent instance 0x1020198d0 2013-08-15 17:25:03.969 espejismo[2368:303] uncaught exception raised 2013-08-15 17:25:03.969 espejismo[2368:303] -[nsimage cvmat]: unrecognized selector sent instance 0x1020198d0 2013-08-15 17:25:03.972 espejismo[2368:303] ( 0 corefoundation 0x00007fff93e76b06 __exceptionpreprocess + 198 1 libobjc.a.dylib 0x00007fff97e0c3f0 objc_exception_throw + 43 2 corefoundation 0x00007fff93f0d40a -[nsobject(nsobject) doesnotrecognizeselector:] + 186 3 corefoundation 0x00007fff93e6502e ___forwarding___ + 414 4 corefoundation 0x00007fff93eaadad __forwarding_prep_1___ + 237 5 espejismo 0x000000010000209e -[testview initwithframe:] + 334 6 appkit 0x00007fff942c00a3 -[nscustomview nibinstantiatewithobjectinstantiator:] + 657 7 appkit 0x00007fff9429f625 -[nsibobjectdata instantiateobject:] + 266 8 appkit 0x00007fff9429edf7 -[nsibobjectdata nibinstantiatewithowner:toplevelobjects:] + 337 9 appkit 0x00007fff9427e11d loadnib + 317 10 appkit 0x00007fff9427d649 +[nsbundle(nsnibloading) _loadnibfile:nametable:withzone:ownerbundle:] + 219 11 appkit 0x00007fff9427d47e -[nsbundle(nsnibloading) loadnibnamed:owner:toplevelobjects:] + 200 12 appkit 0x00007fff9427d25e +[nsbundle(nsnibloading) loadnibnamed:owner:] + 360 13 appkit 0x00007fff942799ff nsapplicationmain + 398 14 espejismo 0x0000000100001112 main + 34 15 libdyld.dylib 0x00007fff922077e1 start + 0 ) 2013-08-15 17:25:03.973 espejismo[2368:303] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[nsimage cvmat]: unrecognized selector sent instance 0x1020198d0' *** first throw call stack: ( 0 corefoundation 0x00007fff93e76b06 __exceptionpreprocess + 198 1 libobjc.a.dylib 0x00007fff97e0c3f0 objc_exception_throw + 43 2 corefoundation 0x00007fff93f0d40a -[nsobject(nsobject) doesnotrecognizeselector:] + 186 3 corefoundation 0x00007fff93e6502e ___forwarding___ + 414 4 corefoundation 0x00007fff93eaadad __forwarding_prep_1___ + 237 5 espejismo 0x000000010000209e -[testview initwithframe:] + 334 6 appkit 0x00007fff942c00a3 -[nscustomview nibinstantiatewithobjectinstantiator:] + 657 7 appkit 0x00007fff9429f625 -[nsibobjectdata instantiateobject:] + 266 8 appkit 0x00007fff9429edf7 -[nsibobjectdata nibinstantiatewithowner:toplevelobjects:] + 337 9 appkit 0x00007fff9427e11d loadnib + 317 10 appkit 0x00007fff9427d649 +[nsbundle(nsnibloading) _loadnibfile:nametable:withzone:ownerbundle:] + 219 11 appkit 0x00007fff9427d47e -[nsbundle(nsnibloading) loadnibnamed:owner:toplevelobjects:] + 200 12 appkit 0x00007fff9427d25e +[nsbundle(nsnibloading) loadnibnamed:owner:] + 360 13 appkit 0x00007fff942799ff nsapplicationmain + 398 14 espejismo 0x0000000100001112 main + 34 15 libdyld.dylib 0x00007fff922077e1 start + 0 ) libc++abi.dylib: terminate called throwing exception
this error thrown @ following line:
cvmat_test = [image cvmat];
which i'm assuming means xcode can't see cvmat method. however, xcode lists method 1 of methods project when try call instance of nsimage.
here's code other post linked in case people don't want click over:
// // nsimage+opencv.h // #import <appkit/appkit.h> @interface nsimage (nsimage_opencv) { } +(nsimage*)imagewithcvmat:(const cv::mat&)cvmat; -(id)initwithcvmat:(const cv::mat&)cvmat; @property(nonatomic, readonly) cv::mat cvmat; @property(nonatomic, readonly) cv::mat cvgrayscalemat; @end
the implementation file:
// // nsimage+opencv.mm // #import "nsimage+opencv.h" static void providerreleasedatanop(void *info, const void *data, size_t size) { return; } @implementation nsimage (nsimage_opencv) -(cgimageref)cgimage { cgcontextref bitmapctx = cgbitmapcontextcreate(null/*data - pass null let cg allocate memory*/, [self size].width, [self size].height, 8 /*bitspercomponent*/, 0 /*bytesperrow - cg calculate if it's allocating data. might padded out bit better alignment*/, [[nscolorspace genericrgbcolorspace] cgcolorspace], kcgbitmapbyteorder32host|kcgimagealphapremultipliedfirst); [nsgraphicscontext savegraphicsstate]; [nsgraphicscontext setcurrentcontext:[nsgraphicscontext graphicscontextwithgraphicsport:bitmapctx flipped:no]]; [self drawinrect:nsmakerect(0,0, [self size].width, [self size].height) fromrect:nszerorect operation:nscompositecopy fraction:1.0]; [nsgraphicscontext restoregraphicsstate]; cgimageref cgimage = cgbitmapcontextcreateimage(bitmapctx); cgcontextrelease(bitmapctx); return cgimage; } -(cv::mat)cvmat { cgimageref imageref = [self cgimage]; cgcolorspaceref colorspace = cgimagegetcolorspace(imageref); cgfloat cols = self.size.width; cgfloat rows = self.size.height; cv::mat cvmat(rows, cols, cv_8uc4); // 8 bits per component, 4 channels cgcontextref contextref = cgbitmapcontextcreate(cvmat.data, // pointer backing data cols, // width of bitmap rows, // height of bitmap 8, // bits per component cvmat.step[0], // bytes per row colorspace, // colorspace kcgimagealphanoneskiplast | kcgbitmapbyteorderdefault); // bitmap info flags cgcontextdrawimage(contextref, cgrectmake(0, 0, cols, rows), imageref); cgcontextrelease(contextref); cgimagerelease(imageref); return cvmat; } -(cv::mat)cvgrayscalemat { cgimageref imageref = [self cgimage]; cgcolorspaceref colorspace = cgcolorspacecreatedevicegray(); cgfloat cols = self.size.width; cgfloat rows = self.size.height; cv::mat cvmat = cv::mat(rows, cols, cv_8uc1); // 8 bits per component, 1 channel cgcontextref contextref = cgbitmapcontextcreate(cvmat.data, // pointer backing data cols, // width of bitmap rows, // height of bitmap 8, // bits per component cvmat.step[0], // bytes per row colorspace, // colorspace kcgimagealphanone | kcgbitmapbyteorderdefault); // bitmap info flags cgcontextdrawimage(contextref, cgrectmake(0, 0, cols, rows), imageref); cgcontextrelease(contextref); cgcolorspacerelease(colorspace); cgimagerelease(imageref); return cvmat; } + (nsimage *)imagewithcvmat:(const cv::mat&)cvmat { return [[[nsimage alloc] initwithcvmat:cvmat] autorelease]; } - (id)initwithcvmat:(const 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, // width cvmat.rows, // height 8, // bits per component 8 * cvmat.elemsize(), // bits per pixel cvmat.step[0], // bytes per row colorspace, // colorspace kcgimagealphanone | kcgbitmapbyteorderdefault, // bitmap info flags provider, // cgdataproviderref null, // decode false, // should interpolate kcgrenderingintentdefault); // intent nsbitmapimagerep *bitmaprep = [[nsbitmapimagerep alloc] initwithcgimage:imageref]; nsimage *image = [[nsimage alloc] init]; [image addrepresentation:bitmaprep]; cgimagerelease(imageref); cgdataproviderrelease(provider); cgcolorspacerelease(colorspace); return image; } @end
my use of file:
#import "appdelegate.h" @implementation appdelegate - (void)dealloc { [super dealloc]; } - (void)applicationdidfinishlaunching:(nsnotification *)anotification { // insert code here initialize application cv::mat cvmat_test; nsimage *image = [nsimage imagenamed:@"image.jpg"]; cvmat_test = [image cvmat]; } @end
ps: have tried adding "-all_load" "other linker flags" doesn't work either.
adding nsimage+opencv.mm file project isn't enough; have add target. can file inspector when file selected, or dragging project navigator target's compile sources build phase.
in future, when add new existing source files project, on list of targets in options sheet , make sure right targets checked.
Comments
Post a Comment