Reputation: 236
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.
Upvotes: 1
Views: 1032
Reputation: 8006
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.
Upvotes: 3