- (UIImage*) detectRects2: (UIImage*)img {
int exifOrientation;
switch (img.imageOrientation) {
case UIImageOrientationUp:
exifOrientation = 1;
break;
case UIImageOrientationDown:
exifOrientation = 3;
break;
case UIImageOrientationLeft:
exifOrientation = 8;
break;
case UIImageOrientationRight:
exifOrientation = 6;
break;
case UIImageOrientationUpMirrored:
exifOrientation = 2;
break;
case UIImageOrientationDownMirrored:
exifOrientation = 4;
break;
case UIImageOrientationLeftMirrored:
exifOrientation = 5;
break;
case UIImageOrientationRightMirrored:
exifOrientation = 7;
break;
default:
break;
}
NSDictionary *detectorOptions = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh };
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeRectangle context:nil options:detectorOptions];
NSArray *features = [faceDetector featuresInImage:[CIImage imageWithCGImage:img.CGImage]
options:@{CIDetectorImageOrientation:[NSNumber numberWithInt:exifOrientation]}];
CIImage *resultImage = [image copy];
for (CIRectangleFeature *feature1 in *features) {
resultImage = [image imageByApplyingFilter:@"CIPerspectiveCorrection"
withInputParameters:@{@"inputTopLeft":[CIVector vectorWithCGPoint:feature1.topLeft] ,
@"inputTopRight": [CIVector vectorWithCGPoint:feature1.topRight],
@"inputBottomLeft": [CIVector vectorWithCGPoint:feature1.bottomLeft],
@"inputBottomRight": [CIVector vectorWithCGPoint:feature1.bottomRight]}];
}
//CIImage *outputImage = filter.outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef outImage = [context createCGImage: resultImage
fromRect: [outputImage extent]];
return [UIImage imageWithCGImage: outImage];
}