choosing the correct hsv boundaries and masking

109 Views Asked by At

Here my code below. I want to detect real time shapes but as you can see there is a problem to detect that.

import cv2
import numpy as np


def nothing(x):
    pass


cap = cv2.VideoCapture(0)
cv2.namedWindow('Settings')

cv2.createTrackbar('Lower-Hue','Settings',0,180,nothing)
cv2.createTrackbar('Lower-Satuation','Settings',0,255,nothing)
cv2.createTrackbar('Lower-Value','Settings',0,255,nothing)
cv2.createTrackbar('Upper-Hue','Settings',0,180,nothing)
cv2.createTrackbar('Upper-Saturation','Settings',0,255,nothing)
cv2.createTrackbar('Upper-Value','Settings',0,255,nothing)

font = cv2.FONT_HERSHEY_SIMPLEX

while True:
    ret,frame = cap.read()
    frame = cv2.flip(frame,1)
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    _, thresh = cv2.threshold(hsv, 240, 255, cv2.THRESH_BINARY)

    lh = cv2.getTrackbarPos('Lower-Hue','Settings')
    ls = cv2.getTrackbarPos('Lower-Saturation', 'Settings')
    lv = cv2.getTrackbarPos('Lower-Value', 'Settings')
    uh = cv2.getTrackbarPos('Upper-Hue', 'Settings')
    us = cv2.getTrackbarPos('Upper-Saturation', 'Settings')
    uv = cv2.getTrackbarPos('Upper-Value', 'Settings')

    lower_color = np.array([lh,ls,lv])
    upper_color = np.array([uh,us,uv])

    mask = cv2.inRange(hsv,lower_color,upper_color)
    kernel = np.ones((5,5),np.uint8) #maskeledikten sonra beyaz nesnelerde oluşan siyah noktaları yoketmek için yapıldı
    mask = cv2.erode(mask,kernel)

    contours,_ = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        area = cv2.contourArea(cnt)  #alan hesabı yaparak belli bir değerin üzerinde alan hesabı yaptırılır
        epsilon = 0.02*cv2.arcLength(cnt,True)
        approx = cv2.approxPolyDP(cnt,epsilon,True)

        x = approx.ravel()[0]
        y = approx.ravel()[1]

        if area >400:
            cv2.drawContours(frame,[approx],0,(0,0,0),5)
            if len(approx) == 3:
                cv2.putText(frame, 'Triangle', (x, y), font, 1, (0,0,0))
            if len(approx) == 4:
                cv2.putText(frame, 'Rectangle', (x, y), font, 1, (0,0,0))
            if len(approx) == 5:
                cv2.putText(frame, 'Pentagon', (x, y), font, 1, (0,0,0))
            if len(approx) == 6:
                cv2.putText(frame, 'Hexagon', (x, y), font, 1, (0,0,0))
            else:
                cv2.putText(frame, 'Ellips', (x, y), font, 1, (0,0,0))


    cv2.imshow('frame',frame)
    cv2.imshow('mask',mask)

    if cv2.waitKey(3) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

enter image description here

First of all the shape is rectangle and color is white. I tried lots of hsv boundaries with trackbar. When i get the values there is a noise. How can i do better masking and How can i fix it.

0

There are 0 best solutions below