iOS如何掩盖图像背景颜色 [英] iOS how to mask the image background color

查看:176
本文介绍了iOS如何掩盖图像背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的iOS应用程序中执行以下操作:

I want to do following thing within in my iOS app:


  1. 用户可以在白色背景纸上绘制内容。

  2. 我的应用程序允许用户捕获绘制的图像。在这里,图像将以背景白色捕获。

  3. 最后从捕获的图像中我需要屏蔽白色背景颜色,只需将图像单独存入UIImage对象。

我完成了第1步和第2步。但我不知道如何做最后一步。有没有openCV库我可以在我的iOS应用程序中使用它?

I completed the steps 1 and 2. But i do not have any idea how to do the last step. Is there any openCV library that i can use it with my iOS app?.

任何可能非常感激的帮助。

Any help that might be really appreciated.

推荐答案

好吧,既然 OpenCV本身就是库,我猜你正在寻找一种方式用OpenCV做到这一点。

Well, since OpenCV itself is THE library, I guess that you are looking for a way to do that with OpenCV.


  • 首先,将输入图像转换为 Mat ,这是OpenCV用来表示的数据类型图像;

  • 然后,假设背景为白色,阈值 Mat 将背景与用户绘制的内容分开。根据下面的示例,此操作的结果使背景变黑,并且每个非黑色的像素将代表用户绘制的内容:

  • First, convert the input image to Mat, which is the data type OpenCV uses to represent an image;
  • Then, assuming the background is white, threshold the Mat to separate the background from whatever the user draw. According to the example below, the result of this operation makes the background black, and every pixel that is not black will represent something the user has draw:


  • 最后,将生成的 Mat 转换为 UIImage :为此,迭代 Mat 并将每个非黑色像素复制到 UIImage ,以获得 UIImage 仅包含用户绘制的内容。

  • Finally, convert the resulting Mat to UIImage: for this, iterate on the Mat and copy every pixel that is not black to the UIImage to have an UIImage that contains only what the user draw.

更好的想法是迭代阈值 Mat ,找出哪个像素不是黑色,而不是直接将其复制到新的 UIImage ,从原始像素中复制该像素(x,y) UIImage ,所以你最后有一个彩色像素,这样可以得到更真实的结果。

A better idea is to iterate on the thresholded Mat, figure out which pixel is not black, and instead of copying it directly to the new UIImage, copy that pixel (x,y) from the original UIImage, so you have a colored pixel at the end, which gives a more realistic result.

这篇关于iOS如何掩盖图像背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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