Commit 173a8fca authored by Basnayake N.S.N.'s avatar Basnayake N.S.N.

Upload New File

parent 235941bd
import cv2
import numpy as np
rois = [] # This will store top-left and bottom-right points of rectangles
current_roi = [] # Temporary storage for current rectangle being drawn
def draw_roi(event, x, y, flags, param):
global current_roi, img_temp
if event == cv2.EVENT_LBUTTONDOWN:
current_roi = [(x, y)]
elif event == cv2.EVENT_LBUTTONUP:
current_roi.append((x, y))
cv2.rectangle(img_temp, current_roi[0], current_roi[1], (0, 255, 0), 2)
rois.append(current_roi)
cv2.imshow("Select ROIs", img_temp)
# Load the image
img = cv2.imread('test15.jpg')
img_temp = img.copy()
cv2.namedWindow("Select ROIs")
cv2.setMouseCallback("Select ROIs", draw_roi)
while True:
cv2.imshow("Select ROIs", img_temp)
key = cv2.waitKey(1)
# If 'c' key is pressed, break out of loop to compute
if key == ord('c'):
break
# If 'ESC' key is pressed, exit
if key == 27:
cv2.destroyAllWindows()
exit()
# Now, compute the combined HSV range of all selected ROIs
h_min, h_max = [], []
s_min, s_max = [], []
v_min, v_max = [], []
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
for roi in rois:
roi_data = img_hsv[roi[0][1]:roi[1][1], roi[0][0]:roi[1][0]]
h, s, v = cv2.split(roi_data)
h_min.append(h.min())
h_max.append(h.max())
s_min.append(s.min())
s_max.append(s.max())
v_min.append(v.min())
v_max.append(v.max())
print(f"H range: ({min(h_min)}, {max(h_max)})")
print(f"S range: ({min(s_min)}, {max(s_max)})")
print(f"V range: ({min(v_min)}, {max(v_max)})")
cv2.destroyAllWindows()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment