gpt4 book ai didi

ios - 如何确定来自 iPhone 相机的图片中是否存在颜色

转载 作者:行者123 更新时间:2023-11-29 01:27:42 25 4
gpt4 key购买 nike

我被要求编写一个 iOS 应用程序,可以检查图片中是否存在红色。我对 Objective C 和 XCode 有一点经验,但不是很多。

如果有人能给我指出正确的方向,我将非常感激。

最佳答案

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

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

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

- (NSUInteger) processImage: (UIImage*) image withRed:(NSUInteger)r green:(NSUInteger)g blue:(NSUInteger)b
{
NSUInteger numberOfPixels = 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 p = image.size.width * image.size.height;

while (p > 0) {
if (pixels->r == r && pixels->g == g && pixels->b == b) {
numberOfPixels++;
}
pixels++;
p--;
}

CGContextRelease(context);
}

free(pixels);
}

return numberOfPixels;
}

使用:

NSUInteger numberOfSpecificColorPixels = [self processImage: [UIImage imageNamed: @"testImage.png" withRed:232 green:212 blue:192]];

这将为您提供特定颜色的像素数,然后您可以根据需要使用它

关于ios - 如何确定来自 iPhone 相机的图片中是否存在颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com