def processKedu(zone, index): ver = projectVertical(zone) (h1, w1) = zone.shape newHorizon = np.zeros([h1, w1], np.uint8) for i in range(0, w1): for j in range(0, ver[i]): newHorizon[j, i] = 255 cv2.imwrite(dir_path + '/7_nums_kedu_' + str(index) + '.jpg', newHorizon) print('img', dir_path + '/7_nums_kedu_' + str(index) + '.jpg') maxtab, mintab = peakdetective.peakdet(ver, 3) # print(maxtab) k_res = list(maxtab[:, 1]) # print(res.index(max(res)),len(res)) k_pos = k_res.index(max(k_res)) k_len = len(k_res) # print('指针位置',k_res.index(max(k_res)), '总刻度线个数',len(k_res)) # print('position', k_res.index(max(k_res)) / (len(k_res) - 1) * 100) border1 = maxtab[0][0] border2 = maxtab[len(maxtab) - 1][0] pos = maxtab[k_pos][0] rate = float(pos - border1) / (border2 - border1) # print('比例计算:', rate) return k_pos, k_len, rate
def processKedu(arr, type_arr): # print(len(arr),type_arr) rate_arr = [] index_arr = [] for ind in range(len(arr)): kedu1, kedu2 = angle_corract(arr[ind]['kedu1'], arr[ind]['kedu2']) ver = projectVertical(kedu1) ver2 = projectVertical(cv2.subtract(kedu2, kedu1)) pos = np.mean(np.where(ver2 == np.max(ver2))) print(pos) (h1, w1) = kedu1.shape newblack = np.zeros([h1, w1], np.uint8) for i in range(0, w1): for j in range(0, ver[i]): newblack[j, i] = 255 cv2.imwrite('./pointer2/4_nums_kedu_' + str(1) + '.jpg', newblack) maxtab, mintab = peakdetective.peakdet(ver, 5) maxs = maxtab[:, 1] tabs = maxtab[:, 0] max_num = np.max(maxs) max_index = np.where((maxs < max_num + 10) & (maxs > max_num - 10))[0] tops = [] for m in max_index: tops.append(tabs[m]) _left = 0 _right = 0 pos_index = 0 # print(i) if type_arr[ind] == 0: tops.reverse() tops.append(tabs[0]) for t in range(len(tops)): if tops[t] <= pos: _left = tops[t - 1] _right = tops[t] pos_index = t - 1 break else: tops.append(tabs[-1]) for t in range(len(tops)): if tops[t] >= pos: _left = tops[t - 1] _right = tops[t] pos_index = t - 1 break print('节点数', len(tops), tops) rate = (pos - _left) / (_right - _left) print(rate, pos_index) rate_arr.append(rate) index_arr.append(pos_index) return rate_arr, index_arr
def split_area(src): # index=0 # _areas=[] # for src in src_arr: # src1 = src.copy() # src1 = cv2.dilate(src1,kernel8,iterations=2) # un,area,un,un = findContours(src1,src) # area = cv2.erode(area,kernel3) # area = cv2.dilate(area,kernel4) # ret, area = cv2.threshold(area, 127, 255, 0) # lines,un = getLineBorder(area,20,20) # print(lines) lines, un = getLineBorder(src, 10, 300) print(un) src_cp = src.copy() for i in lines: src_cp[i, :] = 255 cv2.imwrite('./pointer3/4_area' + str(0) + '.jpg', src_cp) # test_approx(src) nums = src[:lines[2], :] kedu = src[lines[1]:, :] cv2.imwrite('./pointer3/4_nums' + str(0) + '.jpg', nums) kedu = cv2.morphologyEx(kedu, cv2.MORPH_CLOSE, cross1, iterations=4) ret, kedu = cv2.threshold(kedu, 100, 255, 0) _, contours, hierarchy = cv2.findContours(kedu, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) c = sorted(contours, key=cv2.contourArea, reverse=True)[0] x, y, w, h = cv2.boundingRect(c) kedu = kedu[:, x:x + w] # lines1, un = getLineBorder(kedu, 10, 50) # for i in lines1: # kedu[i,:]=255 ver = projectVertical(kedu) maxtab, mintab = peakdetective.peakdet(ver, 5) maxs = maxtab[:, 1] tabs = maxtab[:, 0] max_pos = np.where(maxs == max(maxs))[0][0] print(tabs[max_pos]) cv2.imwrite('./pointer3/4_kedu' + str(0) + '.jpg', kedu)
def processSmallKedu(zone2polar, one_heart, numsShape, polar_, line_border_): polar = cv2.logPolar(zone2polar, (one_heart[0] * 8, one_heart[1] * 8), 600, cv2.WARP_FILL_OUTLIERS) polar = cv2.rotate(polar, cv2.ROTATE_90_COUNTERCLOCKWISE) cv2.imwrite('./v1/cut_polar_1.jpg', polar) unused, numsCanny_1, unused1 = findContours(cv2.dilate(polar_, kernel5, iterations=2), polar, offset1=40, offset2=30, big_index=0) numsCanny_1 = cv2.resize(numsCanny_1, (numsShape[1] * 4, numsShape[0] * 4)) cv2.imwrite('./v1/nums_canny1.jpg', numsCanny_1) kedu_1_pointer = numsCanny_1[line_border_[0]:line_border_[1] + 60, :] kedu_1_pointer = cv2.erode(kedu_1_pointer, kernel3) cv2.imwrite('./v1/nums_kedu_1_pointer.jpg', kedu_1_pointer) ver_ = projectVertical(kedu_1_pointer) (h1, w1) = kedu_1_pointer.shape newHorizon_ = np.zeros([h1, w1], np.uint8) for i in range(0, w1): for j in range(0, ver_[i]): newHorizon_[j, i] = 255 cv2.imwrite('./v1/nums_kedu_111.jpg', newHorizon_) maxtab, mintab = peakdetective.peakdet(ver_, 30) # from matplotlib.pyplot import plot, scatter, show # plot(ver) # scatter(np.array(maxtab)[:, 0], np.array(maxtab)[:, 1], color='blue') # scatter(np.array(mintab)[:, 0], np.array(mintab)[:, 1], color='red') # show() k_res = list(maxtab[:, 1]) # print(res.index(max(res)),len(res)) k_pos = k_res.index(max(k_res)) k_len = len(k_res) print('position', k_res.index(max(k_res)), '总刻度线数:', (len(k_res) - 1)) return k_pos, k_len
def processBigKedu(lineZone): border, ho = getLineBorder(lineZone, 20) lineZone = cv2.erode(lineZone, kernel3) numsCannyshape = lineZone.shape kedu = lineZone[border[2]:border[2] + 500, :] cv2.imwrite('./v1/nums_kedu.jpg', kedu) ver = projectVertical(kedu) (h1, w1) = kedu.shape newHorizon = np.zeros([h1, w1], np.uint8) for i in range(0, w1): for j in range(0, ver[i]): newHorizon[j, i] = 255 cv2.imwrite('./v1/nums_kedu_.jpg', newHorizon) maxtab, mintab = peakdetective.peakdet(ver, 3) k_res = list(maxtab[:, 1]) # print(res.index(max(res)),len(res)) k_pos = k_res.index(max(k_res)) k_len = len(k_res) print('指针位置', k_res.index(max(k_res)), '总刻度线个数', len(k_res)) # print('position', k_res.index(max(k_res)) / (len(k_res) - 1) * 100) return border, k_pos, k_len
def findPointer(src, heart1, heart2): r1 = heart1[2] hei1 = r1 / 3.5 r2 = heart2[2] hei2 = r2 / 2 mask = np.zeros(src.shape, np.uint8) cv2.circle(mask, (heart1[0], heart1[1]), int(r1 + hei1), 255, -1) cv2.circle(mask, (heart2[0], heart2[1]), int(r2 + hei2), 0, -1) cut1 = cv2.bitwise_and(mask, src) cv2.imwrite('./pointer3/3_adp1.jpg', cut1) mask = np.zeros(src.shape, np.uint8) cv2.circle(mask, (heart1[0], heart1[1]), int(r1 + hei1), 255, -1) cut_tmp = cv2.bitwise_and(mask, src) mask = np.zeros(src.shape, np.uint8) cv2.circle(mask, (heart2[0], heart2[1]), int(r2 + hei2), 255, -1) cut2 = cv2.bitwise_and(mask, cut_tmp) cv2.imwrite('./pointer3/3_adp2.jpg', cut2) h1 = r1 / 2.5 mask = np.zeros(src.shape, np.uint8) cv2.circle(mask, (heart1[0], heart1[1]), int(r1 + hei1), 255, -1) cv2.circle(mask, (heart1[0], heart1[1]), int(r1 + hei1 / 3), 0, -1) cut_num1 = cv2.bitwise_and(mask, cut1) cv2.imwrite('./pointer3/3_num1.jpg', cut_num1) h2 = r2 / 2.5 mask = np.zeros(src.shape, np.uint8) cv2.circle(mask, (heart2[0], heart2[1]), int(r2 + hei2), 255, -1) cv2.circle(mask, (heart2[0], heart2[1]), int(r2 + hei2 / 2.5), 0, -1) cut_num2 = cv2.bitwise_and(mask, cut2) cv2.imwrite('./pointer3/3_num2.jpg', cut_num2) theta1 = detect_pointer(cut1, heart1, 1) theta2 = detect_pointer(cut2, heart2, 2) cut1 = make_kedu_area(cut1, heart1, theta1) cut2 = make_kedu_area(cut2, heart2, theta2) # mask = np.zeros(src.shape, np.uint8) # # cv2.ellipse(mask,(heart1[0],heart1[1]),0,0,0,180,255) # angle1 = -180/np.pi * theta1 # angle2 = 180/np.pi * theta2 # cv2.ellipse(mask, (heart1[0],heart1[1]), (int(r1 + hei1), int(r1 + hei1)), 0, angle1-30, angle1+30, 255, -1) # cut1 = cv2.bitwise_and(mask, cut1) # cv2.imwrite('./pointer3/3_ellipse.jpg', cut1) kedu1, area_sets1 = polar_area(cut1, heart1) ver1 = projectVertical(kedu1) # print(ver1) kedu2, area_sets2 = polar_area(cut2, heart2) ver2 = projectVertical(kedu2) maxtab1, mintab1 = peakdetective.peakdet(ver1, 10) maxtab2, mintab2 = peakdetective.peakdet(ver2, 10) rate1 = findeadge(maxtab1) rate2 = findeadge(maxtab2) # drawplot(maxtab1,ver1) drawplot(maxtab2, ver2) polar_num(cut_num1, heart1, area_sets1, 1) # polar_num(cut_num2,heart2,area_sets2,2) return rate1, rate2