02 地图的激光点检测定位
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # -*- coding:utf-8 -*- 2 3 import cv2 4 import os 5 6 7 images = 'D:\\sunway-AI\\Video_Frame\\' 8 if not os.path.exists(images): 9 os.mkdir(images) 10 11 cap = cv2.VideoCapture("D:\\sunway-AI\\地图定位\\Video\\demo_01.mp4") 12 c=0 13 14 kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)) 15 16 writer = None 17 18 fourcc = cv2.VideoWriter_fourcc(*'MJPG') 19 videoWriter = cv2.VideoWriter("F:\\sunway-AI\\Video_Frame\\OUT_02.avi", fourcc, 24,(640,480))#最后一个是保存图片的尺寸 20 21 while(1): 22 23 # 读取视频帧 24 success, frame = cap.read() 25 26 if success: 27 if c==0: 28 pre_frame = frame.copy() 29 c = c+1 30 continue 31 32 frameDelta = cv2.absdiff(frame, pre_frame) 33 pre_frame = frame.copy() 34 35 frame_gray = cv2.cvtColor(frameDelta, cv2.COLOR_BGR2GRAY) 36 frame_thresh = cv2.threshold(frame_gray, 40, 255, cv2.THRESH_BINARY)[1] 37 # frame_opened = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) 38 39 binary, cnts, hierarchy = cv2.findContours(frame_thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 40 41 for cc in cnts: 42 43 if cv2.contourArea(cc) > 50: 44 continue 45 46 (x, y, w, h) = cv2.boundingRect(cc) 47 48 if w<=0 | h<=0: 49 continue 50 51 frame_roi = frame[x:x+w+1, y:y+h+1] 52 53 H, W, C = frame_roi.shape 54 55 # isPoint = False 56 # for hy in range(H): 57 # for wx in range(W): 58 # B = frame_roi[hy][wx][0] 59 # G = frame_roi[hy][wx][1] 60 # R = frame_roi[hy][wx][2] 61 62 # # print("B=",B," G=",G," R=",R) 63 64 # if(B>200): 65 # # print("*****************") 66 # isPoint = True 67 # break 68 69 # if(isPoint): 70 cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) 71 72 73 # 显示视频帧 74 cv2.imshow("capture", frame) 75 # cv2.imwrite(images + str(c) + '.bmp', frame) 76 77 videoWriter.write(frame) 78 79 c=c+1 80 else: 81 break 82 #等候50ms,播放下一帧,或者按q键退出 83 84 if cv2.waitKey(50) &0xFF ==ord('q'): 85 break 86 87 #释放视频流 88 videoWriter.release() 89 cap.release() 90 91 #关闭所有窗口 92 93 cv2.destroyAllWindows()