2012-04-11 19 views
3

Ich habe zwei Bilder des gleichen Formats und Größe.iOS: Looping über die Pixel eines Bildes

Ich möchte über die Pixel jedes Bildes Schleife und finden Sie die meisten oberen linken und unteren rechten Differenzkoordinaten.

Der Unterschied ist die Pixelfarbe der beiden Pixel mit den gleichen Koordinaten.

Können Sie mir bitte einen Beispielcode bereitstellen, der Bildpixel durchläuft und seinen Farbwert erhält?

Antwort

4

Dies funktioniert für mich (das ARC verwendet):

- (BOOL) pointInside: (CGPoint) point withEvent: (UIEvent *) event { 
    CGRect r = CGRectZero ; 

    r.size =[self size] ; 

    CGFloat red ; 
    CGFloat green ; 
    CGFloat blue ; 
    CGFloat alpha ; 

    if (point.x < 0 || point.x > r.size.width || point.y < 0 || point.y > r.size.height) { 
     return NO ; 
    } 

    [inspector colorAt:point red:&red green:&green blue:&blue alpha:&alpha] ; 

    return alpha > 0.01f ; 
} 

Und hier ist die Magie :)

// CGImageInspection.h 
#import <Foundation/Foundation.h> 

@interface CGImageInspection : NSObject 

+ (CGImageInspection *) imageInspectionWithCGImage: (CGImageRef) imageRef ; 

- (void) colorAt: (CGPoint) location 
      red: (CGFloat *) red 
      green: (CGFloat *) green 
      blue: (CGFloat *) blue 
      alpha: (CGFloat *) alpha ; 

@end 

und die entsprechende Umsetzung:

// CGImageInspection.mm 
@interface CGImageInspection() 

- (id) initWithCGImage: (CGImageRef) imageRef ; 

@property (assign, nonatomic) CGContextRef context ; 
@property (assign, nonatomic) void *  pixels ; 
@property (assign, nonatomic) NSUInteger bytesPerRow ; 
@property (assign, nonatomic) NSUInteger bytesPerPixel ; 

@end 

@implementation CGImageInspection 

@synthesize context ; 
@synthesize pixels ; 
@synthesize bytesPerRow ; 
@synthesize bytesPerPixel ; 

- (void) dealloc { 
    if (self.context) { 
     ::CGContextRelease(self.context) ; 
     self.context = 0 ; 
    } 

    if (self.pixels) { 
     ::free(self.pixels) ; 
     self.pixels = 0 ; 
    } 

    ::NSLog(@"CGImageInspection going away") ; 
} 

+ (CGImageInspection *) imageInspectionWithCGImage: (CGImageRef) imageRef { 
    return [[CGImageInspection alloc] initWithCGImage:imageRef] ; 
} 

- (id) initWithCGImage: (CGImageRef) imageRef { 

    if (self = [super init]) { 
     size_t pixelsWide = ::CGImageGetWidth(imageRef) ; 
     size_t pixelsHigh = ::CGImageGetHeight(imageRef) ; 

     self.bytesPerPixel = 4 ; 
     self.bytesPerRow = (pixelsWide * self.bytesPerPixel) ; 
     int  bitmapByteCount = (self.bytesPerRow * pixelsHigh) ; 

     CGColorSpaceRef colorSpace= ::CGColorSpaceCreateDeviceRGB() ; 

     if (colorSpace == 0) { 
      return nil ; 
     } 

     self.pixels = ::calloc(bitmapByteCount, 1) ; 
     if (self.pixels == 0) { 
      ::CGColorSpaceRelease(colorSpace) ; 
      return nil ; 
     } 

     self.context = ::CGBitmapContextCreate(
      self.pixels 
     , pixelsWide 
     , pixelsHigh 
     , 8  // bits per component 
     , self.bytesPerRow 
     , colorSpace 
     , kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big) ; 

     if (self.context == 0) { 
      ::free(self.pixels) ; 
      self.pixels = 0 ; 
      ::CGColorSpaceRelease(colorSpace) ; 
      return nil ; 
     } 
     ::CGContextDrawImage(context, (CGRect) {{0, 0}, {pixelsWide, pixelsHigh}}, imageRef) ; 
     ::CGColorSpaceRelease(colorSpace) ; 
    } 

    return self ; 

} 

- (void) colorAt: (CGPoint) location 
      red: (CGFloat *) red 
      green: (CGFloat *) green 
      blue: (CGFloat *) blue 
      alpha: (CGFloat *) alpha { 

    int yy = (int) location.y ; 
    int xx = (int) location.x ; 

    int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel ; 
    unsigned char * raw = (unsigned char *) self.pixels ; 
    raw += byteIndex ; 

    *red = ((CGFloat) (*raw++))/255.0f ; 
    *green = ((CGFloat) (*raw++))/255.0f ; 
    *blue = ((CGFloat) (*raw++))/255.0f ; 
    *alpha = ((CGFloat) (*raw++))/255.0f ; 
} 

@end 

this helps .

0

Ich denke, Sie wurden downvoted, weil es überhaupt nicht klar ist, dass Sie gegoogelt haben, weiß nicht, vielleicht haben Sie, und Ihr google-fu funktionierte gerade nicht heute. Ich weiß, dass ich solche Tage hatte!

Wie auch immer, How to get the RGB values for a pixel on an image on the iphone sollte Ihnen wahrscheinlich helfen.

Verwandte Themen