0

I'm writing a simple program that finds the brightest pixel in an image which in the future with be implemented it something that finds the brightest pixel of a video frame. With small images it works fine. On my 8x8 test image thats all black with one white pixel it can seemingly realtime find the white pixel however when I upped to to a 1000x1000 image it takes several seconds find it. My goal is to be able to have it locate it 15+ times a second on something higher than 1000x1000. Is this even possible? Here's the code I'm using.

   //These are at the beginning of the class
   static NSBitmapImageRep *imageRepStatic;
   static float brightestBrightness;
   static CGPoint brightest;


    //This is in my function for getting the pixel
    for (int y = 0; y < imageRepStatic.size.height; y++) {
    for (int x = 0; x < imageRepStatic.size.width; x++) {

        NSColor *color = [imageRepStatic colorAtX:x y:y];
        NSArray *pixelData = [[NSString stringWithFormat:@"%@", color] componentsSeparatedByString:@" "];
        float red = [[pixelData objectAtIndex:1] floatValue];
        float green = [[pixelData objectAtIndex:2] floatValue];
        float blue = [[pixelData objectAtIndex:3] floatValue];
        float brightness = (red + green + blue) / 3;
        if (brightness >= brightestBrightness) {brightestBrightness = brightness; brightest = CGPointMake(x, y);}
    }
}

NSLog(@"The brightest pixel is at (%f, %f) and has a brightness of %f", brightest.x, brightest.y, brightestBrightness);
frame ++;
NSLog(@"%i", frame);
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • sounds like you should start digging into CoreImage and OpenGL/OpenCL. – vikingosegundo Dec 30 '15 at 01:20
  • And if you're not going to dig into CoreImage as @vikingosegundo suggests, this would actually be the perfect time to explore implementing a C based solution for this single specific problem rather than rely on Objective-C message passing multiple times per loop, which is extremely expensive in the context you are operating in. If you instead used a single continuous memory region and accessed it directly I expect you would see dramatic improvements even without any other optimization. – David Hoelzer Dec 30 '15 at 01:31
  • As a side point, this is one of the primary reasons that most of the graphics stuff in Apple is all accessing C library functions rather than Objective-C. For example, CGPointMake, which you call, is a C function, not an Objective-C function. Speed, speed speed. – David Hoelzer Dec 30 '15 at 01:32
  • another brute force improvement: cut image in several pieces, process those concurrently. – vikingosegundo Dec 30 '15 at 01:35
  • The brightness is not calculated by: float brightness = (red + green + blue) / 3; You should: float brightness = 0.2126* red + 0.7152* green + 0.0722* blue; – Anders Cedronius Dec 30 '15 at 09:04

0 Answers0