使用OpenCV查找亮点

时间:2017-04-06 15:51:46

标签: python opencv

我想用opencv圈出所有光点。想法是用网络摄像头找到光点,所以如果我在场景中有3个LED,我会喜欢圈出它们。但我不知道如何圈选/过滤它们。到目前为止,这是我的代码:

import cv2
import numpy as np
import imutils

cap = cv2.VideoCapture(0)
while True:
    _, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (11,11), 0)


    (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(blur)


    hi, threshold = cv2.threshold(blur, maxVal-20, 230, cv2.THRESH_BINARY)
    thr = threshold.copy()


    cv2.resize(thr, (300,300))


    edged = cv2.Canny(threshold, 50, 150)
    _, lightcontours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)





    for (i, c) in enumerate(lightcontours):
        points = []
        (x, y, w, h) = cv2.boundingRect(c)
        ((cX, cY), radius) = cv2.minEnclosingCircle(c)
        cv2.circle(frame, (int(cX), int(cY)), int(radius),
            (0, 0, 255), 3)
        points.append([[int(cX), int(cY)]])
        print points
        cv2.putText(frame, "#{}".format(i + 1), (x, y - 15),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)


    cv2.imshow('light', thr)
    cv2.imshow('frame', frame)
    cv2.imshow('edges', edged)
    cv2.waitKey(4)
    key = cv2.waitKey(5) & 0xFF
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

那么我该怎么做呢?我找到了一些来源,他们在图片中圈出了光点,但没有设法让它们适应流媒体。有什么想法吗?

祝你好运

更新: 我更新了我的代码以显示实际状态。

0 个答案:

没有答案
相关问题