We did this using the GPUimage framework like this (calculate brightness and sharpness): (here are some snippets that may help you)
-(BOOL) calculateBrightness:(UIImage *) image { float result = 0; int i = 0; for (int y = 0; y < image.size.height; y++) { for (int x = 0; x < image.size.width; x++) { UIColor *color = [self colorAt:image atX:x andY:y]; const CGFloat * colors = CGColorGetComponents(color.CGColor); float r = colors[0]; float g = colors[1]; float b = colors[2]; result += .299 * r + 0.587 * g + 0.114 * b; i++; } } float brightness = result / (float)i; NSLog(@"Image Brightness : %f",brightness); if (brightness > 0.8 || brightness < 0.3) { return NO; } return YES;
}
-(BOOL) calculateSharpness:(UIImage *) image { GPUImageCannyEdgeDetectionFilter *filter = [[GPUImageCannyEdgeDetectionFilter alloc] init]; BinaryImageDistanceTransform *binImagTrans = [[BinaryImageDistanceTransform alloc] init ]; NSArray *resultArray = [binImagTrans twoDimDistanceTransform:[self getBinaryImageAsArray:[filter imageByFilteringImage:image]]]; if (resultArray == nil) { return NO; } int sum = 0; for (int x = 0; x < resultArray.count; x++) { NSMutableArray *col = resultArray[x]; sum += (int)[col valueForKeyPath:@"@max.intValue"]; }
}
But it is very slow. It takes approx. 40 seconds for a single image from an iPad camera.
source share