| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 
 | import numpy as npimport cv2
 
 color_dist = {'Lower': np.array([100, 50, 10]), 'Upper': np.array([130, 150, 150])}
 
 cap = cv2.VideoCapture(r'E:\car\航天智慧物流-线上赛规则\五个关键技术任务\03. 计算机视觉\视觉任务\测试数据\1-主场景\camtest.mp4')
 while True:
 ret, frame = cap.read()
 frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
 frame2=frame.copy()
 frame = frame[-150:-1, 1:910]
 
 gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
 
 hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
 erode_hsv = cv2.erode(hsv, None, iterations=2)
 
 inRange_hsv = cv2.inRange(erode_hsv, color_dist['Lower'], color_dist['Upper'])
 
 cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
 
 target_list = []
 x=[]
 y=[]
 for c in cnts:
 if cv2.contourArea(c) < 100 or cv2.contourArea(c) > 1500:
 continue
 else:
 target_list.append(c)
 
 for i in target_list:
 M = cv2.moments(i)
 center_x = int(M['m10'] / M['m00'])
 center_y = int(M['m01'] / M['m00'])
 x.append(center_x)
 y.append(center_y)
 
 rect = cv2.minAreaRect(i)
 box = cv2.boxPoints(rect)
 cv2.drawContours(frame2, [np.int0(box)], -1, (0, 255, 255), 2)
 
 if len(target_list) > 2 :
 x=np.array(x)
 z1 = np.polyfit(y, x, 2)
 elif len(target_list) == 2 :
 x=np.array(x)
 z1 = np.polyfit(y, x, 1)
 else :
 z1 = 0
 p1 = np.poly1d(z1)
 
 if len(target_list) >= 2 :
 for i in range(1,frame.shape[0]-1):
 cv2.line(frame2, (round(p1(i)), i+390), (round(p1(i+1)),i+391), (255, 0, 0), 3)
 fx=str(p1)
 cv2.putText(frame2, fx, (10, 500), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2, cv2.LINE_AA)
 
 cv2.waitKey(1)
 
 cv2.imshow('2',frame2)
 
 
 cap.release()
 cv2.waitKey(0)
 cv2.destroyAllWindows()
 
 
 |