Skip to main content
added 9182 characters in body
Source Link
user15852742
user15852742

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

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

deleted 26 characters in body
Source Link
Peter O.
  • 33.1k
  • 14
  • 86
  • 97

I have a bitwise mask of a object from which iI want to detect its highest point how. How can iI do this using opencv pythonOpenCV Python?

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

How can i achieve this ?

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

How can i achieve this ?

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
added 28 characters in body
Source Link
user15852742
user15852742

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

How can i achieve this ?

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

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

How can i achieve this ?

Source Link
user15852742
user15852742
Loading