Showing posts with label xcode. Show all posts
Showing posts with label xcode. Show all posts

Thursday, November 11, 2010

Using OpenCV on the Mac and iPhone

Its been a while since I've posted because of winter break, finals, and the new year. So now its back to the grind. Since i last posted, I was able to grab web data. The easiest way to ping a website is by simply instantiating a string with the websites source code. Its something like NSString *t = [InitFromURL:www.google.com] or something like that. So that makes it extremely easy to process web requests. I just got OpenCV to work in XCode and will post a more detailed explanation of how to go about doing so in the next week. I also plan on trying to port OpenCV to the iphone and possibly doing somesort of biometric application...

Convert IplImage to NSImage in XCode


Convert IplImage to NSImage in XCode

After doing some searching around I ran across this piece of code.

/* IplImage to NSImage
 * Kelly Breed
 */


NSImage* opencvImageToNSImage(IplImage *img){
char *d = img->imageData; // Get a pointer to the IplImage image data.
NSString *COLORSPACE;
if(img->nChannels == 1){
COLORSPACE = NSDeviceWhiteColorSpace;
}
else{
COLORSPACE = NSDeviceRGBColorSpace;
}
NSBitmapImageRep *bmp = [[NSBitmapImageRep alloc]  
 initWithBitmapDataPlanes:NULL pixelsWide:img->width pixelsHigh:img->heightbitsPerSample:img->depth samplesPerPixel:img->nChannels  
 hasAlpha:NO isPlanar:NO colorSpaceName:COLORSPACE bytesPerRow:img->widthStepbitsPerPixel:0];
// Move the IplImage data into the NSBitmapImageRep. widthStep is used in the inner for loop due to the
//   difference between actual bytes in the former and pixel locations in the latter.
// Assignment to colors[] is reversed because that's how an IplImage stores the data.
int x, y;
unsigned int colors[3];
for(y=0; yheight; y++){
for(x=0; xwidth; x++){
if(img->nChannels > 1){
colors[2] = (unsigned int) d[(y * img->widthStep) + (x*3)]; // x*3 due to difference between pixel coords and actual byte layout.
colors[1] = (unsigned int) d[(y * img->widthStep) + (x*3)+1];
colors[0] = (unsigned int) d[(y * img->widthStep) + (x*3)+2];
}
else{
colors[0] = (unsigned int)d[(y * img->width) + x];
//NSLog(@"colors[0] = %d", colors[0]);
}
[bmp setPixel:colors atX:x y:y];
}
}
NSData *tif = [bmp TIFFRepresentation];
NSImage *im = [[NSImage alloc] initWithData:tif];
return [im autorelease];
}

It appears that it converts an IplImage to an NSImage. I got it from here. It seems like it's going to work, I'll post again in the next few days with an update. If it does work, I'll be able to write more GUI friendly based applications with openCV because I'll be able to use interface builder to build all of the GUI components.

Followers