我如何使用iPhone中的Objective-C来计算UIImage中的红色像素? [英] How i count red color pixel from the UIImage using objective-C in iphone?

查看:95
本文介绍了我如何使用iPhone中的Objective-C来计算UIImage中的红色像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iphone软件开发的初学者。
我在皮肤癌上开发应用程序,我想要计算或计算UIImage中的红色像素,这是由iphone相机捕获的。可以从UIImage计算红色像素吗?

I m the beginner in iphone software development. I developing the application on skin cancer in which i want to calculate or count red color pixel from UIImage which is captured by iphone camera.It is possible to count red pixel from UIImage?

推荐答案

由于这是几乎每周都要问的一个问题,我决定制作一个示例项目,展示如何做到这一点。您可以在以下位置查看代码:

Since this is a question that is asked almost weekly, I decided to make a little example project that shows how to do this. You can look at the code at:

http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/PixelAccess/

重要bit是以下代码,它接受 UIImage ,然后计算纯红色像素的数量。这是一个例子,您可以使用它并根据自己的算法进行修改:

The important bit is the following code, which takes a UIImage and then counts the number of pure red pixels. It is an example and you can use it and modify it for your own algorithms:

/**
 * Structure to keep one pixel in RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA format
 */

struct pixel {
    unsigned char r, g, b, a;
};

/**
 * Process the image and return the number of pure red pixels in it.
 */

- (NSUInteger) processImage: (UIImage*) image
{
    NSUInteger numberOfRedPixels = 0;

    // Allocate a buffer big enough to hold all the pixels

    struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap

        CGContextRef context = CGBitmapContextCreate(
            (void*) pixels,
            image.size.width,
            image.size.height,
            8,
            image.size.width * 4,
            CGImageGetColorSpace(image.CGImage),
            kCGImageAlphaPremultipliedLast
        );

        if (context != NULL)
        {
            // Draw the image in the bitmap

            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);

            // Now that we have the image drawn in our own buffer, we can loop over the pixels to
            // process it. This simple case simply counts all pixels that have a pure red component.

            // There are probably more efficient and interesting ways to do this. But the important
            // part is that the pixels buffer can be read directly.

            NSUInteger numberOfPixels = image.size.width * image.size.height;

            while (numberOfPixels > 0) {
                if (pixels->r == 255) {
                    numberOfRedPixels++;
                }
                pixels++;
                numberOfPixels--;
            }

            CGContextRelease(context);
        }

        free(pixels);
    }

    return numberOfRedPixels;
}

一个关于如何调用它的简单示例:

A simple example on how to call this:

- (IBAction) processImage
{
    NSUInteger numberOfRedPixels = [self processImage: [UIImage imageNamed: @"DutchFlag.png"]];
    label_.text = [NSString stringWithFormat: @"There are %d red pixels in the image", numberOfRedPixels];
}

Github上的示例项目包含一个完整的工作示例。

The example project on Github contains a complete working example.

这篇关于我如何使用iPhone中的Objective-C来计算UIImage中的红色像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆