In this lesson we learn to become more comfortable creating Regions of Interests (ROIs). We also show some new methods to stack your windows and keep your windows organized and tightly packed. In this video lesson, we developed the following code:
|
1 2 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import cv2 import time from picamera2 import Picamera2 piCam = Picamera2() W=640 H=360 tStart = time.time() fps = 0 RES = (W,H) piCam.preview_configuration.main.size = RES piCam.preview_configuration.main.format = "RGB888" piCam.preview_configuration.controls.FrameRate=60 piCam.preview_configuration.align() piCam.configure("preview") piCam.start() textLowerLeft = (int(W*.01),int(H*.05)) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontThickness = int(W/425) fontScale = H*.0015 fontColor = (0,0,255) topBar = 65 windowWaste = 25 cv2.namedWindow("Camera", cv2.WINDOW_GUI_NORMAL) cv2.resizeWindow("Camera", W, H) cv2.moveWindow("Camera",0,topBar) cv2.namedWindow("Camera Small", cv2.WINDOW_GUI_NORMAL) cv2.resizeWindow("Camera Small", int(W/2), int(H/2)) cv2.moveWindow("Camera Small",0,topBar+windowWaste+H) cv2.namedWindow("Gray Small", cv2.WINDOW_GUI_NORMAL) cv2.resizeWindow("Gray Small", int(W/2), int(H/2)) cv2.moveWindow("Gray Small",int(W/2),topBar+windowWaste+H) quadrants = ["upperLeft","upperRight","lowerLeft","lowerRight"] x=0 for quadrant in quadrants: cv2.namedWindow(quadrant, cv2.WINDOW_GUI_NORMAL) cv2.resizeWindow(quadrant,int(W/4),int(H/4)) cv2.moveWindow(quadrant,W,topBar+int(x*(windowWaste+H/4))) x=x+1 while True: deltaT = time.time() - tStart tStart=time.time() fps = fps*.95 + (1/deltaT)*.05 frame= piCam.capture_array() frame=cv2.flip(frame,-1) print(frame[int(H/2),int(W/2)]) frame[int(H/2):int(H/2)+10,int(W/2):int(W/2)+10] = [0,0,255] ROI = frame[int(H*.25):int(H*.75),int(W*.25):int(W*.75)].copy() ROI[int(.25*H*.5):int(.75*H*.5),int(.25*W*.5):int(.75*W*.5)] = [0,0,0] ROIgray = cv2.cvtColor(ROI,cv2.COLOR_BGR2GRAY) frameSmall=cv2.resize(frame,(int(W/2),int(H/2))) upperLeft = frame[0:int(H/2),0:int(W/2)] upperRight = frame[0:int(H/2),int(W/2):W-1] lowerLeft = frame[int(H/2):H-1,0:int(W/2)] lowerRight = frame[int(H/2):H-1,int(W/2):W-1] quadDict = { "upperLeft" : upperLeft, "upperRight" : upperRight, "lowerLeft" : lowerLeft, "lowerRight" : lowerRight } myText = "FPS: "+str(round(fps,1)) cv2.putText(frame,myText,textLowerLeft,fontFace,fontScale,fontColor,fontThickness) cv2.imshow("Camera", frame) cv2.imshow("Camera Small",ROI) cv2.imshow("Gray Small",ROIgray) for name, image in quadDict.items(): cv2.imshow(name,image) if cv2.waitKey(1)==ord('q'): break cv2.destroyAllWindows() print('Program Terminated') |
