Ios 检查图像是否全白的最佳方法?
我正在尝试确定图形当前是否全部为白色。我能想到的解决方案是缩小图像的比例,然后逐像素检查图像是否为白色,一旦发现非白色的像素,就会返回否 这是可行的,但我有一种直觉,这可以用一种更有效的方式来完成。代码如下:Ios 检查图像是否全白的最佳方法?,ios,objective-c,performance,core-graphics,Ios,Objective C,Performance,Core Graphics,我正在尝试确定图形当前是否全部为白色。我能想到的解决方案是缩小图像的比例,然后逐像素检查图像是否为白色,一旦发现非白色的像素,就会返回否 这是可行的,但我有一种直觉,这可以用一种更有效的方式来完成。代码如下: - (BOOL)imageIsAllWhite:(UIImage *)image { CGSize size = CGSizeMake(100.0f, 100.0f); UIImageView *imageView = [[UIImageView alloc] initWi
- (BOOL)imageIsAllWhite:(UIImage *)image {
CGSize size = CGSizeMake(100.0f, 100.0f);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[image scaledImageWithSize:size]];
unsigned char pixel[4 * (int)size.width * (int)size.height];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext = CGBitmapContextCreate(
pixel,
(size_t)size.width,
(size_t)size.height,
8,
(size_t)(size.width * 4),
colorSpace,
kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(cgContext, 0, 0);
[imageView.layer renderInContext:cgContext];
CGContextRelease(cgContext);
CGColorSpaceRelease(colorSpace);
for (int i = 0; i < sizeof(pixel); i = i + 4) {
if(!(pixel[i] == 255 && pixel[i+1] == 255 && pixel[i+2] == 255)) {
return NO;
}
}
return YES;
}
-(BOOL)图像为全白:(UIImage*)图像{
CGSize size=CGSizeMake(100.0f,100.0f);
UIImageView*imageView=[[UIImageView alloc]initWithImage:[图像缩放图像大小:大小]];
无符号字符像素[4*(int)size.width*(int)size.height];
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef cgContext=CGBitmapContextCreate(
像素
(size_t)size.width,
(尺寸)尺寸。高度,
8.
(尺寸t)(尺寸宽度*4),
色彩空间,
kCGBitmapAlphaInfoMask和KCGimageAlphaPremultipledLast);
cgContextTranslateCm(cgContext,0,0);
[imageView.layer RenderContext:cgContext];
CGContextRelease(cgContext);
CGCOLORSPACTERELEASE(色彩空间);
对于(int i=0;i
有什么改进意见吗?请按照下面的代码检查UIImage是否为白色
- (BOOL) checkIfImage:(UIImage *)someImage {
CGImageRef image = someImage.CGImage;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
GLubyte * imageData = malloc(width * height * 4);
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = 8;
CGContextRef imageContext =
CGBitmapContextCreate(
imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
CGContextRelease(imageContext);
int byteIndex = 0;
BOOL imageExist = YES;
for ( ; byteIndex < width*height*4; byteIndex += 4) {
CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
imageExist = NO;
break;
}
}
return imageExist;
}
请按照下面的代码检查UIImage是否为白色
- (BOOL) checkIfImage:(UIImage *)someImage {
CGImageRef image = someImage.CGImage;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
GLubyte * imageData = malloc(width * height * 4);
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = 8;
CGContextRef imageContext =
CGBitmapContextCreate(
imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
CGContextRelease(imageContext);
int byteIndex = 0;
BOOL imageExist = YES;
for ( ; byteIndex < width*height*4; byteIndex += 4) {
CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
imageExist = NO;
break;
}
}
return imageExist;
}
请按照下面的代码检查UIImage是否为白色
- (BOOL) checkIfImage:(UIImage *)someImage {
CGImageRef image = someImage.CGImage;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
GLubyte * imageData = malloc(width * height * 4);
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = 8;
CGContextRef imageContext =
CGBitmapContextCreate(
imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
CGContextRelease(imageContext);
int byteIndex = 0;
BOOL imageExist = YES;
for ( ; byteIndex < width*height*4; byteIndex += 4) {
CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
imageExist = NO;
break;
}
}
return imageExist;
}
请按照下面的代码检查UIImage是否为白色
- (BOOL) checkIfImage:(UIImage *)someImage {
CGImageRef image = someImage.CGImage;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
GLubyte * imageData = malloc(width * height * 4);
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = 8;
CGContextRef imageContext =
CGBitmapContextCreate(
imageData, width, height, bitsPerComponent, bytesPerRow, CGImageGetColorSpace(image),
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
);
CGContextSetBlendMode(imageContext, kCGBlendModeCopy);
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
CGContextRelease(imageContext);
int byteIndex = 0;
BOOL imageExist = YES;
for ( ; byteIndex < width*height*4; byteIndex += 4) {
CGFloat red = ((GLubyte *)imageData)[byteIndex]/255.0f;
CGFloat green = ((GLubyte *)imageData)[byteIndex + 1]/255.0f;
CGFloat blue = ((GLubyte *)imageData)[byteIndex + 2]/255.0f;
CGFloat alpha = ((GLubyte *)imageData)[byteIndex + 3]/255.0f;
if( red != 1 || green != 1 || blue != 1 || alpha != 1 ){
imageExist = NO;
break;
}
}
return imageExist;
}
感觉上没有一条快速的路线可以往返于GPU,所以答案其实并不比采用统计方法和使用GCD来确保多核利用率更有趣 在大多数图像中,颜色更可能接近其他类似的颜色。所以,如果一个像素是白色的,那么它的相邻像素很可能也是白色的。因此,通过像素的严格线性渐进比采样点相隔一段距离,然后采样较近的点等更不可能快速找到白色像素。理想情况下,会有一些
f(x)
将相关范围的整数作为输入,并只返回每个整数一次,这样f(x)之间的距离
和f(x+1)
在x=0时最大,然后单调减小
如果映像相当大,如果您能够异步返回结果,则将任务分派到多个核的成本可能会被同时处理多个核的收益所抵消
您正在将图像大小固定为100x100像素。我将冒昧地假设您可以移动到128x128,因为这使得f(x)
很容易-在这种情况下,您可以做一点反转
例如
静态内联整数卷积(整数输入){
//位反转14位数字
返回((输入&0x0001)>7)|
((输入&0x0800)>>9)|
((输入&0x1000)>>11)|
((输入&0x2000)>>13);
}
... 在别处
__block BOOL hasFoundNonWhite=否;
常数int numberOfPixels=128*128;
const int pixelsPerBatch=128;
const int numberOfBatches=numberOfPixels/pixelsPerBatch;
发货申请(批次数量,
调度获取全局队列(调度队列优先级默认为0),
^(大小索引){
如果(非白色){
返回;
}
索引*=像素点阵;
对于(int i=index;i int arrayIndex=indexToCheck感觉没有快速的路线可以往返于GPU,所以答案其实并不比采用统计方法和使用GCD来确保多核利用率更有趣
在大多数图像中,颜色更可能接近其他类似的颜色。因此,如果一个像素是白色的,则其相邻像素也更有可能是白色的。因此,通过像素的严格线性渐进比采样点相隔一段距离,然后采样较近的点等更不可能快速找到白色像素通常会有一些f(x)
将相关的整数范围作为输入,并只返回一次,这样f(x)
和f(x+1)
之间的距离对于x=0
来说最大,然后单调减小
如果映像相当大,如果您能够异步返回结果,则将任务分派到多个核的成本可能会被同时处理多个核的收益所抵消
您正在将图像大小固定为100x100像素。我想冒昧地假设您可以将图像大小移动到128x128,因为这使得f(x)
很容易-在这种情况下,您只需做一点反转即可
例如
静态内联整数卷积(整数输入){
//位反转14位数字
返回((输入&0x0001)>7)|
((输入&0x0800)>>9)|
((输入&0x1000)>>11)|
((输入&0x2000)>>13);
}
在别处
__block BOOL hasFoundNonWhite=否;
常数int numberOfPixels=128*128;
const int pixelsPerBatch=128;
const int numberOfBatches=numberOfPixels/pixelsPerBatch;
发货申请(批次数量,
调度获取全局队列(调度队列优先级默认为0),
^(大小索引){
如果(非白色){
返回;
}
索引*=像素点阵;
对于(int i=index;i int arrayIndex=indexToCheck感觉没有一条快速的路线可以往返于GPU,所以答案其实并不比使用统计应用程序更有趣