Commit 562e5d33 authored by Inès EL HADRI's avatar Inès EL HADRI 💤

Tune HSV in mask_tuner

parent 164186b3
import cv2
import numpy as np
def souris(event, x, y, flags, param):
global lo, hi, color, hsv_px
if event == cv2.EVENT_MOUSEMOVE:
# Conversion des trois couleurs RGB sous la souris en HSV
px = frame[y,x]
px_array = np.uint8([[px]])
hsv_px = cv2.cvtColor(px_array,cv2.COLOR_BGR2HSV)
if event==cv2.EVENT_MBUTTONDBLCLK:
color=image[y, x][0]
if event==cv2.EVENT_LBUTTONDOWN:
if color>5:
color-=1
if event==cv2.EVENT_RBUTTONDOWN:
if color<250:
color+=1
# lo[1]=color-10
# hi[1]=color+10
color=100
lo=np.array([58-20, 50, 20])
hi=np.array([58+20, 255,255])
color_info=(0, 0, 255)
cap=cv2.VideoCapture(0)
cv2.namedWindow('Camera')
cv2.setMouseCallback('Camera', souris)
hsv_px = [0,0,0]
# Creating morphological kernel
kernel = np.ones((3, 3), np.uint8)
while True:
ret, frame=cap.read()
image=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask=cv2.inRange(image, lo, hi)
mask=cv2.erode(mask, kernel, iterations=1)
mask=cv2.dilate(mask, kernel, iterations=1)
image2=cv2.bitwise_and(frame, frame, mask= mask)
cv2.putText(frame, "Couleur: {:d}".format(color), (10, 30), cv2.FONT_HERSHEY_DUPLEX, 1, color_info, 1, cv2.LINE_AA)
# Affichage des composantes HSV sous la souris sur l'image
pixel_hsv = " ".join(str(values) for values in hsv_px)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "px HSV: "+pixel_hsv, (10, 260),
font, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.imshow('Camera', frame)
cv2.imshow('image2', image2)
cv2.imshow('Mask', mask)
if cv2.waitKey(1)&0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()
\ No newline at end of file
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