0

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?

My Mask Expected Result
enter image description here enter image description here

Original Image: enter image description here

Here is the code for that

import cv2 import numpy as np def line(im, pt1, pt2, color, thickness): im = cv2.line(im, pt1, pt2, color=color, thickness=thickness, lineType=cv2.LINE_AA) return im def empty(a): pass path = 'images/m_1.jpg' global img img = cv2.imread(path) img = cv2.resize(img, (640, 480)) org_img = img.copy() copy_img = img.copy() img_blur = cv2.blur(img,(5,5)) imgHSV = cv2.cvtColor(img_blur, cv2.COLOR_BGR2HSV) h_min = 0 h_max = 179 s_min = 0 s_max = 255 v_min = 48 v_max = 166 lower = np.array([h_min, s_min, v_min]) upper = np.array([h_max, s_max, v_max]) mask = cv2.inRange(imgHSV, lower, upper) global imgResult imgResult = cv2.bitwise_and(img, img, mask=mask) imgResult = cv2.resize(imgResult, (640, 480)) cont, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) c = max(cont, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(c) cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) image = cv2.line(img, (int(w/2),y), (int(w/2),y+h), (0,0,255), 2) global cropped_img cropped_img = copy_img[y:y + h, x:x + w] draw_img = np.zeros((img.shape[0], img.shape[1], img.shape[2]), dtype="uint8") cv2.drawContours(draw_img,[c],-1,(255,255,255),thickness=-1) img_bitwise = cv2.bitwise_and(org_img, draw_img) margin = 90 draw_img = draw_img[margin:-margin] imgResult = imgResult[margin:-margin] img = img[margin:-margin] img_bitwise = img_bitwise[margin:-margin] print(draw_img.shape[:2]) has_white = print(np.any(draw_img, axis=1)) print(np.argmin(has_white)) result_i = np.argmin(has_white) print(np.argmin(img_bitwise[result_i,:])) cv2.imwrite('bit_img.png',img_bitwise) cv2.imshow("Track Images", imgResult) cv2.imshow("Result Images", img) cv2.imshow("Cropped Image", cropped_img) cv2.imshow("Draw Image", draw_img) cv2.imshow("bit_img", img_bitwise) k = cv2.waitKey(0) if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): # wait for 's' key to save and exit cv2.imwrite(r'C:\Users\Anush\PycharmProjects\WeldPoolAnalysis\resultImages\imgResult.png', img) cv2.imwrite(r'C:\Users\Anush\PycharmProjects\WeldPoolAnalysis\resultImages\imgCropped.png', cropped_img) cv2.destroyAllWindows() 

Output Of The Script

(300, 640) [[False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [False False False] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [ True True True] [False False False] [False False False] [False False False] [False False False]] 0 0 

use the original image as the main image in the following program and then you can debug the problem easily

3
  • 2
    with black = False and white = True, something like: np.argmin(np.any(mask, axis=1), axis=0) takes the highest row in the array that contains a white pixel (you might need to flip your array or change the axis based on how you defined your array) Commented May 11, 2021 at 7:28
  • That does not need special functions from OpenCV. Just find the first line that is not completely black. Commented May 11, 2021 at 7:29
  • @FrederikBode i didnt really understand what you meant could you please pass it on as a answer i would happly accept it Commented May 11, 2021 at 7:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

so how can we mark this as a line in a image and output is 0
i know how to draw a line but how can we with this this doesnt give any co-ordinate or anything
the last value 2 = result_i is the y coordinate, you can get the x coordinate by np.argmin(mask[result_i,:])(take the leftmost white pixel in that row)
Something seems to be wrong my x,y values are just 0,0
any idea why this happening ?
|