图像像素数据如何“扫描”图像像素?
问题描述:
找到只包含黑色和透明像素的图像左侧的第一个黑色像素。
我有什么:
我知道如何获得的像素数据,并有黑色和透明的像素阵列(在这里找到: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像素的第一列。这样我会知道如何裁剪图像的左侧透明的一面?
如何获得按列的像素?
感谢
沙尼
答
的字节排序左到右,上到下的。所以,做你想要什么,我想你要循环的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);
那么你的第一列。
答
通常情况下,人们可以在图像数组上从上到下遍历行,并在每列内从左到右遍历列。在这种情况下,您需要相反的结果:我们要遍历每列,从左侧开始,在列内遍历所有行,并检查是否存在黑色像素。
这会给你最左侧的黑色像素:
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 ...
您在示例代码中提供的功能虽然有所不同。它从图像中的某个位置开始(由xx
和yy
定义),并从开始位置向右移动count
像素,继续到下一行。它将这些alpha值添加到我怀疑的某个数组中。
当通过xx = yy = 0
时,这将找到具有一定条件的最顶部的像素,而不是最左边的。这个转换由上面的代码给出。请注意,2D图像仅仅是内存中的一维数组,从顶部行开始,从左到右并继续行。做简单的数学运算可以在行或列上进行迭代。