iOS how to mask the image background color

前端 未结 1 1197
说谎
说谎 2021-01-23 12:54

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

  1. user can draw something on white background paper.
  2. my app allows user to capture the drawn image. Here
相关标签:
1条回答
  • 2021-01-23 13:31

    Well, since OpenCV itself is THE library, I guess that you are looking for a way to do that with 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:

    enter image description here

    • 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.

    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.

    0 讨论(0)
提交回复
热议问题