objective c - Compare RGB of two images in IOS -
in app need compare rgb of 2 images same or not. using code...
-(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; }
but method returns same rgb value when setting image name.
@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]; // additional setup after loading view, typically 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 resources can 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(); }
Comments
Post a Comment