Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I crop the black background of the image using OpenCV in Python?

So I have an image processing task at hand which requires me to crop a certain portion of an image. I have no prior experience of OpenCV. I would like to know of a certain approach where I should be headed.

Sample Input Image: enter image description here

Sample Output Image: enter image description here

What I initially thought was to convert the image to a bitmap and remove pixels that are below or above a certain threshold. Since I am free to use OpenCV and Python, I would like to know of any automated algorithm that does so and if not, what should be the right approach for such a problem. Thank you.

like image 941
K. K. Avatar asked Dec 13 '22 15:12

K. K.


1 Answers

Applying a simple threshold should get rid of the background, provided it's always darker than the foreground. If you use the Otsu thresholding algorithm, it should choose a good partition for you. Using your example as input, this gives:

Otsu Thresholded image

Next you could compute the bounding box to select the region of the foreground. Provided the background is distinct enough and there are no holes, this gives you the resulting rect:

[619 x 96 from (0, 113)]

You can then use this rect to crop the original, to produce the desired result:

Cropped original

I wrote the code to solve this in C++. A rough translation into Python would look something like this:

import cv2 as cv

img = cv.imread(sys.argv[1])

grayscale = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

thresholded = cv.threshold(grayscale, 0, 255, cv.THRESH_OTSU)

imwrite("otsu.png", thresholded)

bbox = cv.boundingRect(thresholded)

x, y, w, h = bbox

print(bbox)

foreground = img[y:y+h, x:x+w]

imwrite("foreground.png", foreground)

This method is fast and simple. If you find you have some white holes in your background which enlarge the bounding box, try applying an erosion operator.

FWIW I very much doubt you would get results like this as predictably or reliably using NNs.

like image 197
gavinb Avatar answered May 13 '23 05:05

gavinb