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()
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kobesdu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值