0

現在、iOS の UIImageview からいくつかの rgba 値を見つける方法を見つけようとしています。たとえば、0-0-0白い背景にあるすべての黒いピクセルを見つけて1-1-1、それらの座標を出力したいとします。

私の現在のコード(別のstackoverflow投稿から適応):

 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);

        }
    }
}                

}

誰かがコードを見て、考えられる解決策についてフィードバックを提供できますか?

4

1 に答える 1

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);
            }
        }
    }

助けてくれてありがとう。

于 2013-01-14T13:34:31.177 に答える