2013-01-11 26 views
0

我目前正試圖從iOS中的UIImageview中找到一些rgba值。因此,例如,我想在白色背景1-1-1上查找所有黑色0-0-0像素並輸出它們的座標。在iOS中檢測UIImageview /位圖中的特定RGB值

我當前的代碼(改編自另一個計算器後):

UIImage *myPicture = [UIImage imageNamed:@"somePicture.png"]; 

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(myPicture.CGImage)); 

myWidth = CGImageGetWidth(myPicture.CGImage); 
myHeight = CGImageGetHeight(myPicture.CGImage); 

const UInt8 *pixels = CFDataGetBytePtr(pixelData); 
UInt8 blackThreshold = 10; 
int bytesPerPixel_ = 4; 
for(x = 0; x < myWidth; x++) 
    for(y = 0; y < myHeight; y++) 

    { 
     { 
     int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_; 
     UInt8 redVal = pixels[pixelStartIndex + 1]; 
     UInt8 greenVal = pixels[pixelStartIndex + 2]; 
     UInt8 blueVal = pixels[pixelStartIndex + 3]; 
     if(redVal < blackThreshold && blueVal < blackThreshold && greenVal < blackThreshold) { 
      NSLog(@"x coordinate = %@", x); 
      NSLog(@"y coordinate = %@", y); 

     } 
    } 
}     

}

可能有人看代碼,並提供與問候一些反饋到一個可能的解決方案?

+1

你可以顯示一些代碼和你得到的輸出結果或錯誤嗎? –

+0

我沒有收到任何錯誤,我只是想找到解決問題的方法。 該代碼在我發佈的鏈接上,如果你有興趣。 –

+0

爲什麼它不工作?出了什麼問題?你有什麼嘗試? –

回答

2

經過一些調試,我解決了這個問題。

這裏是我的較暗的像素的座標代碼:

CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage)); 
    int myWidth = CGImageGetWidth(image.CGImage); 
    int myHeight = CGImageGetHeight(image.CGImage); 
    const UInt8 *pixels = CFDataGetBytePtr(pixelData); 
    UInt8 blackThreshold = 10 ; 
    //UInt8 alphaThreshold = 100; 
    int bytesPerPixel_ = 4; 
    for(int x = 0; x < myWidth; x++) 
    { 
     for(int y = 0; y < myHeight; y++) 
     { 
      int pixelStartIndex = (x + (y * myWidth)) * bytesPerPixel_; 
      UInt8 alphaVal = pixels[pixelStartIndex]; 
      UInt8 redVal = pixels[pixelStartIndex + 1]; 
      UInt8 greenVal = pixels[pixelStartIndex + 2]; 
      UInt8 blueVal = pixels[pixelStartIndex + 3]; 

      if(redVal < blackThreshold || blueVal < blackThreshold || greenVal < blackThreshold || alphaVal < blackThreshold) 
      { 
       NSLog(@"x %d, y = %d", x, y); 
      } 
     } 
    } 

感謝您的幫助球員。

相關問題