@@ -7,6 +7,7 @@ @implementation OpenCVTestViewController
77
88- (void )dealloc {
99 AudioServicesDisposeSystemSoundID (alertSoundID);
10+ [timeRecorder release ];
1011 [imageView dealloc ];
1112 [super dealloc ];
1213}
@@ -36,7 +37,7 @@ - (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
3637
3738// NOTE You should convert color mode as RGB before passing to this function
3839- (UIImage *)UIImageFromIplImage : (IplImage *)image {
39- NSLog (@" IplImage (%d , %d ) %d bits by %d channels, %d bytes/row %s " , image->width , image->height , image->depth , image->nChannels , image->widthStep , image->channelSeq );
40+ // NSLog(@"IplImage (%d, %d) %d bits by %d channels, %d bytes/row %s", image->width, image->height, image->depth, image->nChannels, image->widthStep, image->channelSeq);
4041
4142 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
4243 NSData *data = [NSData dataWithBytes: image->imageData length: image->imageSize];
@@ -168,6 +169,70 @@ - (void) opencvFaceDetect:(UIImage *)overlayImage {
168169 }
169170}
170171
172+ - (void )opencvMosaic : (NSNumber *)mosaicSize {
173+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc ] init ];
174+ if (imageView.image ) {
175+ [timeRecorder start ];
176+ int mSize = [mosaicSize intValue ];
177+ IplImage *img = [self CreateIplImageFromUIImage: imageView.image];
178+ IplImage *dst = cvCreateImage (cvGetSize (img), img->depth , img->nChannels );
179+ unsigned int r, g, b;
180+ int p, pp, c;
181+
182+ for (int i = 0 ; i < img->height ; i += mSize) {
183+ for (int j = 0 ; j < img->width ; j += mSize) {
184+ r = b = g = 0 ;
185+ p = (i * img->width + j) * 3 ;
186+ c = 0 ;
187+ for (int k = 0 ; k < mSize; k++) {
188+ if (i + k < img->height ) {
189+ for (int l = 0 ; l < mSize; l++) {
190+ if (j + l < img->width ) {
191+ pp = p + (k * img->width + l) * 3 ;
192+ b += (unsigned char )img->imageData [pp];
193+ g += (unsigned char )img->imageData [pp + 1 ];
194+ r += (unsigned char )img->imageData [pp + 2 ];
195+ c++;
196+ }
197+ }
198+ }
199+ }
200+ r /= c;
201+ g /= c;
202+ b /= c;
203+
204+ for (int k = 0 ; k < mSize; k++) {
205+ if (i + k < img->height ) {
206+ for (int l = 0 ; l < mSize; l++) {
207+ if (j + l < img->width ) {
208+ pp = p + (k * img->width + l) * 3 ;
209+ dst->imageData [pp] = (char )r;
210+ dst->imageData [pp + 1 ] = (char )g;
211+ dst->imageData [pp + 2 ] = (char )b;
212+ }
213+ }
214+ }
215+ }
216+ }
217+ }
218+ cvReleaseImage (&img);
219+ imageView.image = [self UIImageFromIplImage: dst];
220+ cvReleaseImage (&dst);
221+ double time = [timeRecorder end ];
222+ NSLog (@" mosaic: %lf msec" , time);
223+ if (mSize < 128 ) {
224+ NSNumber *doubleSize = [[NSNumber alloc ] initWithInt: mSize * 2 ];
225+ [self performSelectorInBackground: @selector (opencvMosaic: ) withObject: doubleSize];
226+ [doubleSize release ];
227+ }
228+ else {
229+ NSLog (@" average: %lf msec" , [timeRecorder average ]);
230+ }
231+
232+ }
233+ [self hideProgressIndicator ];
234+ [pool release ];
235+ }
171236
172237#pragma mark -
173238#pragma mark IBAction
@@ -213,6 +278,15 @@ - (IBAction)faceDetect:(id)sender {
213278 }
214279}
215280
281+ - (IBAction )mosaic : (id )sender {
282+ cvSetErrMode (CV_ErrModeParent);
283+ [self showProgressIndicator: @" Mosaic" ];
284+ NSNumber *mosaicSize = [[NSNumber alloc ] initWithInt: 2 ];
285+ [timeRecorder reset ];
286+ [self performSelectorInBackground: @selector (opencvMosaic: ) withObject: mosaicSize];
287+ [mosaicSize release ];
288+ }
289+
216290#pragma mark -
217291#pragma mark UIViewControllerDelegate
218292
@@ -223,6 +297,7 @@ - (void)viewDidLoad {
223297
224298 NSURL *url = [NSURL fileURLWithPath: [[NSBundle mainBundle ] pathForResource: @" Tink" ofType: @" aiff" ] isDirectory: NO ];
225299 AudioServicesCreateSystemSoundID ((CFURLRef)url, &alertSoundID);
300+ timeRecorder = [[TimeRecorder alloc ] init ];
226301}
227302
228303- (BOOL )shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation)interfaceOrientation {
0 commit comments