在OPEN CV PYTHON中识别颜色检测中的圆圈

时间:2018-06-08 10:38:55

标签: python opencv

我这里有一个代码可以检测激光,但我在不同光线条件下遇到问题。所以我想我可以解决它,如果我添加一个代码,检查该光是否是一个圆。

问题是我不知道如何在这里应用它。这是激光在面罩中的样子。

我希望你能帮我解决我的代码。

这是我的代码:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) convert from bgr to hsv color space

    lower = np.array([0,0,255]) #range of laser light
    upper = np.array([255, 255, 255])

    mask = cv2.inRange(hsv, lower, upper) 
    maskcopy = mask.copy()

    circles = cv2.HoughCircles(maskcopy, cv2.HOUGH_GRADIENT, 1, 500,
                      param1 = 20, param2 = 10,
                      minRadius = 1, maxRadius = 3)
    _,cont,_ = cv2.findContours(maskcopy, cv2.RETR_LIST,
                            cv2.CHAIN_APPROX_SIMPLE)
    if circles is not None:
        circles = np.round(circles[0,:]).astype('int')

        for(x,y,r) in circles:
            cv2.circle(frame, (x,y), r, (0,255,0),4)

    cv2.imshow('mask', mask)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

我尝试了类似的东西,对我来说最好的解决方案是:

(我将您的图像保存到我的硬盘并制作了示例代码)

import cv2
import math

img = cv2.imread('laser.jpg')
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray_image,100,255,cv2.THRESH_BINARY)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
area = sorted(contours, key=cv2.contourArea, reverse=True)
contour = area[0]
(x,y),radius = cv2.minEnclosingCircle(contour)
radius = int(radius)
area = cv2.contourArea(contour)
circ = 4*area/(math.pi*(radius*2)**2)
cv2.drawContours(img, [contour], 0, (0,255,0), 2)
cv2.imshow('img', img)
print(circ)

所以我的想法是用cv2.findContours(激光点)找到你的轮廓并用它包围圆圈,这样你就可以得到半径,然后得到轮廓为cv2.contourArea的区域并检查它的圆形度使用公式circ = 4*area/(math.pi*(radius*2)**2)。完美的citrcle将返回1的结果。它越是0,你的轮廓越少“圆周”(在下面的图片中)。希望它有所帮助!

enter image description here

enter image description here

所以你的代码应该是这样的,它不会返回任何错误(尝试过它并且有效)

import cv2
import numpy as np
import math

cap = cv2.VideoCapture(0)

while True:
    try:
        ret, frame = cap.read()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #convert from bgr to hsv color space

        lower = np.array([0,0,255]) #range of laser light
        upper = np.array([255, 255, 255])

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

        im2, contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        area = sorted(contours, key=cv2.contourArea, reverse=True)
        contour = area[0]
        (x,y),radius = cv2.minEnclosingCircle(contour)
        radius = int(radius)
        area = cv2.contourArea(contour)
        circ = 4*area/(math.pi*(radius*2)**2)
        print(circ)
    except:
        pass

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

cap.release()
cv2.destroyAllWindows()

答案 1 :(得分:0)

我提出了一种采用不同方法的解决方案。

我的想法是创建一个圆圈,其中心位于蒙版白色区域的中心,半径等于蒙版白色区域宽度的一半。然后我检查这个圆圈与面具有多相似。

以下是代码:

white = np.where(mask>250) # you can also make it == 255
white = np.asarray(white)
minx = min(white[0])
maxx = max(white[0])
miny = min(white[1])
maxy = max(white[1])
radius = int((maxx-minx)/2)
cx = minx + radius
cy = miny + radius
black = mask.copy()
black[:,:]=0
cv2.circle(black, (cy,cx), radius, (255,255,255),-1)
diff = cv2.bitwise_xor(black, mask)
diffPercentage = len(diff>0)/diff.size
print (diffPercentage)

然后你必须拿出百分比阈值"类似"对你来说足够了。

上面的代码测试了从磁盘读取掩码,但视频只是一系列图像。没有您的网络摄像头输入我无法使用视频测试代码,但它应该像这样工作:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower = np.array([0,0,255]) #range of laser light
    upper = np.array([255, 255, 255])

    mask = cv2.inRange(hsv, lower, upper) 
    white = np.where(mask>250) # you can also make it == 255
    white = np.asarray(white)
    minx = min(white[0])
    maxx = max(white[0])
    miny = min(white[1])
    maxy = max(white[1])
    radius = int((maxx-minx)/2)
    cx = minx + radius
    cy = miny + radius
    black = mask.copy()
    black[:,:]=0
    cv2.circle(black, (cy,cx), radius, (255,255,255),-1)
    diff = cv2.bitwise_xor(black, mask)
    diffPercentage = len(diff>0)/diff.size
    print (diffPercentage)

    cv2.imshow('mask', mask)
    cvw.imshow('diff', diff)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()