转载请注明出处,联系我: t39q@163.com
本人热衷于数据库技术及算法的研究,志同道合之士, 欢迎探讨
import cv2 import numpy as np import math img = cv2.imread('13.jpg') img3 = cv2.imread('13.jpg') img2 = cv2.imread('13.jpg') template = cv2.imread('template5.jpg') img = cv2.GaussianBlur(img, (3, 3),0) template = cv2.GaussianBlur(template, (3, 3),0) #cv2.imshow('img', img) #cv2.imshow('template', template) h1,w1 = img.shape[:2] h2,w2 = template.shape[:2] #print(img.shape) #print(template.shape) #CV_TM_CCORR_NORMED TM_SQDIFF TM_SQDIFF_NORMED res = cv2.matchTemplate(img,template,cv2.TM_SQDIFF_NORMED) #print(res.shape) #print(res) #print("(%d, %d)"%(h1-h2+1,w1-w2+1)) min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res) print('Min:'+str(min_val)) #print(max_val) #print(res[277][689]) print(min_loc) #cv2.imshow('res', res) #threshold = 35123456 threshold = 0.13 loc = np.where( res <= threshold) n=0 for pt in zip(*loc[::-1]): cv2.rectangle(img2, pt, (pt[0] + w2, pt[1] + h2), (0,0,255), 1) n=n+1 #print(pt) #cv2.imshow('img2',img2) #print ('n:'+str(n)) top_left = min_loc bottom_right = (top_left[0]+w2,top_left[1]+h2) img_copy = img.copy() cv2.rectangle(img2,top_left,bottom_right,(0,0,255),2) #cv2.imwrite('img_red.jpg',img2) #cv2.imshow('img2',img2) HSV = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV) H, S, V = cv2.split(HSV) LowerBlue = np.array([0, 43, 46]) UpperBlue = np.array([10, 255, 255]) mask = cv2.inRange(HSV, LowerBlue, UpperBlue) Things = cv2.bitwise_and(img, img, mask=mask) erode_hsv = cv2.erode(Things, None, iterations=0) ThingsGray=cv2.cvtColor(erode_hsv,cv2.COLOR_RGB2GRAY) cnts = cv2.findContours(ThingsGray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] #cv2.drawContours(img3,cnts,-1,(0,0,255),3) n=0 center=np.arange(12).reshape((6, 2)) for i in range(len(cnts)): #rect = cv2.minAreaRect(cnts[i]) rect = cv2.minAreaRect(cnts[i]) (x, y), radius = cv2.minEnclosingCircle(cnts[i]) radius = int(radius) #print (rect[1][0]*rect[1][1]) box = cv2.boxPoints(rect) if rect[1][0]*rect[1][1]>2000: cv2.drawContours(img3, [np.int0(box)], -1, (0, 0, 255), 6) center[n][0] = x center[n][1] = y n=n+1 if n==6 : cv2.line(img3, (center[5][0],center[5][1]), (center[4][0],center[4][1]), (0, 0, 255), 6, 8) cv2.line(img3, (center[5][0],center[5][1]), (center[3][0],center[3][1]), (0, 0, 255), 6, 8) cv2.line(img3, (center[5][0],center[5][1]), (center[2][0],center[2][1]), (0, 0, 255), 6, 8) cv2.line(img3, (center[5][0],center[5][1]), (center[1][0],center[1][1]), (0, 0, 255), 6, 8) cv2.line(img3, (center[5][0],center[5][1]), (center[0][0],center[0][1]), (0, 0, 255), 6, 8) line1_length=math.sqrt(math.pow((center[5][1]-center[4][1]),2)+ \ math.pow((center[5][0]-center[4][0]),2)) line2_length=math.sqrt(math.pow((center[5][1]-center[1][1]),2)+ \ math.pow((center[5][0]-center[1][0]),2)) line3_length=math.sqrt(math.pow((center[5][1]-center[2][1]),2)+ \ math.pow((center[5][0]-center[2][0]),2)) line4_length=math.sqrt(math.pow((center[5][1]-center[0][1]),2)+ \ math.pow((center[5][0]-center[0][0]),2)) line5_length=math.sqrt(math.pow((center[5][1]-center[3][1]),2)+ \ math.pow((center[5][0]-center[3][0]),2)) print ('length is:') print(line1_length,line2_length,line3_length,line4_length,line5_length) print('Ration:') print(line2_length/line1_length*100) print(line3_length/line1_length*100) print(line4_length/line1_length*100) print(line5_length/line1_length*100) print ('angle is:') k1=(center[5][1]-center[4][1])/(center[5][0]-center[4][0]) k2=(center[5][1]-center[1][1])/(center[5][0]-center[1][0]) Cobb =math.fabs(np.arctan((k1-k2)/(float(1 + k1*k2)))*180/np.pi) print (Cobb) #k1=(center[5][1]-center[4][1])/(center[5][0]-center[4][0]) k2=(center[5][1]-center[2][1])/(center[5][0]-center[2][0]) Cobb =math.fabs(np.arctan((k1-k2)/(float(1 + k1*k2)))*180/np.pi) print (Cobb) #k1=(center[5][1]-center[4][1])/(center[5][0]-center[4][0]) k2=(center[5][1]-center[0][1])/(center[5][0]-center[0][0]) Cobb =math.fabs(np.arctan((k1-k2)/(float(1 + k1*k2)))*180/np.pi) print (Cobb) #k1=(center[5][1]-center[4][1])/(center[5][0]-center[4][0]) k2=(center[5][1]-center[3][1])/(center[5][0]-center[3][0]) Cobb =math.fabs(np.arctan((k1-k2)/(float(1 + k1*k2)))*180/np.pi) print (Cobb) else: print ('NG') cv2.imshow('img3',img3) print ('count is:') print ('n:'+str(n)) print (center) cv2.waitKey(0) cv2.destroyAllWindows()