Skip to content Skip to sidebar Skip to footer

Finding Highest Point In A Bitwise Mask In Opencv Python

I have a bitwise mask of a object from which I want to detect its highest point. How can I do this using OpenCV Python? Original Image: Here is the code for that import cv2 impor

Solution 1:

Let's say the image looks like this:

0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 1 1 0 0 0
0 1 1 1 1 1 1 0 0

the 1 is white, the 0 is black. let's say this numpy array is called mask. Let's print the shape:

import numpy as np
print(mask.shape)

should give you

(5,9) 

in this case, you should get (image_width, image_height)

Then we'll first check if any rows are either fully black, or there is some pixel that is white with np.any, which does this: 'for every row along axis, is there "any" value which is not 0? if yes return True for that row, otherwise return False for that row'. So:

has_white = print(np.any(mask, axis=1))

yields

array([False, False, True, True, True])

so a True/False value for every row.

Now we take the smallest value, as it is the highest row in your image:

np.argmin(has_white)

yields

2

which is the index from the top down counted

Post a Comment for "Finding Highest Point In A Bitwise Mask In Opencv Python"