选择正确的HSV边界和掩盖
这是我的代码如下。 我想检测实时形状,但正如您所看到的,检测它存在问题。
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()
首先,形状是矩形,颜色是白色。我用轨迹栏尝试了很多 HSV 边界。当我得到这些值时,会出现噪音。我怎样才能做得更好以及如何修复它。
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()
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论