Skip to content

Commit d138222

Browse files
committed
Add mosaic process & time recorder
1 parent 5809b40 commit d138222

File tree

6 files changed

+456
-33
lines changed

6 files changed

+456
-33
lines changed

Classes/OpenCVTestViewController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import <UIKit/UIKit.h>
22
#import <AudioToolbox/AudioToolbox.h>
3+
#import "TimeRecorder.h"
34

45
@interface UIProgressIndicator : UIActivityIndicatorView {
56
}
@@ -49,12 +50,14 @@ typedef enum {
4950
OpenCVTestViewControllerActionSheetAction actionSheetAction;
5051
UIProgressHUD *progressHUD;
5152
SystemSoundID alertSoundID;
53+
TimeRecorder *timeRecorder;
5254
}
5355

5456
- (IBAction)loadImage:(id)sender;
5557
- (IBAction)saveImage:(id)sender;
5658
- (IBAction)edgeDetect:(id)sender;
5759
- (IBAction)faceDetect:(id)sender;
60+
- (IBAction)mosaic:(id)sender;
5861

5962
@property (nonatomic, retain) UIImageView *imageView;
6063
@end

Classes/OpenCVTestViewController.m

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: %lfmsec", 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: %lfmsec", [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 {

Classes/TimeRecorder.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// TimeRecorder.h
3+
// OpenCVTest
4+
//
5+
// Created by mito on 10/01/24.
6+
// Copyright 2010 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
12+
@interface TimeRecorder : NSObject {
13+
NSDate *mStartTime;
14+
NSMutableArray *mTimeArray;
15+
}
16+
-(void) start;
17+
-(double) end;
18+
-(void) reset;
19+
-(double) average;
20+
@end

Classes/TimeRecorder.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// TimeRecorder.m
3+
// OpenCVTest
4+
//
5+
// Created by mito on 10/01/24.
6+
// Copyright 2010 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "TimeRecorder.h"
10+
11+
12+
@implementation TimeRecorder
13+
- (void) dealloc {
14+
[mTimeArray release];
15+
[super dealloc];
16+
}
17+
18+
- (id)init {
19+
if ([super init]) {
20+
mTimeArray = [[NSMutableArray alloc] init];
21+
}
22+
return self;
23+
}
24+
25+
-(void) start {
26+
mStartTime = [NSDate date];
27+
}
28+
29+
-(double) end {
30+
double time = [[NSDate date] timeIntervalSinceDate:mStartTime];
31+
NSNumber *interval = [[NSNumber alloc] initWithDouble:time];
32+
[mTimeArray addObject:interval];
33+
[interval release];
34+
return time;
35+
}
36+
37+
-(void) reset {
38+
[mTimeArray removeAllObjects];
39+
}
40+
41+
-(double) average {
42+
double total = 0;
43+
for (int i = 0; i < [mTimeArray count]; i++) {
44+
NSNumber *time = [mTimeArray objectAtIndex:i];
45+
total += [time doubleValue];
46+
}
47+
return total / [mTimeArray count];
48+
}
49+
@end

0 commit comments

Comments
 (0)