资源简介

本例子是使用python-opencv的支持库实现的表针读数的识别算法

资源截图

代码片段和文件信息

import cv2
import numpy as np
import time
import NUMBER
import screenshot


def dist_2_pts(x1 y1 x2 y2):
    return np.sqrt((x2 - x1)**2 + (y2 - y1)**2)

def avg_circles(circles b):
    avg_x=0
    avg_y=0
    avg_r=0
    for i in range(b):

        avg_x = avg_x + circles[0][i][0]
        avg_y = avg_y + circles[0][i][1]
        avg_r = avg_r + circles[0][i][2]
    avg_x = int(avg_x/ (b))
    avg_y = int(avg_y/(b))
    avg_r = int(avg_r/(b))
    return avg_x avg_y avg_r

def avg_circles_three(circles b):
    number=[1]
    avg_x = [000]
    avg_y = [000]
    avg_r = [000]
    initial=[000]
    for i in range(b):
        if (initial[0] == 1 ):
            if(dist_2_pts(avg_x[0]avg_y[0]circles[0][i][0]circles[0][i][1]) >= (avg_r[0]+circles[0][i][2])):
                if(initial[1] == 1):
                    if (dist_2_pts(avg_x[1] avg_y[1] circles[0][i][0] circles[0][i][1]) >= (avg_r[1] + circles[0][i][2])):
                        if (initial[2] == 1):
                            avg_x[2] = (avg_x[2] + circles[0][i][0])/2
                            avg_y[2] = (avg_y[2] + circles[0][i][1])/2
                            avg_r[2] = (avg_r[2] + circles[0][i][2])/2
                        else:
                            initial[2] = 1
                            number[0]=number[0]+1
                            avg_x[2] =  circles[0][i][0]
                            avg_y[2] =  circles[0][i][1]
                            avg_r[2] =  circles[0][i][2]
                    else:
                        avg_x[1] = (avg_x[1] + circles[0][i][0])/2
                        avg_y[1] = (avg_y[1] + circles[0][i][1])/2
                        avg_r[1] = (avg_r[1] + circles[0][i][2])/2
                else:
                    initial[1] = 1
                    avg_x[1] = circles[0][i][0]
                    avg_y[1] = circles[0][i][1]
                    avg_r[1] = circles[0][i][2]
                    number[0] = number[0] + 1

            else:
                avg_x[0] = (avg_x[0] + circles[0][i][0])/2
                avg_y[0] = (avg_y[0] + circles[0][i][1])/2
                avg_r[0] = (avg_r[0] + circles[0][i][2])/2
        else:
            avg_x[0] = circles[0][i][0]
            avg_y[0] = circles[0][i][1]
            avg_r[0] = circles[0][i][2]
            initial[0]=1
    for i in range(3):
        avg_x[i] = int(avg_x[i])
        avg_y[i] = int(avg_y[i])
        avg_r[i] = int(avg_r[i])
    return avg_x avg_y avg_r number

def calibrate_gauge(gauge_number file_type):
    img = cv2.imread(‘gauge-%s.%s‘ %(gauge_number file_type))
    height width = img.shape[:2]
    gray = cv2.cvtColor(img cv2.COLOR_BGR2GRAY)  #convert to gray


    #detect circles
    circles = cv2.HoughCircles(gray cv2.HOUGH_GRADIENT 1 20 np.array([]) 100 80 int(height*0.1) int(height*0.48))
    a b c = circles.shape
    xyr = avg_circles(circles b)

    ‘‘‘
    Circles=avg_circles_three(circlesb)
    x=Circles[0][0]
    y=Circles[1][0]
    r=

评论

共有 条评论