头歌 python 透视矫正及尺寸测量

import cv2
import numpy as np

### begin ###
import utils
### end ###

def fnJudge(fnIn, fnOut):
    #WW begin ###
    img = utils.fnPerspectiveCorrection(fnIn)  # 使用utils里面的fnPerspectiveCorrection,对fnIn 做透视矫正
    ### end ###
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换为灰度

    circles = cv2.HoughCircles(      # 查找 圆
        imgGray, cv2.HOUGH_GRADIENT, 1, minDist=60,
        param1=80, param2=30, minRadius=5, maxRadius=500)

    circles = np.uint16(np.around(circles))

    for idx, i in enumerate(circles[0,:]):
        # 画轮廓
        cv2.circle(img,(i[0],i[1]), i[2], (255,255,255), 2)
        # 画圆心
        cv2.circle(img,(i[0],i[1]), 2, (255,255,255), 3)
        ### begin ###
        # 计算半径大小(单位:mm)
        r = i[2] * 210 /img.shape[0] * 4.1

        # 这里补全

        # 在矫正之后的图中,标注半径数值,保留到小数点后2位
        cv2.putText(img, str(round(r/10,2)),(i[1],i[0]) , cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
        r = int(round(r*80/119))

        # 在矫正之后的图中,标注表示半径的箭头
        cv2.arrowedLine(img, (i[1],i[0]), (i[1],i[0] + r),
                (255, 255, 255),
                thickness=3, line_type=8, shift=0, tipLength=0.2
        )

        print(r,end='') # 输出直径

        ### end ###

    cv2.imwrite(fnOut, img)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值