2015-07-22 1 views
5

Dans mon application, j'ai besoin de comparer le RVB de deux images sont identiques ou non. J'utilise ce code ...Comparer RGB de deux images dans IOS

-(CGFloat)compareImage:(UIImage *)imgPre capturedImage:(UIImage *)imgCaptured 
{  

int colorDiff; 
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(imgPre.CGImage)); 
int myWidth = (int)CGImageGetWidth(imgPre.CGImage)/2; 
int myHeight =(int)CGImageGetHeight(imgPre.CGImage)/2; 
const UInt8 *pixels = CFDataGetBytePtr(pixelData); 

int bytesPerPixel_ = 4; 
int pixelStartIndex = (myWidth + myHeight) * bytesPerPixel_; 
UInt8 alphaVal = pixels[pixelStartIndex]; 
UInt8 redVal = pixels[pixelStartIndex + 1]; 
UInt8 greenVal = pixels[pixelStartIndex + 2]; 
UInt8 blueVal = pixels[pixelStartIndex + 3]; 
UIColor *color = [UIColor colorWithRed:(redVal/255.0f) green:(greenVal/255.0f) blue:(blueVal/255.0f) alpha:(alphaVal/255.0f)]; 
NSLog(@"color of image=%@",color); 
NSLog(@"color of R=%hhu/G=%hhu/B=%hhu",redVal,greenVal,blueVal); 


CFDataRef pixelDataCaptured = CGDataProviderCopyData(CGImageGetDataProvider(imgCaptured.CGImage)); 
int myWidthCaptured = (int)CGImageGetWidth(imgCaptured.CGImage)/2; 
int myHeightCaptured =(int)CGImageGetHeight(imgCaptured.CGImage)/2; 
const UInt8 *pixelsCaptured = CFDataGetBytePtr(pixelDataCaptured); 

int pixelStartIndexCaptured = (myWidthCaptured + myHeightCaptured) * bytesPerPixel_; 
UInt8 alphaValCaptured = pixelsCaptured[pixelStartIndexCaptured]; 
UInt8 redValCaptured = pixelsCaptured[pixelStartIndexCaptured + 1]; 
UInt8 greenValCaptured = pixelsCaptured[pixelStartIndexCaptured + 2]; 
UInt8 blueValCaptured = pixelsCaptured[pixelStartIndexCaptured + 3]; 
UIColor *colorCaptured = [UIColor colorWithRed:(redValCaptured/255.0f) green:(greenValCaptured/255.0f) blue:(blueValCaptured/255.0f) alpha:(alphaValCaptured/255.0f)]; 
NSLog(@"color of captured image=%@",colorCaptured); 
NSLog(@"color of captured image R=%hhu/G=%hhu/B=%hhu",redValCaptured,greenValCaptured,blueValCaptured); 

colorDiff=sqrt((redVal-249)*(redVal-249)+(greenVal-greenValCaptured)*(greenVal-greenValCaptured)+(blueVal-blueValCaptured)*(blueVal-blueValCaptured)); 

return colorDiff; 
} 

mais cette méthode retourne même valeur RVB quand je suis en train de l'image par son nom.

+6

Vous n'utilisez pas 'imgCaptured' dans votre méthode. C'est bizarre. – Larme

+0

peut-être il appelle cette méthode après avoir capturé l'image de la méthode déléguée. @ Larme –

+0

@ Larme: désolé par erreur, j'ai posté un léger code erroné ... s'il vous plaît vérifier le code uodated ... ici l'image imgCaptured est stockée image et imgPre est actuellement capturé image par caméra. J'obtiens la même valeur RVB de l'image stockée pour l'image difféente –

Répondre

1
@interface ViewController()<UIImagePickerControllerDelegate, UINavigationControllerDelegate> 
{ 
    IBOutlet UIImageView *imgLeft; 
    IBOutlet UIImageView *imgRight; 

    IBOutlet UILabel *lblLeft; 
    IBOutlet UILabel *lblRight; 

    IBOutlet UILabel *lblResult; 


    CGFloat redLeft,GreenLeft,BlueLeft; 
    CGFloat redRight,GreenRight,BlueRight; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UITapGestureRecognizer * tapRecognizerLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTappedLeft:)]; 
    [imgLeft addGestureRecognizer:tapRecognizerLeft]; 
    imgLeft.userInteractionEnabled = YES; 

    UITapGestureRecognizer * tapRecognizerRight = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTappedRight:)]; 
    [imgRight addGestureRecognizer:tapRecognizerRight]; 
    imgRight.userInteractionEnabled = YES; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(IBAction)calculateDistance:(id)sender 
{ 
    CGFloat derivedRedDiff=redLeft-redRight; 
    if (derivedRedDiff<0) { 
     derivedRedDiff*=-1; 
    } 
    CGFloat derivedGreenDiff=GreenLeft-GreenRight; 
    if (derivedGreenDiff<0) { 
     derivedGreenDiff*=-1; 
    } 
    CGFloat derivedBlueDiff=BlueLeft-BlueRight; 
    if (derivedBlueDiff<0) { 
     derivedBlueDiff*=-1; 
    } 

    CGFloat distance=sqrtf(derivedRedDiff*derivedRedDiff+derivedGreenDiff*derivedGreenDiff+derivedBlueDiff*derivedBlueDiff); 
    NSLog(@"Distance = %f",distance); 
    lblResult.text=[NSString stringWithFormat:@"Distance = %f",distance]; 
} 



-(void)imageTappedLeft:(UITapGestureRecognizer *)recognizer 
{ 
    CGPoint point = [recognizer locationInView:imgLeft]; 

    UIGraphicsBeginImageContext(imgLeft.bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [imgLeft.layer renderInContext:context]; 

    int bpr =(int) CGBitmapContextGetBytesPerRow(context); 
    unsigned char * data = CGBitmapContextGetData(context); 
    if (data != NULL) 
    { 
     int offset = bpr*round(point.y) + 4*round(point.x); 
     int blue = data[offset+0]; 
     int green = data[offset+1]; 
     int red = data[offset+2]; 
     int alpha = data[offset+3]; 

     CGFloat derivedAlpha=alpha/255.0f; 

     NSLog(@"%d %d %d %d %f", alpha, red, green, blue,derivedAlpha); 

     redLeft=red; 
     GreenLeft=green; 
     BlueLeft=blue; 

     lblLeft.text=[NSString stringWithFormat:@"%d %d %d %f", red, green, blue,derivedAlpha]; 
    } 

    UIGraphicsEndImageContext(); 
} 

-(void)imageTappedRight:(UITapGestureRecognizer *)recognizer 
{ 
    CGPoint point = [recognizer locationInView:imgRight]; 

    UIGraphicsBeginImageContext(imgRight.bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [imgRight.layer renderInContext:context]; 

    int bpr =(int) CGBitmapContextGetBytesPerRow(context); 
    unsigned char * data = CGBitmapContextGetData(context); 
    if (data != NULL) 
    { 
     int offset = bpr*round(point.y) + 4*round(point.x); 
     int blue = data[offset+0]; 
     int green = data[offset+1]; 
     int red = data[offset+2]; 
     int alpha = data[offset+3]; 

     CGFloat derivedAlpha=alpha/255.0f; 

     NSLog(@"%d %d %d %d %f", alpha, red, green, blue,derivedAlpha); 

     redRight=red; 
     GreenRight=green; 
     BlueRight=blue; 

     lblRight.text=[NSString stringWithFormat:@"%d %d %d %f", red, green, blue,derivedAlpha]; 
    } 

    UIGraphicsEndImageContext(); 
}