简体   繁体   English

如何在uiimage中查找特定颜色并更改该颜色

[英]how to find particular color in an uiimage and change that color

I want to find particular colour in an image,for example I have an image which has many colours.by assigning red parameter i want change other colour pixels except red colour pixels. 我想在图像中找到特定的颜色,例如,我有一个具有多种颜色的图像。通过分配红色参数,我想更改除红色像素以外的其他颜色像素。 please watch the below image. 请观看下图。 in the above updated image only blur colour is highlighted all other colours were changed. 在上面更新的图像中,仅突出显示模糊颜色,其他所有颜色均已更改。

在此处输入图片说明 Updated Pic 更新的图片

在此处输入图片说明

Ok, so I got curious on how hard would it be to achieve this reverse chroma-key, and it turns out that given the example from Apple it is quite easy! 好的,所以我很好奇实现反向色度键有多困难,事实证明,以苹果公司示例为例,这很容易!

// Allocate memory
const unsigned int size = 32;
float *cubeData = (float *)malloc (size * size * size * sizeof (float) * 4);
float rgb[3], *c = cubeData;
CGFloat hue = CGFLOAT_MAX;

CGFloat minHueAngle = 210;
CGFloat maxHueAngle = 240;

// Populate cube with a simple gradient going from 0 to 1
for (int z = 0; z < size; z++){
    rgb[2] = ((double)z)/(size-1); // Blue value
    for (int y = 0; y < size; y++){
        rgb[1] = ((double)y)/(size-1); // Green value
        for (int x = 0; x < size; x ++){
            rgb[0] = ((double)x)/(size-1); // Red value
            // Convert RGB to HSV
            // You can find publicly available rgbToHSV functions on the Internet
            UIColor *color = [UIColor colorWithRed:rgb[0] green:rgb[1] blue:rgb[2] alpha:1];
            [color getHue:&hue saturation:nil brightness:nil alpha:nil];
            hue *= 360;

            // Use the hue value to determine which to make transparent
            // The minimum and maximum hue angle depends on
            // the color you want to remove
            float alpha = (hue > minHueAngle && hue < maxHueAngle) ? 1.0f: 0.0f;
            // Calculate premultiplied alpha values for the cube
            c[0] = rgb[0] * alpha;
            c[1] = rgb[1] * alpha;
            c[2] = rgb[2] * alpha;
            c[3] = alpha;
            c += 4; // advance our pointer into memory for the next color value
        }
    }
}
// Create memory with the cube data
NSData *data = [NSData dataWithBytesNoCopy:cubeData
                                    length:(size * size * size * sizeof (float) * 4)
                              freeWhenDone:YES];
CIFilter *colorCube = [CIFilter filterWithName:@"CIColorCube"];
[colorCube setValue:@(size) forKey:@"inputCubeDimension"];
[colorCube setValue:data forKey:@"inputCubeData"];

UIImage *originalImage = [UIImage imageNamed:@"yourImageName"];
CIImage *ciImage = [CIImage imageWithCGImage:originalImage.CGImage];
[colorCube setValue:ciImage forKey:kCIInputImageKey];


UIImage *result = [UIImage imageWithCIImage:[colorCube outputImage]];

This resulted in this image, from your example image : 这是从您的示例图片生成的图片: 在此处输入图片说明

I've got to say it looks pretty decent, given I only tinkered with the values for a minute or two. 我不得不说它看起来相当不错,因为我只修改了一两分钟的值。 Of course, to achieve the total end result you want, you'd need to use two more filters - namely CIPhotoEffectMono and CISourceOverCompositing to first make your original image black & white and then overlay the result from CubeMap filter on the black & white image. 当然,要获得所需的最终总结果,您还需要使用两个滤镜CIPhotoEffectMonoCISourceOverCompositing首先将原始图像CISourceOverCompositing黑白,然后将来自CubeMap滤镜的结果覆盖在黑白图像上。

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

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