2012-01-02 160 views
4

目標:圖像像素數據如何「掃描」圖像像素?

找到只包含黑色和透明像素的圖像左側的第一個黑色像素。

我有什麼:

我知道如何獲得的像素數據,並有黑色和透明的像素陣列(在這裏找到:https://stackoverflow.com/a/1262893/358480):

+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count 
{ 
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; 

// First get the image into your data buffer 
CGImageRef imageRef = [image CGImage]; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width * 4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
              bitsPerComponent, bytesPerRow, colorSpace, 
              kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

// Now your rawData contains the image data in the RGBA8888 pixel format. 
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 
for (int ii = 0 ; ii < count ; ++ii) 
{ 
    NSUInteger alpha = (rawData[byteIndex + 3] * 1.0)/255.0; 
    byteIndex += 4; 
    [result addObject:[NSNumber numberWithInt:alpha]]; 
} 

free(rawData); 

return result; 
} 

什麼問題?

我無法理解函數「掃描」圖像的順序。

我想要的只是獲取圖像的列,並找到列表1中的非transperant像素的第一列。這樣我會知道如何裁剪圖像的左側透明的一面?

如何獲得按列的像素?

感謝

沙尼

回答

4

的字節排序左到右,上到下的。所以,做你想要什麼,我想你要循環的rawData是這樣的:一個包含非透明像素將被x

int x = 0; 
int y = 0; 
BOOL found = NO; 
for (x = 0; x < width; x++) { 
    for (y = 0; y < height; y++) { 
     unsigned char alphaByte = rawData[(y*bytesPerRow)+(x*bytesPerPixel)+3]; 
     if (alphaByte > 0) { 
      found = YES; 
      break; 
     } 
    } 
    if (found) break; 
} 

NSLog(@"First non-transparent pixel at %i, %i", x, y); 

那麼你的第一列。

0

通常情況下,人們可以在圖像數組上從上到下遍歷行,並在每列內從左到右遍歷列。在這種情況下,您需要相反的結果:我們要遍歷每列,從左側開始,在列內遍歷所有行,並檢查是否存在黑色像素。

這會給你最左側的黑色像素:

size_t maxIndex = height * bytesPerRow; 
for (size_t x = 0; x < bytesPerRow; x += bytesPerPixel) 
{   
    for (size_t index = x; index < maxIndex; index += bytesPerRow) 
    { 
     if (rawData[index + 3] > 0) 
     { 
      goto exitLoop; 
     } 
    } 
} 
exitLoop: 

if (x < bytesPerRow) 
{ 
    x /= bytesPerPixel; 
    // left most column is `x` 
} 

那麼,這等於mattjgalloway,只要稍微優化,整潔得:o

雖然goto通常允許放棄內循環中的兩個循環,它仍然很難看。使我真的想念那些漂亮的流量控制語句D ...

您在示例代碼中提供的功能雖然有所不同。它從圖像中的某個位置開始(由xxyy定義),並從開始位置向右移動count像素,繼續到下一行。它將這些alpha值添加到我懷疑的某個數組中。

當通過xx = yy = 0時,這將找到具有一定條件的最頂部的像素,而不是最左邊的。這個轉換由上面的代碼給出。請注意,2D圖像僅僅是內存中的一維數組,從頂部行開始,從左到右並繼續行。做簡單的數學運算可以在行或列上進行迭代。