Removing background of image (pytorch, python)

Link to the jupyter notebook containing my code is here: https://github.com/alpizano/machine-learning-neural-network/blob/master/image_processing.ipynb

So I am starting a DL neural network project and my Professor assigned that we FIRST take an image in python (+pytorch) and convert 3d matrix (rows, cols, RGB) to 2d matrix (rows,cols) by averaging (R+G+B)/3 channels to turn into grayscale, THEN to remove the background of the image by picking an intensity of a pixel, say 200, and looping/iterating thru the pixels, ex. nested for loop below:

I’m trying not to make this post too long so I can actually get help/answers (new to machine learning) but I basically grayscaled the image. Below is 1st the color image, then 2nd the gray version:

original
1
grayscale
2

So after succesfully making the grayscale image, I then made a new 2d matrix, that I initialized to the (columns, rows) 850x1332 of my image and value 255 (white?). So I then used a nested for loop to iterate thru the old gray scale matrix and COPY any pixels < 50 (black/dark colors?) to my new 2d matrix I made earlier. The for loop code & resulting image was this:

nested for loop code


supposed to have Background removed, but Foreground color also gone
3

My problem is that I only wanted the background removed, and you can see it’s removing the color inside the main image (Vegeto from Dragonball super).

Can anyone recommend me what steps I could take? Is it possible to iterate thru each pixel and change their RGB value of the original color img and look for a range of blues, for ex? but then i would have to change parameters for each image…

Or do I simply need to start building a neural network now and utilize object detection/segmention so it “intelligently” removes the background BUT NOT foreground?

I would greatly appreciate any help, I’ve programmed before but fairly new to the Machine learning realm, Dnn, and I really am intrigued and curious. I currently using the Getting Started with Deep
Learning Using PyTorch by Vishnu Subramanian, so I’ll take any recommendations to books, youtube videos, or online courses.

The problem is that your approach is very image-specific, and it generally won’t work with images where e.g., your background is much darker than the image itself.

I tried different methods a few years ago and based on my experience, reliable background removal is quite a challenging task. Eventually, you still need to check all output images to make sure things work properly. It’s basically more of an object detection and image segmentation task that once you detected the object, you basically apply a mask.

I would recommend starting with established, non-DL CV tools for that. E.g., using OpenCV or scikit-image. E.g., have a look at http://scikit-image.org/docs/dev/user_guide/tutorial_segmentation.html

1 Like