I'm pretty new to Python and I would like to locate the extremes of a binary image. There's a white shape in the middle of a black background and I would like to locate the top, bottom, left and right enclosing rectangle.
My way of doing this is by finding the first non-zero pixels in all directions.
My function is goes as this, but it only works on the Y axis. How can I manage to pass through the X axis?
def first_non_zero(img):
width = img.shape[1]
height = img.shape[0]
idx = 0
result = 0
for j in range(0, height):
idx = np.argmax(img[j])
if idx > 0:
result = j
break
return result
I'd simply use numpy.nonzero and then find minimum and maximum for each axis.
Script:
import cv2
import numpy as np
img = cv2.imread('blob_in_the_middle.png', cv2.IMREAD_GRAYSCALE)
positions = np.nonzero(img)
top = positions[0].min()
bottom = positions[0].max()
left = positions[1].min()
right = positions[1].max()
output = cv2.rectangle(cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
, (left, top), (right, bottom), (0,255,0), 1)
cv2.imwrite('blob_with_bounds.png', output)
Sample input:

Sample output:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With