如何在iOS上使用OpenCV对CGImage进行二值化? [英] How do I binarize a CGImage using OpenCV on iOS?

查看:365
本文介绍了如何在iOS上使用OpenCV对CGImage进行二值化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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进行二值化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆