1 #import "ViewController.h"
6 #ifndef DOXYGEN_SHOULD_SKIP_THIS
7 @interface ViewController ()
10 @implementation ViewController
13 NSArray *process = [[NSArray alloc]initWithObjects:@"load image", @"convert to gray", @"compute gradient",
14 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
19 @synthesize myImageView;
27 UIImage *myScreenShot = [UIImage imageNamed:@"monkey.png"];
30 self.myImageView = [[UIImageView alloc] initWithImage:myScreenShot];
33 CGRect myFrame = CGRectMake(0.0f, 0.0f,
self.myImageView.frame.size.width*2,
self.myImageView.frame.size.height*2);
34 [
self.myImageView setFrame:myFrame];
37 [
self.view addSubview:self.myImageView];
40 CGFloat posx=140, posy=350;
42 CGSize button_size = CGSizeMake( 150, 25 );
43 for (
int i=0; i<[process count]; i++) {
44 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
45 [button addTarget:self action:@selector(checkButtonClick:) forControlEvents:UIControlEventTouchUpInside];
46 [button setTitle:[process objectAtIndex: i] forState:UIControlStateNormal];
48 button.frame = CGRectMake(posx, posy+i*padding, button_size.width, button_size.height);
49 [button setBackgroundColor:[UIColor blueColor]];
50 [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
51 button.layer.cornerRadius = 10;
52 [
self.view addSubview:button];
56 - (void) checkButtonClick:(UIButton *)paramSender{
58 UIButton *myButton = paramSender;
61 if([myButton.currentTitle isEqualToString:[process objectAtIndex: 0]]){
63 NSLog(
@"Clicked on \"%@\
" button ", [process objectAtIndex: 0]);
65 [myImageView setImage:[UIImage imageNamed:@"monkey.png"]];
67 else if([myButton.currentTitle isEqualToString:[process objectAtIndex: 1]]){
69 NSLog(
@"Clicked on \"%@\
" button ", [process objectAtIndex: 1]);
71 UIImage *img = [UIImage imageNamed:@"monkey.png"];
73 [myImageView setImage:[
self UIImageFromVpImageGray:gray]];
75 else if([myButton.currentTitle isEqualToString:[process objectAtIndex: 2]]){
77 NSLog(
@"Clicked on \"%@\
" button ", [process objectAtIndex: 2]);
79 UIImage *img = [UIImage imageNamed:@"monkey.png"];
85 [myImageView setImage:[
self UIImageFromVpImageGray:gray]];
87 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
88 else if([myButton.currentTitle isEqualToString:[process objectAtIndex: 3]]){
90 NSLog(
@"Clicked on \"%@\
" button ", [process objectAtIndex: 3]);
92 UIImage *img = [UIImage imageNamed:@"monkey.png"];
96 [myImageView setImage:[
self UIImageFromVpImageGray:canny]];
105 CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
107 if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
108 NSLog(
@"Input UIImage is grayscale");
111 CGContextRef contextRef = CGBitmapContextCreate(gray.bitmap,
118 kCGBitmapByteOrderDefault);
120 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
121 CGContextRelease(contextRef);
129 NSLog(
@"Input UIImage is color");
132 colorSpace = CGColorSpaceCreateDeviceRGB();
134 CGContextRef contextRef = CGBitmapContextCreate(color.bitmap,
138 4 * image.size.width,
140 kCGImageAlphaNoneSkipLast |
141 kCGBitmapByteOrderDefault);
143 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
144 CGContextRelease(contextRef);
153 - (
vpImage<
unsigned char>)vpImageGrayFromUIImage:(UIImage *)image
155 CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
157 if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
158 NSLog(
@"Input UIImage is grayscale");
161 CGContextRef contextRef = CGBitmapContextCreate(gray.bitmap,
168 kCGBitmapByteOrderDefault);
170 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
171 CGContextRelease(contextRef);
175 NSLog(
@"Input UIImage is color");
178 colorSpace = CGColorSpaceCreateDeviceRGB();
180 CGContextRef contextRef = CGBitmapContextCreate(color.bitmap,
184 4 * image.size.width,
186 kCGImageAlphaNoneSkipLast |
187 kCGBitmapByteOrderDefault);
189 CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
190 CGContextRelease(contextRef);
204 NSData *data = [NSData dataWithBytes:I.bitmap length:I.getSize()*4];
205 CGColorSpaceRef colorSpace;
207 colorSpace = CGColorSpaceCreateDeviceRGB();
209 CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
212 CGImageRef imageRef = CGImageCreate(I.getWidth(),
218 kCGImageAlphaNone|kCGBitmapByteOrderDefault,
222 kCGRenderingIntentDefault
227 UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
228 CGImageRelease(imageRef);
229 CGDataProviderRelease(provider);
230 CGColorSpaceRelease(colorSpace);
238 -(UIImage *)UIImageFromVpImageGray:(
vpImage<
unsigned char>)I
240 NSData *data = [NSData dataWithBytes:I.bitmap length:I.getSize()];
241 CGColorSpaceRef colorSpace;
243 colorSpace = CGColorSpaceCreateDeviceGray();
245 CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
248 CGImageRef imageRef = CGImageCreate(I.getWidth(),
254 kCGImageAlphaNone|kCGBitmapByteOrderDefault,
258 kCGRenderingIntentDefault
263 UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
264 CGImageRelease(imageRef);
265 CGDataProviderRelease(provider);
266 CGColorSpaceRelease(colorSpace);
272 - (void)didReceiveMemoryWarning {
273 [
super didReceiveMemoryWarning];
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void getGradX(const vpImage< unsigned char > &I, vpImage< double > &dIx)
static void canny(const vpImage< unsigned char > &I, vpImage< unsigned char > &Ic, const unsigned int gaussianFilterSize, const double thresholdCanny, const unsigned int apertureSobel)