I am trying to remove the white background of few images programmatically and make it transparent. The format of the image is simple, it has a white background and a single object mainly positioned in the middle.
I want to replicate the functionality of the https://remove.bg website which makes the background of the image transparent.
I have tried using sharp
or imageMagick
software to make the background transparent. But it makes some of the pixels inside the main object become transparent too. Which I definitely don't want.
Was using the below code in imageMagik
to convert the white background to transparent
convert brownie.jpg -transparent white brownie.png
convert cakebig.jpg -fuzz 1% -transparent white cakebig.png
Didn't seem to work perfectly.
After running the commands, the images did become transparent but few of the pixels inside the main object got affected too.
Input Image
Output from ImageMagik (See how some of the pixels inside main object got transparent)
Expected Output from https://remove.bg (See no effect on the main object)
It seems like an image processing problem and seems like OpenCV
is the best solution for this. I don't know much about the library so it's a little tricky. Was checking out some code and came across grab cut
and graph cut
algorithm which can be used for image segmentation. But I am not totally sure about it. Please help in the proper solution to this in OpenCV.