0

Using C++ / OpenCV or Matlab I'm trying to find a method to replace the white background of an image from file with a transparent background and then save the image for further use elsewhere. (The image is similar to something like this: http://www.psdgraphics.com/file/monitor-on-white-background.jpg)

I understand this task can be completed easily with simple image editing software but I need to do this for a large batch of images. I've tried methods such as Make white background transparent png matlab as well as a bunch of others with no luck.

Thanks!

Community
  • 1
  • 1
MSTTm
  • 293
  • 3
  • 7
  • 18

1 Answers1

0
  1. Load the image. Loading images in OpenCV
  2. Convert it to a 4 channel image. OpenCV: transforming 3 channel image into 4 channel
  3. Determine where the background is adjust the alpha channel of those pixels accordingly. I don't know what your images look like, but if there is no other white in your images, it's a fairly simple task. (If a pixel is white, then it should be transparent.) Otherwise, if there is other white in your images, then you need to look for the contiguous region(s) of white that make up the background. See Flood Fill Algorithm
  4. Save the image (See the link from 1.)
Community
  • 1
  • 1
Matthew Pope
  • 7,212
  • 1
  • 28
  • 49
  • First, saving alpha does only work with png images (https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce). Secondly, the flood fill procedure isn't really a good idea. how about using masks and setTo? – kallaballa Oct 21 '22 at 07:58