图像像素数据如何“扫描”图像数据。图像像素? [英] How image pixel data "scans" the image pixels?
问题描述
目标:
查找仅包含黑色和透明像素的图像左侧的第一个黑色像素。
Finding the first black pixel on the left side of an image that contains black and transparent pixels only.
我有什么:
我知道如何获取像素数据并拥有黑色和透明像素的数组(在此处找到: https://stackoverflow.com/a/1262893/358480 ):
I know how to get the pixel data and have an array of black and transparent pixels (found it here : 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;
}
有什么问题?
我无法理解函数扫描图像的顺序。
I can not understand the order which the function "scans" the image.
我想要的只是获取图像的列并找到列表1非透明像素的第一列。这样我就会知道如何裁剪图像左侧透明的一面?
What i want is to get only the columns of the image and locate the first column that has at list 1 non-transperant pixel. this way I will know how to crop the left, transparent side of the image?
如何按列获取像素?
谢谢
Shani
推荐答案
字节从左到右,从上到下排序。所以要做你想做的事情,我想你想循环遍历 rawData
,如下所示:
The bytes are ordered left-to-right, top-to-bottom. So to do what you want, I think you want to loop over the rawData
like this:
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列
。
Then your first column that contains a non-transparent pixel will be column x
.
这篇关于图像像素数据如何“扫描”图像数据。图像像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!