Basler相机强光区域泛红问题排查及代码修改咨询
Basler相机强光区域泛红问题排查求助
- 现象:Basler相机拍摄画面中强光区域出现泛红(红色色域覆盖),其余区域显示正常,需消除该现象
- 怀疑点:代码中第40-41行的
cv2.COLOR_BGR2RGB颜色空间转换及归一化操作可能导致问题,但不清楚修改方向
相关代码
def run_camera(): converter = pylon.ImageFormatConverter() converter.OutputPixelFormat = pylon.PixelType_BGR8packed converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned top_camera.OutputQueueSize = 2 top_camera.StartGrabbing(pylon.GrabStrategy_LatestImages) try: while (True): top_grabResult = top_camera.RetrieveResult(5000, pylon.TimeoutHandling_Return) while not top_grabResult.GrabSucceeded(): logger.info("top grab failed, trying again") top_grabResult = top_camera.RetrieveResult(5000, pylon.TimeoutHandling_Return) if top_grabResult.GrabSucceeded(): image = converter.Convert(top_grabResult) package_detection_img = image.Array package_detection_img = cv2.rotate(package_detection_img, cv2.ROTATE_90_COUNTERCLOCKWISE) # Get the original camera resolution original_h, original_w = package_detection_img.shape[:2] # print(f"Current camera resolution: {original_w} x {original_h}") # Define the desired size as per your original image shape desired_h, desired_w = 1088, 720 left = 623 right = desired_w - left # Adjusted cropping cropped_width = original_w - (left + right) package_detection_img = package_detection_img[:, left:left + cropped_width] # Resize to desired size (original training image size) package_detection_img = cv2.resize(package_detection_img, (desired_w, desired_h)) cv2.imshow('Live Stream', package_detection_img) # display the window with the name 'Live Stream' cv2.waitKey(1) # Adjusted for brand_name_img brand_name_desired_w = 108.5 brand_name_desired_h = 519.5 brand_name_crop_w = int((desired_w - brand_name_desired_w) / 2) brand_name_crop_h = int((desired_h - brand_name_desired_h) / 2) brand_name_img = package_detection_img[brand_name_crop_h:-brand_name_crop_h, brand_name_crop_w:-brand_name_crop_w] brand_name_img = cv2.resize(brand_name_img, (int(brand_name_desired_w), int(brand_name_desired_h))) # Call letterbox here, before the color conversion and normalization package_detection_img = letterbox(package_detection_img, new_shape=(desired_h, desired_w))[0] package_detection_img = cv2.cvtColor(package_detection_img, cv2.COLOR_BGR2RGB) package_detection_img = package_detection_img / 255.0 # cv2.imshow('Live Stream', package_detection_img) # display the window with the name 'Live Stream' # cv2.waitKey(1) # Call transformations here for both package and brand images # Convert numpy arrays to PIL images for transformations package_detection_img = Image.fromarray((package_detection_img * 255).astype(np.uint8)) brand_name_img = Image.fromarray((brand_name_img * 255).astype(np.uint8)) yield package_detection_img, brand_name_img else: logger.info(f"Top camera failed to grab an image") yield np.zeros((desired_h, desired_w, 3)) finally: top_camera.Close() def detect(): if source == '1': for batch in dataloader: # print("Dataloader successfully incorporated") # Unnormalize before displaying batch = unnormalize(batch) frame = batch[0].squeeze(0).permute(1, 2, 0).cpu().numpy() # Convert back to HWC format for OpenCV # Convert back to 0-255 range and to uint8 for OpenCV frame = (frame * 255).astype(np.uint8) # Keep a copy of the frame for display purposes frame_img = frame.copy() # Resize, convert to RGB, normalize, and convert to tensor for inference img = cv2.resize(frame, (int(imgsz[1].item()), int(imgsz[0]))) img = img / 255.0 img = np.ascontiguousarray(img.transpose((2, 0, 1))) # HWC to CHW img = torch.from_numpy(img).float().unsqueeze(0).to(device) print(f"Transformed img shape: {img.shape}")
内容的提问来源于stack exchange,提问作者kora_num14
相关产品推荐
相关产品推荐

