想知道是否有办法使用蒙版或甚至是自定义颜色空间来隔离图像中的单一颜色。我最终正在寻找一种快速的方法来隔离图像中的14种颜色 - 如果有掩蔽方法,它可能比走过像素更快。
感谢任何帮助!
答案 0 :(得分:0)
您可以使用自定义颜色空间(文档here),然后在以下代码中将其替换为“CGColorSpaceCreateDeviceGray()”:
- (UIImage *)convertImageToGrayScale:(UIImage *)image
{
// Create image rectangle with current image width/height
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
// Grayscale color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); // <- SUBSTITUTE HERE
// Create bitmap content with current image size and grayscale colorspace
CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
// Draw image into current context, with specified rectangle
// using previously defined context (with grayscale colorspace)
CGContextDrawImage(context, imageRect, [image CGImage]);
// Create bitmap image info from pixel data in current context
CGImageRef imageRef = CGBitmapContextCreateImage(context);
// Create a new UIImage object
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
// Release colorspace, context and bitmap information
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CFRelease(imageRef);
// Return the new grayscale image
return newImage;
}
此代码来自this blog,值得一看的是从图像中删除颜色。