2013-05-05 33 views
2

我正在使用iOS開發的核心圖像框架查找該圖像的像素的顏色我用下面的代碼使用的是iOS

CGImageRef imgSource = self.ImageView.image.CGImage; 
CFDataRef m_DataRed1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource)); 
UInt8 *dataOrignal = (UInt8 *)CFDataGetBytePtr(m_DataRed1); 
double lenghtSource = CFDataGetLength(m_DataRed1); 
NSLog(@"lenght : %f",lenghtSource); 
int redPixel; 
int greenPixel; 
int bluePixel; 

for (int index=0 ; index<lenghtSource; index+=4) { 
    if (dataOrignal[index+1] ==236 || dataOrignal[index+2]==236 || dataOrignal[index+3]==236) { 
     dataOrignal[index + 1] = 205; 
     dataOrignal[index +2] = 25; 
     dataOrignal[index+3]= 55; 
     NSLog(@"this the value of red channel %d", index); 
    } 


} 


NSUInteger width = CGImageGetWidth(imgSource); //202 
NSUInteger height = CGImageGetHeight(imgSource);//173 
size_t bitsPerComponent = CGImageGetBitsPerComponent(imgSource); 
size_t bitsPerPixel = CGImageGetBitsPerPixel(imgSource); 
size_t bytesPerRow = CGImageGetBytesPerRow(imgSource); 


NSLog(@"the width = %u and height=%u of the image is ",width,height); 
NSLog(@"the bits per component is %zd", bitsPerComponent); 
NSLog(@"the bits per pixel is %zd", bitsPerPixel); 
NSLog(@"the bytes per row is %zd" , bytesPerRow); 


CGColorSpaceRef colorspace = CGImageGetColorSpace(imgSource); 
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imgSource); 
CFDataRef newData = CFDataCreate(NULL, dataOrignal, lenghtSource); 
CGDataProviderRef provider = CGDataProviderCreateWithCFData(newData); 

CGContextRef context = CGBitmapContextCreate(newData, width, height, bitsPerComponent, bytesPerRow, colorspace, bitmapInfo); 
CGPoint point = CGContextGetTextPosition(context); 
NSInteger xx = point.x; 
point.y; 

NSLog(@"this is the value of x axis %d",xx); 

CGImageRef newImg = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault); 





UIImage *newImage1 = [UIImage imageWithCGImage:newImg]; 
self.ImageView.image = newImage1; 

,但現在我想什麼是尋找像素的座標像素的x軸和y軸,所以任何人都可以幫助我嗎?或者任何人已經完成了這個代碼?請注意,我沒有使用opencv,如果你知道opencv然後請完全引導

+0

你想要一個像素的顏色在給定的x,y座標嗎?或給定顏色的x,y座標? – Jano 2013-05-05 20:03:06

+0

感謝您的答覆我想要的顏色像素的X,Y座標我在if語句中變化 – 2013-05-06 05:15:02

回答

0

我希望這是你在找什麼。

我將獲取寬度和高度的函數調用移動到for循環的上面,以便它們可以在for循環中用於x和y座標計算。另外,您可能打印了紅色通道的錯誤值。我在適當的版本調用中添加了CG對象。我不知道你是否需要釋放CF對象。

CGImageRef imgSource = self.ImageView.image.CGImage; 
CFDataRef m_DataRed1 = CGDataProviderCopyData(CGImageGetDataProvider(imgSource)); 
UInt8 *dataOrignal = (UInt8 *)CFDataGetBytePtr(m_DataRed1); 
double lenghtSource = CFDataGetLength(m_DataRed1); 
NSLog(@"lenght : %f",lenghtSource); 
int redPixel; 
int greenPixel; 
int bluePixel; 

int triggeredIndex = 0; 
NSUInteger width = CGImageGetWidth(imgSource); //202 
NSUInteger height = CGImageGetHeight(imgSource);//173 

for (int index=0 ; index<lenghtSource; index+=4) 
{ 
    if (dataOrignal[index+1] ==236 || dataOrignal[index+2]==236 || dataOrignal[index+3]==236) 
    { 
     dataOrignal[index+1] = 205; 
     dataOrignal[index+2] = 25; 
     dataOrignal[index+3] = 55; 

     int xcoord = index/(int)(4*width); //Don't think the casts are necessary, but just in case... 
     int ycoord = index % (int)(4*width); 

     //use xcoord and ycoord here... 

     //NSLog(@"this the value of red channel %d", index); 
     //did you mean this? 
     NSLog(@"this the value of red channel %d", dataOrginal[index]); 
    } 
} 


//moved width and height to before the loop 
size_t bitsPerComponent = CGImageGetBitsPerComponent(imgSource); 
size_t bitsPerPixel = CGImageGetBitsPerPixel(imgSource); 
size_t bytesPerRow = CGImageGetBytesPerRow(imgSource); 


NSLog(@"the width = %u and height=%u of the image is ",width,height); 
NSLog(@"the bits per component is %zd", bitsPerComponent); 
NSLog(@"the bits per pixel is %zd", bitsPerPixel); 
NSLog(@"the bytes per row is %zd" , bytesPerRow); 


CGColorSpaceRef colorspace = CGImageGetColorSpace(imgSource); 
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imgSource); 
CFDataRef newData = CFDataCreate(NULL, dataOrignal, lenghtSource); 
CGDataProviderRef provider = CGDataProviderCreateWithCFData(newData); 

CGContextRef context = CGBitmapContextCreate(newData, width, height, bitsPerComponent,  bytesPerRow, colorspace, bitmapInfo); 
CGPoint point = CGContextGetTextPosition(context); 
NSInteger xx = point.x; 
point.y; //this is unnecessary and does nothing 

NSLog(@"this is the value of x axis %d",xx); 

CGImageRef newImg = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorspace, bitmapInfo, provider, NULL, true, kCGRenderingIntentDefault); 





UIImage *newImage1 = [UIImage imageWithCGImage:newImg]; 
self.ImageView.image = newImage1; 

//always remember to clean up after yourself! 
CGImageRelease(imgSource); 
CGColorSpaceRelease(colorspace); 
CGDataProviderRelease(provider); 
CGContextRelease(context); 
CGImageRelease(newImg); 

請記住,不要忘了釋放你的CG對象!

+0

謝謝你的幫助,但我已經做到了 – 2013-08-24 15:41:21