图像像素数据如何“扫描”图像像素?

4

目标:

查找仅包含黑色和透明像素的图像左侧的第一个黑色像素。

我的条件:

我知道如何获取像素数据,并且有一个黑色和透明像素数组(在这里找到它:https://dev59.com/qHRB5IYBdhLWcg3w-8A9#1262893):

+ (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个不透明像素。这样我就知道如何裁剪图像左侧的透明部分了?

如何按列获取像素?

谢谢

Shani

2个回答

4
字节按照从左到右,从上到下的顺序排序。因此,为了实现您想要的功能,我认为您需要像这样循环遍历rawData
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);

那么第一列中包含不透明像素的列将是列x


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图像只是内存中的1D数组,从左到右从顶部行开始,然后继续下一行。通过简单的数学计算,可以迭代行或列。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接