问题描述
在我的 iOS 项目中,我有一个 RGB 格式的 CGImage,我想对其进行二值化(转换为黑白).我想使用 OpenCV 来做到这一点,但我是 OpenCV 的新手.我找到了一本关于 OpenCV 的书,但它不适用于 iPhone.
In my iOS project, I have a CGImage in RGB that I'd like to binarize (convert to black and white). I would like to use OpenCV to do this, but I'm new to OpenCV. I found a book on OpenCV, but it was not for iPhone.
如何在 iOS 上使用 OpenCV 对这样的图像进行二值化?
How can I binarize such an image using OpenCV on iOS?
推荐答案
如果你不想在你的 iOS 项目中设置 OpenCV,我的开源 GPUImage 框架内部有两个阈值过滤器,用于图像的二值化,一个简单阈值和一个基于像素附近局部亮度的自适应阈值.
If you don't want to set up OpenCV in your iOS project, my open source GPUImage framework has two threshold filters within it for binarization of images, a simple threshold and an adaptive one based on local luminance near a pixel.
您可以对图像应用一个简单的阈值,然后使用如下代码提取生成的二值化 UIImage:
You can apply a simple threshold to an image and then extract a resulting binarized UIImage using code like the following:
UIImage *inputImage = [UIImage imageNamed:@"inputimage.png"];
GPUImageLuminanceThresholdFilter *thresholdFilter = [[GPUImageLuminanceThresholdFilter alloc] init];
thresholdFilter.threshold = 0.5;
UIImage *thresholdFilter = [thresholdFilter imageByFilteringImage:inputImage];
(如果您的应用程序中不使用 ARC,请释放上述过滤器)
(release the above filter if not using ARC in your application)
如果您希望将此图像显示到屏幕上,可以将阈值输出发送到 GPUImageView.如果您愿意,您还可以使用这些过滤器处理实时视频,因为它们完全在 GPU 上运行.
If you wish to display this image to the screen instead, you can send the thresholded output to a GPUImageView. You can also process live video with these filters, if you wish, because they are run entirely on the GPU.
这篇关于如何在 iOS 上使用 OpenCV 对 CGImage 进行二值化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!