我想要发生的是消除噪音并且只识别圆圈。到目前为止我有这些代码:
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])
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()
它的作用是检测最亮的光并检查它是多么圆。我想要发生的是消除噪音并仅检测圆圈。我希望你能帮我解决我的代码问题。
这只是我的程序检测最亮像素的一个例子。这是原始图片:
答案 0 :(得分:3)
您可以尝试通过过滤不在您的尺寸范围内的其他轮廓来选择轮廓。你应该明白,我也刚开始学习python和opencv,并且有可能存在大量更好的方法。代码应该是这样的:
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
lower = np.array([0,0,255])
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)
for number in range(0, len(area)):
cnts = area[number]
if 40 < len(cnts) < 80:
contour = area[number]
break
(x,y),radius = cv2.minEnclosingCircle(contour)
radius = int(radius)
area2 = cv2.contourArea(contour)
circ = 4*area2/(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()
你必须改变for循环的范围,以便它适合你的目的。
更新
也许更好......你可以消除其他轮廓(带有循环标准的噪音):
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
lower = np.array([0,0,255])
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)
for number in range(0, len(area)):
cnts = area[number]
if 40 < len(cnts) < 80:
contour = area[number]
(x,y),radius = cv2.minEnclosingCircle(contour)
radius = int(radius)
area2 = cv2.contourArea(contour)
circ = 4*area2/(math.pi*(radius*2)**2)
if 0.8 < circ < 1.5:
rect = cv2.boundingRect(contour)
x,y,w,h = rect
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(frame,'Laser point detected',(x+w+10,y+h),0,0.5,(0,255,0))
print(circ)
break
except:
pass
cv2.imshow('mask', mask)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
答案 1 :(得分:1)
您可以使用Hough circle transform执行此操作。
这是this图片的解决方案:
import cv2
import numpy as np
img = cv2.imread('stack.jpg',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
black = np.zeros_like(cimg)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(black,(i[0],i[1]),i[2],(0,255,0),1)
# draw the center of the circle
cv2.circle(black,(i[0],i[1]),2,(0,0,255),1)
cv2.imshow('detected circles',black)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出: