Example using opencv, Pi5 and pi camera
The sliders set the color filtering, set for pink at present may need adjustment.
Crop boxes are defined in code, may need adjustment to suit your setup. More advanced code could automatically find the pink bars...
Values shown are relative brightness.
The sliders set the color filtering, set for pink at present may need adjustment.
Crop boxes are defined in code, may need adjustment to suit your setup. More advanced code could automatically find the pink bars...
Values shown are relative brightness.
Code:
#!/usr/bin/env python3import cv2import numpy as npfrom picamera2 import Picamera2# start the video streampicam2 = Picamera2()picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640, 480)}))picam2.start()font = cv2.FONT_HERSHEY_SIMPLEXcv2.namedWindow('image')# startup valueslowH = 130highH = 200lowS = 100highS = 255lowV = 0highV = 255# crop setup# crop1x1_1 = 340x1_2 = 400y1_1 = 160y1_2 = 190#crop2x2_1 = 340x2_2 = 400y2_1 = 125y2_2 = 155 def callback(x): pass# create trackbars for color changecv2.createTrackbar('lowH','image',lowH,255,callback)cv2.createTrackbar('highH','image',highH,200,callback)cv2.createTrackbar('lowS','image',lowS,255,callback)cv2.createTrackbar('highS','image',highS,255,callback)cv2.createTrackbar('lowV','image',lowV,255,callback)cv2.createTrackbar('highV','image',highV,255,callback)while True: # grab the frame frame = picam2.capture_array() # get trackbar positions lowH = cv2.getTrackbarPos('lowH', 'image') highH = cv2.getTrackbarPos('highH', 'image') lowS = cv2.getTrackbarPos('lowS', 'image') highS = cv2.getTrackbarPos('highS', 'image') lowV = cv2.getTrackbarPos('lowV', 'image') highV = cv2.getTrackbarPos('highV', 'image') hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_hsv = np.array([lowH, lowS, lowV]) higher_hsv = np.array([highH, highS, highV]) mask = cv2.inRange(hsv, lower_hsv, higher_hsv) frame2 = cv2.bitwise_and(frame, frame, mask=mask) # define cropped areas cv2.rectangle(frame2, (x1_1, y1_1), (x1_2, y1_2), (0, 255, 0), 2) cropped_img1 = frame2[y1_1:y1_2, x1_1:x1_2] cv2.rectangle(frame2, (x2_1, y2_1), (x2_2, y2_2), (0, 255, 0), 2) cropped_img2 = frame2[y2_1:y2_2, x2_1:x2_2] # convert crops to gray and calculate sum gray_frame1 = cv2.cvtColor( cropped_img1, cv2.COLOR_BGR2GRAY) gray_frame2 = cv2.cvtColor( cropped_img2, cv2.COLOR_BGR2GRAY) cv2.putText(frame2,str(np.sum(gray_frame1)), (400,200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.putText(frame2,str(np.sum(gray_frame2)), (400,160), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2) # show the images dim = (320, 240) resize1 = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA) resize2 = cv2.resize(frame2, dim, interpolation = cv2.INTER_AREA) output = np.hstack((resize1,resize2)) cv2.imshow('image', output) k = cv2.waitKey(10) # press esc to quit if k == 27: breakcv2.destroyAllWindows()Statistics: Posted by gordon77 — Thu Jan 08, 2026 1:50 pm