如何在 uiimage 中找到特定颜色并更改该颜色
how to find particular color in an uiimage and change that color
我想在图像中找到特定的颜色,例如我有一个图像有很多 colours.by 分配红色参数我想改变除红色像素之外的其他颜色像素。请看下图。在上面的更新图像中,只有模糊颜色突出显示所有其他颜色都已更改。
更新图片
好吧,所以我很好奇要实现这个反转有多难 chroma-key,事实证明给定 example from Apple 这很容易!
// 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]];
从您的示例图片中得出这张图片:
我不得不说它看起来相当不错,因为我只修改了一两分钟的值。当然,为了达到你想要的最终结果,你需要使用两个过滤器 - 即 CIPhotoEffectMono
和 CISourceOverCompositing
首先使你的原始图像黑白,然后覆盖 CubeMap 的结果在黑白图像上过滤。
我想在图像中找到特定的颜色,例如我有一个图像有很多 colours.by 分配红色参数我想改变除红色像素之外的其他颜色像素。请看下图。在上面的更新图像中,只有模糊颜色突出显示所有其他颜色都已更改。
好吧,所以我很好奇要实现这个反转有多难 chroma-key,事实证明给定 example from Apple 这很容易!
// 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]];
从您的示例图片中得出这张图片:
我不得不说它看起来相当不错,因为我只修改了一两分钟的值。当然,为了达到你想要的最终结果,你需要使用两个过滤器 - 即 CIPhotoEffectMono
和 CISourceOverCompositing
首先使你的原始图像黑白,然后覆盖 CubeMap 的结果在黑白图像上过滤。