Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ViewController.mm
1 #import "ViewController.h"
2 #ifdef __cplusplus
3 #import <visp3/visp.h>
4 #endif
5 
6 #ifndef DOXYGEN_SHOULD_SKIP_THIS
7 @interface ViewController ()
8 @end
9 
10 @implementation ViewController
11 
12 // Define the different process we want to apply to the input image
13 NSArray *process = [[NSArray alloc]initWithObjects:@"load image", @"convert to gray", @"compute gradient",
14 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
15  @"canny detector",
16 #endif
17  nil];
18 
19 @synthesize myImageView;
20 #endif
21 
22 - (void)viewDidLoad {
23 
24  [super viewDidLoad];
25 
26  // create an image
27  UIImage *myScreenShot = [UIImage imageNamed:@"monkey.png"];
28 
29  // image view instance to display the image
30  self.myImageView = [[UIImageView alloc] initWithImage:myScreenShot];
31 
32  // set the frame for the image view
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];
35 
36  // add the image view to the current view
37  [self.view addSubview:self.myImageView];
38 
39  // create buttons
40  CGFloat posx=140, posy=350;
41  CGFloat padding = 50;
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];
47 
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];
53  }
54 }
55 
56 - (void) checkButtonClick:(UIButton *)paramSender{
57 
58  UIButton *myButton = paramSender;
59 
60  //check which button was tapped
61  if([myButton.currentTitle isEqualToString:[process objectAtIndex: 0]]){
62  // load image
63  NSLog(@"Clicked on \"%@\" button ", [process objectAtIndex: 0]);
64 
65  [myImageView setImage:[UIImage imageNamed:@"monkey.png"]];
66  }
67  else if([myButton.currentTitle isEqualToString:[process objectAtIndex: 1]]){
68  // convert to gray
69  NSLog(@"Clicked on \"%@\" button ", [process objectAtIndex: 1]);
70 
71  UIImage *img = [UIImage imageNamed:@"monkey.png"];
72  vpImage<unsigned char> gray = [self vpImageGrayFromUIImage:img];
73  [myImageView setImage:[self UIImageFromVpImageGray:gray]];
74  }
75  else if([myButton.currentTitle isEqualToString:[process objectAtIndex: 2]]){
76  // compute gradient
77  NSLog(@"Clicked on \"%@\" button ", [process objectAtIndex: 2]);
78 
79  UIImage *img = [UIImage imageNamed:@"monkey.png"];
80  vpImage<unsigned char> gray = [self vpImageGrayFromUIImage:img];
81  vpImage<double> dIx;
82  vpImageFilter::getGradX(gray, dIx);
83  vpImageConvert::convert(dIx, gray);
84 
85  [myImageView setImage:[self UIImageFromVpImageGray:gray]];
86  }
87 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
88  else if([myButton.currentTitle isEqualToString:[process objectAtIndex: 3]]){
89  // canny detector
90  NSLog(@"Clicked on \"%@\" button ", [process objectAtIndex: 3]);
91 
92  UIImage *img = [UIImage imageNamed:@"monkey.png"];
93  vpImage<unsigned char> gray = [self vpImageGrayFromUIImage:img];
95  vpImageFilter::canny(gray, canny, 5, 15, 3);
96  [myImageView setImage:[self UIImageFromVpImageGray:canny]];
97  }
98 #endif
99 }
100 
102 // Converts an UIImage that could be in gray or color into a ViSP color image
103 - (vpImage<vpRGBa>)vpImageColorFromUIImage:(UIImage *)image
104 {
105  CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
106 
107  if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
108  NSLog(@"Input UIImage is grayscale");
109  vpImage<unsigned char> gray(image.size.height, image.size.width); // 8 bits per component, 1 channel
110 
111  CGContextRef contextRef = CGBitmapContextCreate(gray.bitmap, // pointer to data
112  image.size.width, // width of bitmap
113  image.size.height, // height of bitmap
114  8, // bits per component
115  image.size.width, // bytes per row
116  colorSpace, // colorspace
117  kCGImageAlphaNone |
118  kCGBitmapByteOrderDefault); // bitmap info flags
119 
120  CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
121  CGContextRelease(contextRef);
122 
123  vpImage<vpRGBa> color;
124  vpImageConvert::convert(gray, color);
125 
126  return color;
127  }
128  else {
129  NSLog(@"Input UIImage is color");
130  vpImage<vpRGBa> color(image.size.height, image.size.width); // 8 bits per component, 4 channels
131 
132  colorSpace = CGColorSpaceCreateDeviceRGB();
133 
134  CGContextRef contextRef = CGBitmapContextCreate(color.bitmap, // pointer to data
135  image.size.width, // width of bitmap
136  image.size.height, // height of bitmap
137  8, // bits per component
138  4 * image.size.width, // bytes per row
139  colorSpace, // colorspace
140  kCGImageAlphaNoneSkipLast |
141  kCGBitmapByteOrderDefault); // bitmap info flags
142 
143  CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
144  CGContextRelease(contextRef);
145 
146  return color;
147  }
148 }
150 
152 // Converts an UIImage that could be in gray or color into a ViSP gray image
153 - (vpImage<unsigned char>)vpImageGrayFromUIImage:(UIImage *)image
154 {
155  CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
156 
157  if (CGColorSpaceGetModel(colorSpace) == kCGColorSpaceModelMonochrome) {
158  NSLog(@"Input UIImage is grayscale");
159  vpImage<unsigned char> gray(image.size.height, image.size.width); // 8 bits per component, 1 channel
160 
161  CGContextRef contextRef = CGBitmapContextCreate(gray.bitmap, // pointer to data
162  image.size.width, // width of bitmap
163  image.size.height, // height of bitmap
164  8, // bits per component
165  image.size.width, // bytes per row
166  colorSpace, // colorspace
167  kCGImageAlphaNone |
168  kCGBitmapByteOrderDefault); // bitmap info flags
169 
170  CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
171  CGContextRelease(contextRef);
172 
173  return gray;
174  } else {
175  NSLog(@"Input UIImage is color");
176  vpImage<vpRGBa> color(image.size.height, image.size.width); // 8 bits per component, 4 channels (color channels + alpha)
177 
178  colorSpace = CGColorSpaceCreateDeviceRGB();
179 
180  CGContextRef contextRef = CGBitmapContextCreate(color.bitmap, // pointer to data
181  image.size.width, // width of bitmap
182  image.size.height, // height of bitmap
183  8, // bits per component
184  4 * image.size.width, // bytes per row
185  colorSpace, // colorspace
186  kCGImageAlphaNoneSkipLast |
187  kCGBitmapByteOrderDefault); // bitmap info flags
188 
189  CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
190  CGContextRelease(contextRef);
191 
193  vpImageConvert::convert(color, gray);
194 
195  return gray;
196  }
197 }
199 
201 // Converts a color ViSP image into a color UIImage
202 -(UIImage *)UIImageFromVpImageColor:(vpImage<vpRGBa>)I
203 {
204  NSData *data = [NSData dataWithBytes:I.bitmap length:I.getSize()*4];
205  CGColorSpaceRef colorSpace;
206 
207  colorSpace = CGColorSpaceCreateDeviceRGB();
208 
209  CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
210 
211  // Creating CGImage from vpImage
212  CGImageRef imageRef = CGImageCreate(I.getWidth(), // width
213  I.getHeight(), // height
214  8, // bits per component
215  8 * 4, // bits per pixel
216  4 * I.getWidth(), // bytesPerRow
217  colorSpace, // colorspace
218  kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
219  provider, // CGDataProviderRef
220  NULL, // decode
221  false, // should interpolate
222  kCGRenderingIntentDefault // intent
223  );
224 
225 
226  // Getting UIImage from CGImage
227  UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
228  CGImageRelease(imageRef);
229  CGDataProviderRelease(provider);
230  CGColorSpaceRelease(colorSpace);
231 
232  return finalImage;
233 }
235 
237 // Converts a gray level ViSP image into a gray level UIImage
238 -(UIImage *)UIImageFromVpImageGray:(vpImage<unsigned char>)I
239 {
240  NSData *data = [NSData dataWithBytes:I.bitmap length:I.getSize()];
241  CGColorSpaceRef colorSpace;
242 
243  colorSpace = CGColorSpaceCreateDeviceGray();
244 
245  CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
246 
247  // Creating CGImage from vpImage
248  CGImageRef imageRef = CGImageCreate(I.getWidth(), // width
249  I.getHeight(), // height
250  8, // bits per component
251  8, // bits per pixel
252  I.getWidth(), // bytesPerRow
253  colorSpace, // colorspace
254  kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
255  provider, // CGDataProviderRef
256  NULL, // decode
257  false, // should interpolate
258  kCGRenderingIntentDefault // intent
259  );
260 
261 
262  // Getting UIImage from CGImage
263  UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
264  CGImageRelease(imageRef);
265  CGDataProviderRelease(provider);
266  CGColorSpaceRelease(colorSpace);
267 
268  return finalImage;
269 }
271 
272 - (void)didReceiveMemoryWarning {
273  [super didReceiveMemoryWarning];
274  // Dispose of any resources that can be recreated.
275 }
276 
277 @end
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void getGradX(const vpImage< unsigned char > &I, vpImage< double > &dIx)
Definition: vpRGBa.h:66
static void canny(const vpImage< unsigned char > &I, vpImage< unsigned char > &Ic, const unsigned int gaussianFilterSize, const double thresholdCanny, const unsigned int apertureSobel)