Created
July 2, 2012 15:30
-
-
Save a1phanumeric/3033803 to your computer and use it in GitHub Desktop.
Calculate luminosity of an image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSArray* dayArray = [NSArray arrayWithObjects:@"Image One",@"Image Two",@"Image Three",nil]; | |
for(NSString* day in dayArray) | |
{ | |
for(int i=1;i<=2;i++) | |
{ | |
UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png",day,i]]; | |
unsigned char* pixels = [image rgbaPixels]; | |
double totalLuminance = 0.0; | |
for(int p=0;p<image.size.width*image.size.height*4;p+=4) | |
{ | |
totalLuminance += pixels[p]*0.299 + pixels[p+1]*0.587 + pixels[p+2]*0.114; | |
} | |
totalLuminance /= (image.size.width*image.size.height); | |
totalLuminance /= 255.0; | |
NSLog(@"%@ (%d) = %f",day,i,totalLuminance); | |
} | |
} |
If the values are in the 0-255 range you have to divide. (Usally are 0-1, then you dont need to divide by 255)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why this?