I am trying to find a highest point of an object in a mask of bitwise. I researched and found this question i did exactly as the answer said but the results were just (0,0) which are wrong
I am just trying in the same image as the question
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 


np.where(has_white)to find the coordinates of white pixels, thennp.argminon the y's.cv2.morphologyExopening/closing to your b/w image.