-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenImageHandler.mm
executable file
·1287 lines (1134 loc) · 37.4 KB
/
OpenImageHandler.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SimpleAnnotate
//
// Created by Joel Brogan on 22/8/15.
// Copyright (c) 2015 Joel Brogan. All rights reserved.
//
#import "OpenImageHandler.h"
#import "NSImage+OpenCV.h"
std::pair<int, GLenum> CVtoGLtypes[] =
{
std::make_pair(CV_8U, GL_UNSIGNED_BYTE),
std::make_pair(CV_8S, GL_BYTE),
std::make_pair(CV_16U, GL_UNSIGNED_SHORT),
std::make_pair(CV_16S, GL_SHORT),
std::make_pair(CV_32S, GL_INT),
std::make_pair(CV_32F, GL_FLOAT),
std::make_pair(CV_64F, GL_DOUBLE)
};
std::map<int, GLenum> CVtoGLtype(CVtoGLtypes,
CVtoGLtypes + sizeof CVtoGLtypes / sizeof CVtoGLtypes[0]);
@implementation OpenImageHandler
@synthesize size;
@synthesize imageRect;
@synthesize aspectRatio;
@synthesize format;
@synthesize null;
@synthesize binary;
@synthesize namePrefix,currentPath,extenshion;
- (void)setStartCenter:(Vector2)sc
{
[lock lockForWriting];
startCenter = sc;
[self CalculateCorners];
[lock unlock];
}
- (void)setEndCenter:(Vector2)ec
{
[lock lockForWriting];
endCenter = ec;
[self CalculateCorners];
[lock unlock];
}
- (void)setRotation:(GLKQuaternion)rot
{
[lock lockForWriting];
rotation = rot;
[self CalculateCorners];
[lock unlock];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:[NSNumber numberWithBool:null] forKey:@"null"];
if(!null)
{
std::vector<uchar> encodedMat;
std::vector<int> encodedMatParams(2);
encodedMatParams[0] = CV_IMWRITE_JPEG_QUALITY;
encodedMatParams[1] = 100; //jpeg compression quality
cv::imencode(".jpg", Cv, encodedMat);
[aCoder encodeObject:[NSData dataWithBytes:encodedMat.data() length:encodedMat.size()*sizeof(uchar)] forKey:@"Image"];
[aCoder encodeObject:[NSValue valueWithPoint:NSMakePoint(Cv.cols, Cv.rows)] forKey:@"CvSize"];
[aCoder encodeObject:[NSNumber numberWithInt:Cv.elemSize()] forKey:@"MatElemSize"];
[aCoder encodeObject:[NSData dataWithBytes:Cv.data length:Cv.cols*Cv.rows*Cv.elemSize()] forKey:@"CvData"];
[aCoder encodeObject:[NSValue valueWithSize:NSMakeSize(size.width, size.height)] forKey:@"size"];
[aCoder encodeObject:[NSValue valueWithRect:NSMakeRect(imageRect.origin.x,imageRect.origin.y, imageRect.size.width, imageRect.size.height)] forKey:@"imageRect"];
[aCoder encodeObject:[NSNumber numberWithFloat:aspectRatio] forKey:@"aspectRatio"];
[aCoder encodeObject:[NSNumber numberWithInt:format] forKey:@"format"];
[aCoder encodeObject:[NSNumber numberWithBool:binary] forKey:@"binary"];
}
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
null =[(NSNumber *)[aDecoder decodeObjectForKey:@"null"] boolValue];
if(!null)
{
NSData *CvData = (NSData *)[aDecoder decodeObjectForKey:@"Image"];
uchar * imageBytes = (uchar *)[CvData bytes];
std::vector<uchar> imageVector(imageBytes,imageBytes+CvData.length/sizeof(uchar));
Cv = cv::imdecode(imageVector, CV_LOAD_IMAGE_ANYDEPTH);
cv = new IplImage(Cv);
NSSize s= [(NSValue *)[aDecoder decodeObjectForKey:@"size"] sizeValue];
size = CGSizeMake(s.width, s.height);
NSRect r = [[aDecoder decodeObjectForKey:@"imageRect"] rectValue];
imageRect = CGRectMake(r.origin.x, r.origin.y, r.size.width, r.size.height);
aspectRatio = [[aDecoder decodeObjectForKey:@"aspectRatio"] floatValue];
format = [[aDecoder decodeObjectForKey:@"format"] intValue];
binary =[[aDecoder decodeObjectForKey:@"binary"] boolValue];
}
}
return self;
}
- (id)init
{
self = [super init];
if(self)
{
namePrefix = nil;
currentPath = nil;
extenshion = [@".JPG" retain];
color = Color();
cv = NULL;
data.gl = NULL;
null = true;
data.glSet = false;
binary = false;
size.width = 0;
size.height = 0;
aspectRatio = 1;
imageRect.size = size;
imageRect.origin.x = 0;
imageRect.origin.y = 0;
startCenter = Vector2(0,0);
endCenter = Vector2(0,0);
rotation = GLKQuaternionIdentity;
[self CalculateCorners];
}
return self;
}
- (id)initWithIplImage:(IplImage*)image Color:(Color)c BinaryImage:(bool)b
{
self = [super init];
if(self)
{
namePrefix = nil;
currentPath = nil;
extenshion = [@".JPG" retain];
color = c;
cv = image;
Cv = cv::Mat(image);
ownsAnIPL = true;
binary = b;
data.glSet = false;
if(cv!=NULL)
{
null = false;
size.width = cv->width;
size.height = cv->height;
aspectRatio = size.width/size.height;
}
else
{
null = true;
size.width = 0;
size.height = 0;
aspectRatio = 1;
}
imageRect.size = size;
imageRect.origin.x = 0;
imageRect.origin.y = 0;
startCenter = Vector2(0,0);
endCenter = Vector2(0,0);
rotation = GLKQuaternionIdentity;
[self CalculateCorners];
}
return self;
}
- (id)initWithCVMat:(cv::Mat)image Color:(Color)c BinaryImage:(bool)b
{
self = [super init];
if(self)
{
namePrefix = nil;
currentPath = nil;
extenshion = [@".JPG" retain];
color = c;
Cv = image;
cv = new IplImage(Cv);
binary = b;
data.glSet = false;
if(cv!=NULL)
{
null = false;
size.width = cv->width;
size.height = cv->height;
aspectRatio = size.width/size.height;
}
else
{
null = true;
size.width = 0;
size.height = 0;
aspectRatio = 1;
}
imageRect.size = size;
imageRect.origin.x = 0;
imageRect.origin.y = 0;
startCenter = Vector2(0,0);
endCenter = Vector2(0,0);
rotation = GLKQuaternionIdentity;
[self CalculateCorners];
}
return self;
}
- (id)initWithChannels:(NSArray*)channels Color:(Color)c BinaryImage:(bool)b
{
self = [super init];
if(self)
{
namePrefix = nil;
currentPath = nil;
extenshion = [@".JPG" retain];
color = c;
std::vector<cv::Mat> chans;
for(int i=0; i<channels.count; i++)
{
id obj = [channels objectAtIndex:i];
NSAssert([obj isKindOfClass:OpenImageHandler.class], @"You can not merge any thing but Open Image Handlers into an OpenImageHandler");
chans.push_back(((OpenImageHandler*)obj).Cv);
}
cv::Mat image;
cv::merge(chans, image);
Cv = image;
cv = new IplImage(Cv);
binary = b;
data.glSet = false;
if(cv!=NULL)
{
null = false;
size.width = cv->width;
size.height = cv->height;
aspectRatio = size.width/size.height;
}
else
{
null = true;
size.width = 0;
size.height = 0;
aspectRatio = 1;
}
imageRect.size = size;
imageRect.origin.x = 0;
imageRect.origin.y = 0;
startCenter = Vector2(0,0);
endCenter = Vector2(0,0);
rotation = GLKQuaternionIdentity;
[self CalculateCorners];
}
return self;
}
- (id)initWithFilePath:(const char*)path Color:(Color)c BinaryImage:(bool)b
{
self = [super init];
if(self)
{
currentPath = [[NSString stringWithUTF8String:path] retain];
namePrefix = [[[currentPath lastPathComponent] stringByDeletingPathExtension] retain];
extenshion = [[currentPath pathExtension] retain];
if([[extenshion uppercaseString] isEqualToString:@"CR2"])
{
NSImage*image = [[NSImage alloc] initWithContentsOfFile:currentPath];
Cv = image.CVMat;
[image release];
}
else
{
if(b)
{
Cv = cv::imread(path,0);
}
else
{
Cv = cv::imread(path);
}
}
color = c;
cv = new IplImage(Cv);
data.glSet = false;
binary = b;
if(cv!=NULL)
{
null = false;
// size.width = cV.cols;
// size.height = cV.rows;
size.width = cv->width;
size.height = cv->height;
aspectRatio = size.width/size.height;
}
else
{
null = true;
size.width = 0;
size.height = 0;
aspectRatio = 1;
}
imageRect.size = size;
imageRect.origin.x = 0;
imageRect.origin.y = 0;
startCenter = Vector2(0,0);
endCenter = Vector2(0,0);
rotation = GLKQuaternionIdentity;
[self CalculateCorners];
}
return self;
}
- (id)initWithFilePath:(const char*)path Color:(Color)c BinaryImage:(bool)b NamePrefix:(NSString*)nP
{
self = [super init];
if(self)
{
currentPath = [[NSString stringWithUTF8String:path] retain];
namePrefix = [nP retain];
extenshion = [[currentPath pathExtension] retain];
color = c;
Cv = cv::imread(path);
cv = new IplImage(Cv);
data.glSet = false;
binary = b;
if(cv!=NULL)
{
null = false;
// size.width = cV.cols;
// size.height = cV.rows;
size.width = cv->width;
size.height = cv->height;
aspectRatio = size.width/size.height;
}
else
{
null = true;
size.width = 0;
size.height = 0;
aspectRatio = 1;
}
imageRect.size = size;
imageRect.origin.x = 0;
imageRect.origin.y = 0;
startCenter = Vector2(0,0);
endCenter = Vector2(0,0);
rotation = GLKQuaternionIdentity;
[self CalculateCorners];
}
return self;
}
- (id)initWithNamePrefix:(NSString*)nP
{
self = [self init];
if(self)
{
namePrefix = [nP retain];
currentPath = nil;
extenshion = [@".JPG" retain];
}
return self;
}
- (id)initWithIplImage:(IplImage*)image Color:(Color)c BinaryImage:(bool)b NamePrefix:(NSString*)nP
{
self = [self initWithIplImage:image Color:c BinaryImage:b];
if(self)
{
namePrefix = [nP retain];
currentPath = nil;
extenshion = [@".JPG" retain];
}
return self;
}
- (id)initWithCVMat:(cv::Mat)image Color:(Color)c BinaryImage:(bool)b NamePrefix:(NSString*)nP
{
self = [self initWithCVMat:image Color:c BinaryImage:b];
if(self)
{
namePrefix = [nP retain];
currentPath = nil;
extenshion = [@".JPG" retain];
}
return self;
}
- (id)copy
{
cv::Mat copyedMat;
Cv.copyTo(copyedMat);
OpenImageHandler*copiedImage = [[OpenImageHandler alloc] initWithCVMat:copyedMat Color:color BinaryImage:binary];
copiedImage.namePrefix = namePrefix;
copiedImage.currentPath = nil;
copiedImage.extenshion = extenshion;
return copiedImage;
}
- (void)SetRenderOffset:(Vector2)renderOffset
{
[lock lockForWriting];
imageRect.origin = CGPointMake(renderOffset.x, renderOffset.y);
[lock unlock];
}
- (GLuint)gl
{
if(!data.glSet)
{
if([self LoadGLTexture])
{
data.glSet = true;
}
else
{
NSString *error = [NSString stringWithFormat:@"The IplImage in OpenImageHanlder:%@ is NULL, cannot get gl version.", self];
NSAssert(false,error);
[lock unlock];
return nil;
}
}
GLenum gl = data.gl;
return gl;
}
- (bool)LoadGLTexture
{
//Ref: http://www.gamedev.net/page/resources/_/technical/opengl/opengl-texture-mapping-an-introduction-r947
if (null) return false;
glGenTextures(1, data.glAddress);
glBindTexture( GL_TEXTURE_2D, data.gl );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// if(format==GL_LUMINANCE)
// {
// glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size.width, size.height,0, GL_ALPHA, GL_UNSIGNED_BYTE, cv->imageData);
// }
// else
// {
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.width, size.height,0, format, GL_UNSIGNED_BYTE, cv->imageData);
// }
GLenum type = CVtoGLtype[Cv.depth()];
if(binary)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, size.width, size.height,0, GL_ALPHA, type, cv->imageData);
}
else
{
switch(Cv.channels())
{
case 1:
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, size.width, size.height,0, GL_LUMINANCE, type, cv->imageData);
break;
case 2:
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, size.width, size.height,0, GL_LUMINANCE_ALPHA, type, cv->imageData);
break;
case 3:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size.width, size.height,0, GL_BGR, type, cv->imageData);
break;
case 4:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size.width, size.height,0, GL_BGRA, type, cv->imageData);
break;
default:
NSAssert(false, @"You must have 1 to 4 channels in the CV::Mat to glTexture convershion function!");
break;
}
}
return true;
}
- (Vector3)MouseOverPointForScreenPoint:(Vector2)screenPoint UsingSpaceConverter:(SpaceConverter)spaceConverter
{
Vector2 imagePoint = spaceConverter.ScreenToImageVector(screenPoint);
return Vector3(floor(imagePoint.x)+0.5,floor(imagePoint.y)+0.5,0);
}
- (void)GraphUsingSpaceConverter:(SpaceConverter)spaceConverter
{
// if([self BeginGraphingUsingSpaceConverter:spaceConverter])
{
glColor4f(color.r/255.0, color.g/255.0, color.b/255.0, 1);
[lock lock];
if(data.ObjectChanged)
{
[data deallocImage];
data.ObjectChanged = false;
}
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, self.gl);
glBegin(GL_TRIANGLE_STRIP);
{
Vector2 LL_temp = spaceConverter.ImageToCameraVector(LL);
Vector2 UL_temp = spaceConverter.ImageToCameraVector(UL);
Vector2 UR_temp = spaceConverter.ImageToCameraVector(UR);
Vector2 LR_temp = spaceConverter.ImageToCameraVector(LR);
glTexCoord2f(0.0, 0.0);
glVertex3f(LL_temp.x, LL_temp.y, minZ);
glTexCoord2f(0.0, -1.0);
glVertex3f(UL_temp.x, UL_temp.y, minZ);
glTexCoord2f(1.0, 0.0);
glVertex3f(LR_temp.x, LR_temp.y, minZ);
glTexCoord2f(1.0, -1.0);
glVertex3f(UR_temp.x, UR_temp.y, minZ);
}
glEnd();
glDisable(GL_TEXTURE_2D);
[lock unlock];
}
// [self EndGraphing];
}
- (void)CalculateCorners
{
LL = Vector3(Vector2(0,imageRect.size.height) +imageRect.origin-startCenter).RotatedBy(rotation).AsVector2() + endCenter;
UL = Vector3(Vector2(0, 0) +imageRect.origin-startCenter).RotatedBy(rotation).AsVector2() + endCenter;
UR = Vector3(Vector2(imageRect.size.width, 0) +imageRect.origin-startCenter).RotatedBy(rotation).AsVector2() + endCenter;
LR = Vector3(Vector2(imageRect.size.width,imageRect.size.height) +imageRect.origin-startCenter).RotatedBy(rotation).AsVector2() + endCenter;
}
- (void)SaveAtPath:(NSString*)path
{
[lock lock];
cvSaveImage(path.UTF8String, cv);
[lock unlock];
}
- (NSDictionary*)MetaData
{
return nil;
[lock lock];
if(currentPath)
{
CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef)[NSURL fileURLWithPath:currentPath], NULL);
NSDictionary* metadata = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source,0,NULL);
//Upload Part Meta Data
NSDictionary*ExpandedImageMetaData = [self ExpandedMetaDateDictionaryForMetaData:metadata];
CFRelease(source);
[metadata release];
[lock unlock];
return ExpandedImageMetaData;
}
[lock unlock];
return nil;
}
- (NSDictionary*)ExpandedMetaDateDictionaryForMetaData:(NSDictionary*)metaData
{
[lock lock];
NSMutableArray*Keys = [[NSMutableArray alloc] initWithCapacity:50];
NSMutableArray*Values = [[NSMutableArray alloc] initWithCapacity:50];
for(NSString*Key in metaData.allKeys)
{
if([Key hasPrefix:@"{"])
{
NSString*rootKey = [[Key stringByReplacingOccurrencesOfString:@"{" withString:@""] stringByReplacingOccurrencesOfString:@"}" withString:@""];
NSDictionary*ValuesOfKey = [metaData objectForKey:Key];
for(int i=0; i<ValuesOfKey.count; i++)
{
id value = [[ValuesOfKey allValues] objectAtIndex:i];
[Keys addObject:[NSString stringWithFormat:@"%@.%@",rootKey,[[ValuesOfKey allKeys] objectAtIndex:i]]];
[Values addObject:[self stringForPartMetaDateQueryByAppendingValue:value]];
}
}
else
{
[Keys addObject:Key];
[Values addObject:[self stringForPartMetaDateQueryByAppendingValue:[metaData objectForKey:Key]]];
}
}
NSDictionary*dict = [NSDictionary dictionaryWithObjects:Values forKeys:Keys];
[Values release];
[Keys release];
[lock unlock];
return dict;
}
- (NSString*)stringForPartMetaDateQueryByAppendingValue:(id)value
{
[lock lock];
NSString*OUT = @"";
NSString*className = [[value class] description];
if([className isEqualToString:@"__NSCFNumber"] || [className isEqualToString:@"__NSCFConstantString"] || [className isEqualToString:@"__NSCFString"])
{
OUT = [value description];
}
else if([className isEqualToString:@"__NSArrayM"])
{
OUT = [[((NSArray*)value) valueForKey:@"description"] componentsJoinedByString:@"."];
}
else
{
NSLog(@"Class %@ Not supported!",className);
}
[lock unlock];
return OUT;
}
+ (OpenImageHandler*)ZerosWithSize:(CGSize)s
{
return [[[OpenImageHandler alloc] initWithCVMat:cv::Mat::zeros(s.height, s.width, CV_8UC1) Color:White BinaryImage:YES] autorelease];
}
+ (OpenImageHandler*)WhiteWithSize:(CGSize)s
{
return [[[OpenImageHandler alloc] initWithCVMat:cv::Mat(s.height, s.width, CV_8UC1, 255) Color:White BinaryImage:YES] autorelease];
}
- (OpenImageHandler*)GreyScaled
{
[lock lock];
cv::Mat grey;
if(Cv.channels()>1)
{
cv::cvtColor(Cv, grey, CV_BGR2GRAY);
}
else
{
grey = Cv.clone();
}
[lock unlock];
return [[[OpenImageHandler alloc] initWithCVMat:grey Color:color BinaryImage:binary NamePrefix:namePrefix] autorelease];
}
- (OpenImageHandler*)BGR_2_HSV
{
[lock lock];
cv::Mat thisImage = self.Cv;
// NSAssert(thisImage.channels()==3, @"BGR_2_HSV only supports three channel images!");
cv::Mat hsv;
cv::cvtColor(thisImage, hsv, cv::COLOR_BGR2HSV);
[lock unlock];
OpenImageHandler*HSV = [[OpenImageHandler alloc] initWithCVMat:hsv Color:Color(255,255,255) BinaryImage:NO];
return [HSV autorelease];
}
- (OpenImageHandler*)BGR_2_YCrCb
{
cv::Mat thisImage = self.Cv;
// NSAssert(thisImage.channels()==3, @"BGR_2_YCbCr only supports three channel images!");
cv::Mat hsv;
cv::cvtColor(thisImage, hsv, cv::COLOR_BGR2YCrCb);
[lock unlock];
OpenImageHandler*HSV = [[OpenImageHandler alloc] initWithCVMat:hsv Color:Color(255,255,255) BinaryImage:NO];
return [HSV autorelease];
}
- (OpenImageHandler*)HSV_2_BGR
{
[lock lock];
cv::Mat thisImage = self.Cv;
// NSAssert(thisImage.channels()==3, @"HSV_2_BGR only supports three channel images!");
cv::Mat bgr;
cv::cvtColor(thisImage, bgr, cv::COLOR_HSV2BGR);
[lock unlock];
OpenImageHandler*HSV = [[OpenImageHandler alloc] initWithCVMat:bgr Color:Color(255,255,255) BinaryImage:NO];
return [HSV autorelease];
}
- (OpenImageHandler*)GREY_2_FalseColor
{
[lock lock];
cv::Mat thisImage = self.Cv;
// NSAssert(thisImage.channels()==1, @"GREY_2_FalseColor only supports single channel images!");
cv::Mat ones = cv::Mat(thisImage.rows, thisImage.cols, thisImage.type(), 255);
std::vector<cv::Mat> channels;
channels.push_back(thisImage);
channels.push_back(ones);
channels.push_back(ones);
cv::Mat H11;
cv::merge(channels, H11);
[lock unlock];
cv::Mat falseColor;
cv::cvtColor(H11, falseColor, cv::COLOR_HSV2BGR);
OpenImageHandler*FALSE_COLOR = [[OpenImageHandler alloc] initWithCVMat:falseColor Color:Color(255,255,255) BinaryImage:NO];
return [FALSE_COLOR autorelease];
}
- (OpenImageHandler*)BGR_2_H_FalseColor
{
// NSAssert(self.Cv.channels()==3, @"BGR_2_H_FalseColor only supports three channel images!");
return [[[[self BGR_2_HSV] Channels] objectAtIndex:0] GREY_2_FalseColor];
}
- (OpenImageHandler*)HSV_2_H_FalseColor
{
// NSAssert(self.Cv.channels()==3, @"BGR_2_H_FalseColor only supports three channel images!");
return [[[self Channels] objectAtIndex:0] GREY_2_FalseColor];
}
- (OpenImageHandler*)SobelOfOrder:(Vector2)order
{
cv::Mat output;
cv::Sobel(Cv, output, Cv.depth(), order.x, order.y, 7);
OpenImageHandler*Sobel = [[OpenImageHandler alloc] initWithCVMat:output Color:Color(255,255,255) BinaryImage:NO];
return [Sobel autorelease];
}
- (NSArray*)Channels
{
[lock lock];
cv::Mat thisImage = self.Cv;
std::vector<cv::Mat> channels;
cv::split(thisImage, channels);
[lock lock];
NSMutableArray*Channels = [[NSMutableArray alloc] initWithCapacity:thisImage.channels()];
for(int i=0; i<thisImage.channels(); i++)
{
OpenImageHandler*channel = [[OpenImageHandler alloc] initWithCVMat:channels[i] Color:Color(255,255,255) BinaryImage:NO];
[Channels addObject:channel];
[channel release];
}
return [NSArray arrayWithArray:[Channels autorelease]];
}
- (OpenImageHandler*)ThreeChannel
{
NSArray*Chans = [self Channels];
std::vector<cv::Mat> channels;
if(Chans.count>=3)
{
channels.push_back(((OpenImageHandler*)[Chans objectAtIndex:0]).Cv);
channels.push_back(((OpenImageHandler*)[Chans objectAtIndex:1]).Cv);
channels.push_back(((OpenImageHandler*)[Chans objectAtIndex:2]).Cv);
}
else
{
channels.push_back(((OpenImageHandler*)[Chans objectAtIndex:0]).Cv);
channels.push_back(((OpenImageHandler*)[Chans objectAtIndex:0]).Cv);
channels.push_back(((OpenImageHandler*)[Chans objectAtIndex:0]).Cv);
}
cv::Mat threeChan;
cv::merge(channels, threeChan);
return [[[OpenImageHandler alloc] initWithCVMat:threeChan Color:color BinaryImage:binary] autorelease];
}
- (OpenImageHandler*)Normalized
{
NSArray*channels = [self Channels];
for(int i=0; i<channels.count; i++)
{
cv::Mat chan = ((OpenImageHandler*)[channels objectAtIndex:i]).Cv;
cv::normalize(chan, chan, 0, 255, cv::NORM_MINMAX, CV_8UC1);
}
return [[[OpenImageHandler alloc] initWithChannels:channels Color:Color(255,255,255) BinaryImage:NO] autorelease];
}
- (OpenImageHandler*)EqualizeHistograms
{
NSArray*channels = [self Channels];
for(int i=0; i<channels.count; i++)
{
cv::Mat chan = ((OpenImageHandler*)[channels objectAtIndex:i]).Cv;
cv::equalizeHist(chan, chan);
// cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE(2.0, cv::Size(8,8));
// clahe->apply(chan, chan);
}
return [[[OpenImageHandler alloc] initWithChannels:channels Color:Color(255,255,255) BinaryImage:NO] autorelease];
}
- (OpenImageHandler*)FloodFillResultFromPoint:(Vector2)seed WithChangeInColorFromSeedLower:(Color)lowerDif Upper:(Color)upperDif
{
uchar fillValue = 255;
cv::Mat floodFilled_Boardered = cv::Mat::zeros(imageRect.size.height+2, imageRect.size.width+2, CV_8UC1);
[lock lockForWriting];
int fillResult = cv::floodFill(Cv, floodFilled_Boardered, seed.AsCvPoint(), cv::Scalar(255,255,255), NULL, lowerDif.AsCVScaler(), upperDif.AsCVScaler(), cv::FLOODFILL_FIXED_RANGE | cv::FLOODFILL_MASK_ONLY | (fillValue << 8));
[lock unlock];
NSLog(@"Fill Result:%i",fillResult);
cv::Mat floodFilledMat = floodFilled_Boardered(cv::Rect(1,1,imageRect.size.width,imageRect.size.height));
OpenImageHandler*floodFilled = [[OpenImageHandler alloc] initWithCVMat:floodFilledMat Color:Color(255,255,255) BinaryImage:NO];
return floodFilled;
}
float absf(float val)
{
return val < 0 ? -val : val;
}
- (OpenImageHandler*)Demosiaced
{
NSLog(@"DEMO");
//See: http://tx.technion.ac.il/~rc/Demosaicing_algorithms.pdf Algorythm 8 "Directionally Weighted Gradient Based Interpolation" or http://www.arl.army.mil/arlreports/2010/ARL-TR-5061.pdf
cv::Mat image = Cv; if(Cv.channels()>1) image = [self GreyScaled].Cv;
cv::Mat demosiaced = cv::Mat::zeros(imageRect.size.height,imageRect.size.width,CV_8UC3);
int imageStep = image.step[1];
int demosiacedStep = demosiaced.step[1];
//Iterate Rs and Bs and Interpolate Gs while gathering knowns:
uchar*imageRowY_minus2;
uchar*imageRowY_minus1 = image.ptr(1-1);
uchar*imageRowY_center = image.ptr(1);
uchar*imageRowY_added1 = image.ptr(1+1);
uchar*imageRowY_added2 = image.ptr(1+2);
for(int y=2; y<image.rows-2; y++)
{
imageRowY_minus2 = imageRowY_minus1;
imageRowY_minus1 = imageRowY_center;
imageRowY_center = imageRowY_added1;
imageRowY_added1 = imageRowY_added2;
imageRowY_added2 = image.ptr(y+2);
uchar*demosiacedRowY = demosiaced.ptr(y);
for(int x=2 + y%2; x<image.cols-2; x+=2)
{
int xIndex = x*demosiacedStep;
int xIndex_m2 = (x-2)*imageStep;
int xIndex_m1 = xIndex_m2+imageStep;
int xIndex_c0 = xIndex_m1+imageStep;
int xIndex_p1 = xIndex_c0+imageStep;
int xIndex_p2 = xIndex_p1+imageStep;
//*******************************************************************************************************************************************************************************************************************************
float C1 = imageRowY_minus2[ xIndex_c0 ];
float G2 = imageRowY_minus1[ xIndex_c0 ];
float C3 = imageRowY_center[ xIndex_m2 ]; float G4 = imageRowY_center[ xIndex_m1 ]; float C5 = imageRowY_center[ xIndex_c0 ]; float G6 = imageRowY_center[ xIndex_p1 ]; float C7 = imageRowY_center[ xIndex_p2 ];
float G8 = imageRowY_added1[ xIndex_c0 ];
float C9 = imageRowY_added2[ xIndex_c0 ];
//*******************************************************************************************************************************************************************************************************************************
float Gn = absf( G8 - G2 ) + absf( C5 - C1 );
float Ge = absf( G4 - G6 ) + absf( C5 - C7 );
float Gs = absf( G2 - G8 ) + absf( C5 - C9 );
float Gw = absf( G6 - G4 ) + absf( C5 - C3 );
float Wn = 1.0/(1.0+Gn);
float We = 1.0/(1.0+Ge);
float Ws = 1.0/(1.0+Gs);
float Ww = 1.0/(1.0+Gw);
float Gn_norm = G2 + absf( C5 - C1 )/2.0;
float Ge_norm = G6 + absf( C5 - C7 )/2.0;
float Gs_norm = G8 + absf( C5 - C9 )/2.0;
float Gw_norm = G4 + absf( C5 - C3 )/2.0;
float green = (Wn * Gn_norm + We * Ge_norm + Ws * Gs_norm + Ww * Gw_norm) / (Wn + We + Ws + Ww);
demosiacedRowY[xIndex+1] = green;
if(x%2) //Curent x,y is a Bayer Red pixel
{
demosiacedRowY[xIndex ] = C5;
}
else//Curent x,y is a Bayer Blue pixel
{
demosiacedRowY[xIndex+2] = C5;
}
}
}
//Re-Iterate Rs and Bs and Interpolate Bs and Rs while gathering knowns:
imageRowY_minus1;
imageRowY_center = image.ptr(1);
imageRowY_added1 = image.ptr(1+1);
uchar*demosiacedRowY_minus1;
uchar*demosiacedRowY_center = demosiaced.ptr(1);
uchar*demosiacedRowY_added1 = demosiaced.ptr(1+1);
for(int y=2; y<image.rows-2; y++)
{
imageRowY_minus1 = imageRowY_center;
imageRowY_center = imageRowY_added1;
imageRowY_added1 = image.ptr(y+1);
demosiacedRowY_minus1 = demosiacedRowY_center;
demosiacedRowY_center = demosiacedRowY_added1;
demosiacedRowY_added1 = demosiaced.ptr(y+1);
for(int x=2 + y%2; x<image.cols-2; x+=2)
{
int image_xIndex_m1 = (x-1)*imageStep;
int image_xIndex_c0 = image_xIndex_m1+imageStep;
int image_xIndex_p1 = image_xIndex_c0+imageStep;
int demosiaced_xIndex_m1 = (x-1)*demosiacedStep;
int demosiaced_xIndex_c0 = demosiaced_xIndex_m1+demosiacedStep;
int demosiaced_xIndex_p1 = demosiaced_xIndex_c0+demosiacedStep;
//*****************************************************************************************************************************************************
float C1 = imageRowY_minus1[ image_xIndex_m1 ]; float G2 = imageRowY_minus1[ image_xIndex_c0 ]; float C3 = imageRowY_minus1[ image_xIndex_p1 ];
float G4 = imageRowY_center[ image_xIndex_m1 ]; float C5 = imageRowY_center[ image_xIndex_c0 ]; float G6 = imageRowY_center[ image_xIndex_p1 ];
float C7 = imageRowY_added1[ image_xIndex_m1 ]; float G8 = imageRowY_added1[ image_xIndex_c0 ]; float C9 = imageRowY_added1[ image_xIndex_p1 ];
//*****************************************************************************************************************************************************
//*****************************************************************************************************************************************************
float G1 = demosiacedRowY_minus1[ demosiaced_xIndex_m1+1 ]; float G3 = demosiacedRowY_minus1[ demosiaced_xIndex_p1+1 ];
float G5 = demosiacedRowY_center[ demosiaced_xIndex_c0+1 ];
float G7 = demosiacedRowY_added1[ demosiaced_xIndex_m1+1 ]; float G9 = demosiacedRowY_added1[ demosiaced_xIndex_p1+1 ];
//*****************************************************************************************************************************************************
float C_NE = absf( C7-C3 ) + absf( G5-G3 );
float C_SE = absf( C1-C9 ) + absf( G5-G9 );
float C_SW = absf( C3-C7 ) + absf( G5-G7 );
float C_NW = absf( C9-C1 ) + absf( G5-G1 );
float W_NE = 1/(1+C_NE);
float W_SE = 1/(1+C_SE);
float W_SW = 1/(1+C_SW);
float W_NW = 1/(1+C_NW);
float C_NE_norm = C3 + absf( G5-G3 )/2;
float C_SE_norm = C9 + absf( G5-G9 )/2;
float C_SW_norm = C7 + absf( G5-G7 )/2;
float C_NW_norm = C1 + absf( G5-G1 )/2;
float Color = (W_NE * C_NE_norm + W_SE * C_SE_norm + W_SW * C_SW_norm + W_NW * C_NW_norm) / (W_NE + W_SE + W_SW + W_NW);
if(x%2) //Curent x,y is a Bayer Red pixel, set Blue
{
demosiacedRowY_center[demosiaced_xIndex_c0+2] = Color;
}
else //Curent x,y is a Bayer Blue pixel, set Red
{
demosiacedRowY_center[demosiaced_xIndex_c0 ] = Color;
}
}
}
demosiacedRowY_minus1;
demosiacedRowY_center = demosiaced.ptr(1);
demosiacedRowY_added1 = demosiaced.ptr(1+1);
for(int y=2; y<image.rows-2; y++)
{
uchar*imageRowY = image.ptr(y);
demosiacedRowY_minus1 = demosiacedRowY_center;
demosiacedRowY_center = demosiacedRowY_added1;
demosiacedRowY_added1 = demosiaced.ptr(y+1);
for(int x=2 + !(y%2); x<image.cols-2; x+=2)
{
int image_xIndex = x*imageStep;
int demosiaced_xIndex_m1 = (x-1)*demosiacedStep;
int demosiaced_xIndex_c0 = demosiaced_xIndex_m1+demosiacedStep;
int demosiaced_xIndex_p1 = demosiaced_xIndex_c0+demosiacedStep;
// //**************************************************************************************************************************************************************************************************************************************************
// float m2_m2 = imageRowY_minus2[ xIndex_m2 ]; float m1_m2 = imageRowY_minus2[ xIndex_m1 ]; float c0_m2 = imageRowY_minus2[ xIndex_c0 ]; float p1_m2 = imageRowY_minus2[ xIndex_p1 ]; float p2_m2 = imageRowY_minus2[ xIndex_p2 ];
// float m2_m1 = imageRowY_minus1[ xIndex_m2 ]; float m1_m1 = imageRowY_minus1[ xIndex_m1 ]; float c0_m1 = imageRowY_minus1[ xIndex_c0 ]; float p1_m1 = imageRowY_minus1[ xIndex_p1 ]; float p2_m1 = imageRowY_minus1[ xIndex_p2 ];
//
// float m2_c0 = imageRowY_center[ xIndex_m2 ]; float m1_c0 = imageRowY_center[ xIndex_m1 ]; float c0_c0 = imageRowY_center[ xIndex_c0 ]; float p1_c0 = imageRowY_center[ xIndex_p1 ]; float p2_c0 = imageRowY_center[ xIndex_p2 ];
//
// float m2_p1 = imageRowY_added1[ xIndex_m2 ]; float m1_p1 = imageRowY_added1[ xIndex_m1 ]; float c0_p1 = imageRowY_added1[ xIndex_c0 ]; float p1_p1 = imageRowY_added1[ xIndex_p1 ]; float p2_p1 = imageRowY_added1[ xIndex_p2 ];
// float m2_p2 = imageRowY_added2[ xIndex_m2 ]; float m1_p2 = imageRowY_added2[ xIndex_m1 ]; float c0_p2 = imageRowY_added2[ xIndex_c0 ]; float p1_p2 = imageRowY_added2[ xIndex_p1 ]; float p2_p2 = imageRowY_added2[ xIndex_p2 ];
// //**************************************************************************************************************************************************************************************************************************************************
float c0_c0 = imageRowY[ image_xIndex ];
if(x%2)
{
//**************************************************************************************************************************************************
float c0_m1 = demosiacedRowY_minus1[ demosiaced_xIndex_c0 ];
float m1_c0 = demosiacedRowY_center[ demosiaced_xIndex_m1+2 ]; float p1_c0 = demosiacedRowY_center[ demosiaced_xIndex_p1+2 ];