OpenCV(Python)基礎 | 跟踪(指定/遮罩)視頻(圖片/相片)中指定顏色 ~ inRange

OpenCV(Python)基礎 | 跟踪(指定/遮罩)視頻(圖片/相片)中指定顏色 ~ inRange

OpenCV(Python)基礎 | 跟踪(指定/遮罩)視頻(圖片/相片)中指定顏色 ~ inRange


資料來源: https://mp.weixin.qq.com/s?__biz=MzIwMDU3MDkzNg==&mid=2651312492&idx=1&sn=1a093dc53ee98940b39a7240de401324&chksm=8d08503bba7fd92dadf2d5d2eb57fb3ae35ffb157af450ff849a6e96fa7968d90a7f470a5132&scene=126&sessionid=1607388989&key=e420ad4ff565d51fbc143339165afd922b87f1bcd194e10ff1d91676a9d42592ed36a2c2d7ab8aef114b816ccd7acc2d8374d9437b32cadfe3b6f963fdc30f91c14fbc067ec1e703aa19490353f7dccf1794271a2fe974298065bf88dcaedb0a778bf6c280d6eed99f829a34f32ae5cf37ba6eb1b19a7eb9a1b3a127213d7c4d&ascene=1&uin=MjIwODk2NDgxNw%3D%3D&devicetype=Windows+10+x64&version=6300002f&lang=zh_TW&exportkey=AmHHv3ajukqYlxGP9h8yslw%3D&pass_ticket=0gGYf2c03M7ejUVfmjrIHgJkhrMGS61jg%2FgyZlOfGhQCljXh8cE4NsI%2BQGQbG5sm&wx_header=0

https://www.bilibili.com/video/BV1QW411F7e7?p=1



API: mask = cv2.inRange(hsv, lower, upper)

hsv:rgb圖像轉換為hsv格式的圖像

lower:圖像中低於這個lower的值,圖像值變為0,即變為黑

upper:圖像中高於這個upper的值,圖像值變為0,即變為黑

在lower~upper之間的值變成255,即變為白

inRange用於實現圖片的二值化


code:

# 可从视频中跟踪指定颜色,指定颜色置为白,其余置为黑
def extract_object_demo():
    capture=cv.VideoCapture("./images/vtest.avi")
    while(True):
        ret,frame=capture.read()
        if ret ==False:
            break
        hsv=cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        lower_hsv = np.array([35, 43, 46])  # 对应的绿色的hsv中h,s,v的最小值
        upper_hsv = np.array([77, 255, 255])  #对应的绿色的hsv中的h,s,v最大值
        mask=cv.inRange(hsv,lowerb=lower_hsv,upperb=upper_hsv)
        cv.imshow("video",frame)
        cv.imshow("mask",mask)
        c=cv.waitKey(40)
        if c==27:
            break

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *