Python版
- 直线检测
使用的图
import cv2
import numpy as np
img=cv2.imread('python.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges=cv2.Canny(gray,50,120)#注意canny的c是大写啊!!!!!!!
minLineLength=2
MaxLineGap=5
lines=cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,MaxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow("edges",edges)
cv2.imshow('lines',img)
cv2.waitKey()
cv2.destroyAllWindows()
- 圆检测
import cv2
import numpy as np
python=cv2.imread('python.jpg')
gray_img=cv2.cvtColor(python,cv2.COLOR_BGR2GRAY)
img=cv2.medianBlur(gray_img,5)
cimag=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles=cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,param1=100,param2=30,minRadius=0,maxRadius=0)
circles=np.uint16(np.around(circles))#uint无符号整型
for i in circles[0,:]:
#画外部圆
cv2.circle(python,(i[0],i[1]),i[2],(0,255,0),2)
#画圆心
cv2.circle(python, (i[0], i[1]), 2, (0, 0, 255), 3)
cv2.imwrite('python4.jpg',python)
cv2.imshow('circle',python)
cv2.waitKey()
cv2.destroyAllWindows()