Thanks Tim for helping out! I've ended up reiterating your solution to something similar. Just in case anyone would be interested, I will also post it here. (Ended up accessing an ndarray image by [Y, X] coordinates. Did use PixelLens Pycharm extension to debug how image arrays look like)
The code:
def find_scale_box_edges(image: np.ndarray):
step = 5
def find_last_black_pixel(image: np.ndarray, start, step):
_x = start[0]
_y = start[1]
if (image[_y, _x][:3] != [0,0,0]).all():
raise Exception
while _x > 0 and _x < image.shape[1]:
while _y > 0 and _y < image.shape[0]:
if (image[_y + step[1], _x + step[0]][:3] != [0,0,0]).any():
return [_x, _y]
else:
_x = _x + step[0]
_y = _y + step[1]
raise Exception
for x in range(image.shape[1]-step-1, step, -50):
for y in range(image.shape[0]):
if (image[y, x][:3] == [0, 0, 0]).any():
# check N horizontal nearby pixels to see if its truly a line.
upper_border_test = image[y:y+2, x-step:x+step][:,:,:3]
if (upper_border_test[:, 0] == [[0, 0, 0], [255, 255, 255]]).all():
top_left_corner = find_last_black_pixel(image, [x,y], [-1, 0])
top_right = find_last_black_pixel(image, [x, y], [1, 0])
bottom_right_corner = find_last_black_pixel(image, top_right, [0, 1])
# scale_box_image = image[top_left_corner[1]:bottom_right_corner[1],
# top_left_corner[0]:bottom_right_corner[0]]
return [top_left_corner, bottom_right_corner]
return [None, None]