【问题标题】:Detect most black pixel on an image - objective-c iOS检测图像上的大多数黑色像素-objective-c iOS
【发布时间】:2012-01-21 02:53:31
【问题描述】:

我有一张图片!很久没做像素检测了,我记得你必须以某种方式将像素转换为数组,然后找到图像的宽度以找出像素何时到达一行的末尾并转到下一个啊,很多复杂的东西哈哈!无论如何,我现在不知道如何做到这一点,但我需要检测名为“image1”的图像的最左侧最暗像素的 x&y 坐标……有什么好的起点吗?

【问题讨论】:

    标签: objective-c ios image colors pixel


    【解决方案1】:

    去你的书店,找到一本 Erica Sadun 写的名为“iOS Developer's Cookbook”的书。转到第 378 页,那里有像素检测方法。您可以查看这个 RGB 值数组并运行 for 循环来排序并找到 R、G 和 B 值之和最小的像素(这将是 0-255),这将为您提供最接近黑色的像素. 如果需要,我也可以发布代码。但是这本书是最好的来源,因为它给出了方法和解释。

    这些是我的,有一些变化。方法名称保持不变。我改变的只是图像,它基本上来自图像选择器。

    -(UInt8 *) createBitmap{
    if (!self.imageCaptured) {
            NSLog(@"Error: There has not been an image captured.");
            return nil;
        }
        //create bitmap for the image
        UIImage *myImage = self.imageCaptured;//image name for test pic
        CGContextRef context = CreateARGBBitmapContext(myImage.size);
        if(context == NULL) return NULL;
        CGRect rect = CGRectMake(0.0f/*start width*/, 0.0f/*start*/, myImage.size.width /*width bound*/, myImage.size.height /*height bound*/); //original
    //    CGRect rect = CGRectMake(myImage.size.width/2.0 - 25.0 /*start width*/, myImage.size.height/2.0 - 25.0 /*start*/, myImage.size.width/2.0 + 24.0 /*width bound*/, myImage.size.height/2.0 + 24.0 /*height bound*/); //test rectangle
    
        CGContextDrawImage(context, rect, myImage.CGImage);
        UInt8 *data = CGBitmapContextGetData(context);
        CGContextRelease(context);    
        return data;
    }
    CGContextRef CreateARGBBitmapContext (CGSize size){
    
        //Create new color space
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        if (colorSpace == NULL) {
            fprintf(stderr, "Error allocating color space\n");
            return NULL;
        }
        //Allocate memory for bitmap data
        void *bitmapData = malloc(size.width*size.height*4);
        if(bitmapData == NULL){
            fprintf(stderr, "Error: memory not allocated\n");
            CGColorSpaceRelease(colorSpace);
            return NULL;
        }
        //Build an 8-bit per channel context
        CGContextRef context = CGBitmapContextCreate(bitmapData, size.width, size.height, 8, size.width*4, colorSpace, kCGImageAlphaPremultipliedFirst);
        CGColorSpaceRelease(colorSpace);
        if (context == NULL) {
            fprintf(stderr, "Error: Context not created!");
            free(bitmapData);
            return NULL;
        }
        return context;
    
    }
    NSUInteger blueOffset(NSUInteger x, NSUInteger y, NSUInteger w){
        return y*w*4 + (x*4+3);
    }
    NSUInteger redOffset(NSUInteger x, NSUInteger y, NSUInteger w){
        return y*w*4 + (x*4+1);
    }
    

    底部的方法 redOffset 将为您提供 ARGB(Alpha-Red-Green-Blue)比例的 Red 值。要更改您正在查看的 ARGB 中的通道,请将添加到 redOffset 函数中的 x 变量的值更改为 0 以查找 alpha,将其保持为 1 以查找红色,2 查找绿色,3 查找蓝色。这是有效的,因为它只查看在上述方法中创建的数组,并且添加到 x 占索引值。本质上,使用三种颜色(红色、绿色和蓝色)的方法,并找到每个像素的总和。哪个像素的红色、绿色和蓝色一起具有最低值是最黑的。

    【讨论】:

    • 啊!多么幸运!我有 Erica 的《iPhone 开发者手册》一书,但没有《iOS 开发者手册》啊啊啊! :( 哈哈!介意分享一些像素检测方法的名称吗?
    • 这太棒了!但我如何检测最黑值?如果我没有绝对黑色的像素,如何检测下一个最接近黑色的颜色?
    • 我发现了一种对我有用的不同方法......但这确实回答了这个问题!问题已获批准!
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2016-06-30
    • 2021-04-26
    • 2011-06-04
    • 1970-01-01
    相关资源
    最近更新 更多