使用Python+OpenCV进行图像处理之入门教程
小白学视觉
共 4284字,需浏览 9分钟
·
2020-12-27 19:01
点击上方“小白学视觉”,选择加"星标"或“置顶”
重磅干货,第一时间送达
简介
什么是OpenCV
实现我们的第一个滤波器
检测逻辑
什么是HSV
色调:根据光谱,物体的颜色可分为红、蓝、绿、黄四种颜色。 饱和度:它定义了颜色的强度。 值:定义颜色的亮度。
我们来看看代码
import cv2
import numpy as np
import cv2
import numpy as np
def nothing():
pass
cap=cv2.VideoCapture(0)
while(1):
_, img = cap.read()
k=cv2.waitkey(1)&0xFF
if k==27:
break
cap.release()
cv2.destroyAllWindows()
cv2.namedWindow( )
import cv2
import numpy as np
def nothing(x):
pass
cap=cv2.VideoCapture(0)
cv2.namedWindow('tracking')
cv2.createTrackbar('LH','tracking',0,255,nothing)
cv2.createTrackbar('LS','tracking',0,255,nothing)
cv2.createTrackbar('LV','tracking',0,255,nothing)
cv2.createTrackbar('UH','tracking',255,255,nothing)
cv2.createTrackbar('US','tracking',255,255,nothing)
cv2.createTrackbar('UV','tracking',255,255,nothing)
= cv2.cvtColor( ,cv2.COLOR_BGR2HSV)
= np.array( )
= cv2.inRange(hav, l_b, u_b)
res = cv2.bitwise_and(, , mask = )
import cv2
import numpy as np
def nothing(x):
pass
cap=cv2.VideoCapture(0)
cv2.namedWindow('tracking')
cv2.createTrackbar('LH','tracking',0,255,nothing)
cv2.createTrackbar('LS','tracking',0,255,nothing)
cv2.createTrackbar('LV','tracking',0,255,nothing)
cv2.createTrackbar('UH','tracking',255,255,nothing)
cv2.createTrackbar('US','tracking',255,255,nothing)
cv2.createTrackbar('UV','tracking',255,255,nothing)
while (1):
_, img=cap.read()
hav=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
l_h=cv2.getTrackbarPos('LH','tracking')
l_s=cv2.getTrackbarPos('LS', 'tracking')
l_v=cv2.getTrackbarPos('LV', 'tracking')
u_h=cv2.getTrackbarPos('UH', 'tracking')
u_s=cv2.getTrackbarPos('US', 'tracking')
u_v=cv2.getTrackbarPos('UV', 'tracking')
l_b=np.array([l_h,l_s,l_v])
u_b=np.array([u_h,u_s,u_v])
mask=cv2.inRange(hav, l_b, u_b)
res = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('image', img)
cv2.imshow('mask', mask)
cv2.imshow('res', res)
k=cv2.waitKey(1)&0xFF
if k==27:
break
cap.release()
cv2.destroyAllWindows()
cv2.imshow( , )
结论
参考引用
Implementing colour and shape-based object detection and tracking with OpenCV(https://hub.packtpub.com/implementing-color-and-shape-based-object-detection-and-tracking-with-opencv-and-cuda-tutorial/).
交流群
欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~
评论