Created
July 9, 2013 02:45
-
-
Save ozw-sei/5954297 to your computer and use it in GitHub Desktop.
平均値シフト法による画像領域分割 on iOS
※要OpenCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ViewController.h" | |
| #import <opencv2/opencv.hpp> | |
| @interface ViewController () | |
| @end | |
| @implementation ViewController | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| // Do any additional setup after loading the view, typically from a nib. | |
| UIImage *src = [UIImage imageNamed:@"mona"]; | |
| IplImage *cvsrc = [self IplImageFromUIImage:src]; | |
| CvMemStorage *storage = 0; | |
| CvSeq *comp = 0; | |
| storage = cvCreateMemStorage (0); | |
| IplImage *cvdst = cvCloneImage (cvsrc); | |
| cvPyrMeanShiftFiltering(cvsrc, cvdst,30,30, 2, cvTermCriteria (CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 5, 1)); | |
| cvReleaseMemStorage (&storage); | |
| UIImage *img = [self UIImageFromIplImage:cvdst]; | |
| UIImageView *imageView = [[UIImageView alloc] initWithImage:img]; | |
| [self.view addSubview:imageView]; | |
| } | |
| - (void)didReceiveMemoryWarning | |
| { | |
| [super didReceiveMemoryWarning]; | |
| // Dispose of any resources that can be recreated. | |
| } | |
| - (IplImage*)IplImageFromUIImage:(UIImage*)image | |
| { | |
| CGImageRef imageRef = image.CGImage; | |
| CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
| IplImage *iplimage = cvCreateImage(cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4 ); | |
| CGContextRef contextRef = CGBitmapContextCreate( | |
| iplimage->imageData, | |
| iplimage->width, | |
| iplimage->height, | |
| iplimage->depth, | |
| iplimage->widthStep, | |
| colorSpace, | |
| kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault); | |
| CGContextDrawImage(contextRef, | |
| CGRectMake(0, 0, image.size.width, image.size.height), | |
| imageRef); | |
| CGContextRelease(contextRef); | |
| CGColorSpaceRelease(colorSpace); | |
| IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3); | |
| cvCvtColor(iplimage, ret, CV_RGBA2BGR); | |
| cvReleaseImage(&iplimage); | |
| return ret; | |
| } | |
| - (UIImage*)UIImageFromIplImage:(IplImage*)image | |
| { | |
| CGColorSpaceRef colorSpace; | |
| if (image->nChannels == 1) | |
| { | |
| colorSpace = CGColorSpaceCreateDeviceGray(); | |
| } else { | |
| colorSpace = CGColorSpaceCreateDeviceRGB(); | |
| //BGRになっているのでRGBに変換 | |
| cvCvtColor(image, image, CV_BGR2RGB); | |
| } | |
| NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize]; | |
| CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); | |
| CGImageRef imageRef = CGImageCreate(image->width, | |
| image->height, | |
| image->depth, | |
| image->depth * image->nChannels, | |
| image->widthStep, | |
| colorSpace, | |
| kCGImageAlphaNone|kCGBitmapByteOrderDefault, | |
| provider, | |
| NULL, | |
| false, | |
| kCGRenderingIntentDefault | |
| ); | |
| UIImage *ret = [UIImage imageWithCGImage:imageRef]; | |
| CGImageRelease(imageRef); | |
| CGDataProviderRelease(provider); | |
| CGColorSpaceRelease(colorSpace); | |
| return ret; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment