python+opencv学习之--提取角点

#实现提取轮廓的功能,并且绘制出轮廓的重心
import numpy as np
import cv2
from pylab import imshow
from pylab import array
from pylab import plot
from pylab import title
#读取图片
img = cv2.imread('timg.jpeg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#转灰度值
#二值化,canny检测
binaryImg = cv2.Canny(img,50,200)
#寻找轮廓
contours,contourstype=cv2.findContours(binaryImg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#查看轮廓数量
# print (len(contours))
# print (type(contours))
# print (type(contourstype))
# print ((contourstype))
temp = np.ones(binaryImg.shape,np.uint8)*255
for each in contours:
    M = cv2.moments(each)#轮廓的矩
    if (M['m00']!=0):
        cx = int(M['m10']/M['m00'])#计算重心
        cy = int(M['m01']/M['m00'])
        pos=(cx,cy)
        print(pos)
    cv2.circle(temp,pos, 4, (0, 0, 0), 4)#绘制轮廓的中心点
#画出轮廓:temp是白色幕布,contours是轮廓,-1表示全画,然后是颜色,厚度
cv2.drawContours(temp,contours,-1,(0,255,0),3)
cv2.imshow("contours",temp)
cv2.waitKey(0)
cv2.destroyAllWindows()
 

你可能感兴趣的:(图像处理)