Skip to content

Instantly share code, notes, and snippets.

@a1phanumeric
Created July 2, 2012 15:30
Show Gist options
  • Save a1phanumeric/3033803 to your computer and use it in GitHub Desktop.
Save a1phanumeric/3033803 to your computer and use it in GitHub Desktop.
Calculate luminosity of an image.
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);
}
}
@TheHyphen
Copy link

totalLuminance /= 255.0;

Why this?

@kevor
Copy link

kevor commented Apr 3, 2018

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