Python,平面图片转极值图片相互转换

发布时间 2023-09-22 14:27:22作者: hotzhml

Hough圆检测获取瓶口位置和大小

'''hough圆变换'''
cimg = cv2.cvtColor(bottle,cv2.COLOR_GRAY2BGR)  # 转换成彩色图
circles = cv2.HoughCircles(median,cv2.HOUGH_GRADIENT,1,100,
                         param1=100,param2=60,minRadius=150,maxRadius=160)  # Hough圆检测
circles = np.uint16(np.around(circles))
#print(circles)
for i in circles[0,:]:    # 遍历circles,i为列表,i中包含多个列表,列表为[x,y,r]圆心坐标和半径
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
#cv_show( 'cimg',cimg)

极坐标变化与反变换

为了消除瓶口外侧部分带来的干扰。进行极坐标变换,将极坐标中心设为圆心,极半径为圆半径;进行反变化,将瓶口放在原图大小的原位置

'''极坐标变换'''
polarImg = cv2.warpPolar(bottle,(300,900),center,radius,
                         cv2.INTER_LINEAR + cv2.WARP_POLAR_LINEAR)
#cv_show('polarImg',polarImg)
'''反变换'''
b_bottle=cv2.warpPolar(polarImg,psp, center,radius,
                       cv2.INTER_LINEAR + cv2.WARP_POLAR_LINEAR + cv2.WARP_INVERSE_MAP)
#cv_show('b_bottle',b_bottle)