OpenCV自学笔记10:视频中分割出圆形

视频中分割出圆形

import cv2
import numpy as np

cap = cv2.VideoCapture('video/video.avi')

low_range = np.array([0, 123, 100])
high_range = np.array([5, 255, 255])

lastX = 0
lastY = 0
deltaX = 0
deltaY = 0

while True:
    ret, frame = cap.read()

    # change bgr to hsv
    hue_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # set threshold, the white area is the ball
    threshold_img = cv2.inRange(hue_image, low_range, high_range)

    # find contours to get center of the ball
    image, contour, hierarchy = cv2.findContours(threshold_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    if contour:
        circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT, 1, 100, \
                                   param1=20, param2=10, minRadius=10, maxRadius=20)
        if circles is not None:
            circle = circles[0][0]
            x = circle[0]
            y = circle[1]
            radius = circle[2]
            center = (x, y)

            deltaX = int(x) - lastX
            deltaY = int(y) - lastY
            lastX = int(x)
            lastY = int(y)

            if radius > 5 and lastX != 0 and lastY != 0 and np.abs(deltaX - lastX) > 5 and np.abs(deltaY - lastY) > 5 :
                img = cv2.circle(frame, center, radius, (0, 255, 0), 2)
                img = cv2.line(frame, (lastX, lastY), (lastX + deltaX, lastY + deltaY), (0, 255, 0), 2)

    cv2.imshow('video2', frame)
    if cv2.waitKey(100) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值