# Detect circles
import cv2
from matplotlib import pyplot as plt
%matplotlib inline
cap = cv2.VideoCapture(0)
window_name='camera'
cv2.namedWindow(window_name,cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
while (True):
ret, frame = cap.read()
# frame = cv2.Canny(frame, 100,100)
img = cv2.medianBlur(frame,5)
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow(window_name,img)
# out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # btw, you need to click the screen first. And then
# press q to quit
break
cap.release()
# out.release()
cv2.destroyAllWindows()
# Detect lines
import cv2
from matplotlib import pyplot as plt
%matplotlib inline
cap = cv2.VideoCapture(0)
window_name='camera'
cv2.namedWindow(window_name,cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name,cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
while (True):
ret, frame = cap.read()
edges = cv2.Canny(frame, 100,100)
# img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2)
lines = cv2.HoughLinesP(edges,1,np.pi/180,threshold=30,minLineLength=50,maxLineGap=30)
if lines is not None:
lines = np.uint16(np.around(lines))
for i in lines[0,:]:
# draw the outer circle
cv2.line(frame,(i[0],i[1]),(i[2],i[3]),(0,255,0),2)
# draw the center of the circle
cv2.imshow(window_name,frame)
# out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # btw, you need to click the screen first. And then
# press q to quit
break
cap.release()
# out.release()
cv2.destroyAllWindows()
# hough circle explain
img=np.zeros((100,100),dtype=np.uint16)
cv2.circle(img,(20,30),10,1,1)
cv2.circle(img,(50,60),10,1,1)
plt.imshow(img.transpose())
<matplotlib.image.AxesImage at 0x7f4a97a65710>
r=10
himg=np.zeros_like(img)
count=0
for x,y in np.argwhere(img>0):
timg=np.zeros_like(himg)
cv2.circle(timg,(x,y),r,1,1)
himg=himg+timg
count+=1
if count==100:
break
plt.imshow(himg)
<matplotlib.image.AxesImage at 0x7f4aa8073fd0>
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x,y=np.meshgrid(range(himg.shape[0]),range(himg.shape[1]))
ax.plot_surface(x,y,himg)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7f4a97dabdd8>
#obsolete
import numpy as np
def myhoughline(edges):
degs=np.array(range(360))
img=np.int32(np.zeros((len(degs),int(np.ceil(np.sqrt(edges.shape[0]**2+edges.shape[1]**2))))))
# not complete
return img
# too slow
def myhoughcircle(edges,r):
img=np.zeros_like(edges,dtype=np.int32)
for x,y in np.argwhere(edges > 0):
timg=cv2.circle(img,(x,y),r,1,1)
img=timg+img
return img