问题描述
我想在我的iOS应用程序中执行以下操作:
I want to do following thing within in my iOS app:
- 用户可以在白色背景纸上绘制内容。
- 我的应用程序允许用户捕获绘制的图像。在这里,图像将以背景白色捕获。
- 最后从捕获的图像中我需要屏蔽白色背景颜色,只需将图像单独存入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.
- 首先,,这是OpenCV用来表示的数据类型图像;
- 然后,假设背景为白色,将背景与用户绘制的内容分开。根据下面的示例,此操作的结果使背景变黑,并且每个非黑色的像素将代表用户绘制的内容:
- 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
toUIImage
: for this, iterate on theMat
and copy every pixel that is not black to theUIImage
to have anUIImage
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如何掩盖图像背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!