简体   繁体   English

检测图像上的大多数黑色像素 - objective-c iOS

[英]Detect most black pixel on an image - objective-c iOS

I have an image! 我有一张图片! It's been so long since I've done pixel detection, I remember you have to convert the pixels to an array somehow and then find the width of the image to find out when the pixels reach the end of a row and go to the next one and ahh, lots of complex stuff haha! 我已经做了像素检测已经很久了,我记得你必须以某种方式将像素转换为数组,然后找到图像的宽度以找出像素到达行尾的时间并转到下一行和啊,很多复杂的东西哈哈! Anyways I now have no clue how to do this anymore but I need to detect the left-most darkest pixel's x&y coordinates of my image named "image1"... Any good starting places? 无论如何,我现在不知道如何做到这一点,但我需要检测我的图像中最左边最黑暗像素的x和y坐标名为“image1”...任何好的起点?

Go to your bookstore, find a book called "iOS Developer's Cookbook" by Erica Sadun. 去你的书店,找到Erica Sadun的一本名为“iOS Developer's Cookbook”的书。 Go to page 378-ish and there are methods for pixel detection there. 转到第378页,其中有像素检测方法。 You can look in this array of RGB values and run a for loop to sort and find the pixel that has the smallest sum of R, G, and B values (this will be 0-255) that will give you the pixel closest to black. 您可以查看这个RGB值数组并运行for循环来排序并找到具有最小R,G和B值之和的像素(这将是0-255),它将为您提供最接近黑色的像素。 I can also post the code if needed. 如果需要,我也可以发布代码。 But the book is the best source as it gives methods and explanations. 但这本书是最好的来源,因为它给出了方法和解释。

These are mine with some changes. 这些是我的一些变化。 The method name remains the same. 方法名称保持不变。 All I changed was the image which basically comes from an image picker. 我改变的只是图像,它基本上来自图像选择器。

-(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);
}

The method on the bottom, redOffset, will get you the Red value in the ARGB (Alpha-Red-Green-Blue) scale. 底部的方法redOffset将以ARGB(Alpha-Red-Green-Blue)比例获得Red值。 To change what channel in the ARGB you are looking at, change the value added to the x variable in the redOffset function to 0 to find alpha, keep it at 1 to find red, 2 to find green, and 3 to find blue. 要更改您正在查看的ARGB中的哪个频道,请将redOffset函数中添加到x变量的值更改为0以查找alpha,将其保持为1以查找红色,将其保持为2以查找绿色,将3更改为蓝色。 This works because it just looks at an array made in the methods above and the addition to x accounts for the index value. 这是有效的,因为它只是查看上面方法中的数组,并添加x帐户的索引值。 Essentially, use methods for the three colors (Red, green, and blue) and find the summation of those for each pixel. 基本上,使用三种颜色(红色,绿色和蓝色)的方法,并找到每个像素的总和。 Whichever pixel has the lowest value of red, green, and blue together is the most black. 无论哪个像素具有最低的红色,绿色和蓝色值,都是最黑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM