Python3+OpenCV3+Pycharm编程:黑白相机乒乓球识别V1.0

# -*- coding: utf-8 -*-
# By:iloveluoluo
# 2019.3.25
import cv2 as cv
# import numpy as np

capture = cv.VideoCapture(1)  # 打开摄像头
while True:
    ret, frame = capture.read()  # 返回值,每一帧图像
    # frame = cv.flip(frame, 1)  # 镜像变换左右,上下1或-1

    dst = cv.medianBlur(frame, 5)  # 中值滤波
    cv.imshow("median video", dst)

    # hsv = cv.cvtColor(dst, cv.COLOR_BGR2HSV)
    # cv.imshow("hsv", hsv)
    # lower_hsv = np.array([0, 0, 221])
    # upper_hsv = np.array([180, 30, 255])
    # extract_white = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)  # inRange API进行颜色提取
    # cv.imshow("extract_red demo", extract_white)

    canny = cv.Canny(dst, 40, 80)
    cv.imshow('canny video', canny)

    # 霍夫变换圆检测
    circles = cv.HoughCircles(canny, cv.HOUGH_GRADIENT, 1, 50, param1=80, param2=30, minRadius=60, maxRadius=150)
    # 输出返回值,方便查看类型
    # print(circles)

    # 输出检测到圆的个数
    print("Sum:%d" % len(circles[0]))

    # 根据检测到圆的信息,画出每一个圆
    for circle in circles[0]:
        # 坐标行列(就是圆心)
        x = int(circle[0])
        y = int(circle[1])
        # 半径
        r = int(circle[2])
        # 在原图用指定颜色圈出圆,参数设定为int所以圈画存在误差
        img = cv.circle(frame, (x, y), r, (0, 0, 255), 1, 8, 0)
        # 圆的基本信息
        print("x=%d, y=%d, r=%d" % (x, y, r))
    cv.imshow("video", img)

    key = cv.waitKey(1)
    if key == 27:  # Esc退出
        break

cv.destroyAllWindows()

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值