Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding first non-zero value in an image

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
like image 224
NickB Avatar asked Dec 21 '25 21:12

NickB


1 Answers

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:

like image 194
Dan Mašek Avatar answered Dec 24 '25 11:12

Dan Mašek