Skip to content

Commit 351fdff

Browse files
authored
Fix OpenCV 4.12 cv2.rectangle error due to non-contiguous arrays (#510)
OpenCV 4.12 requires C-contiguous arrays for in-place drawing operations. The image from rasterio (via np.transpose) and the blended array from float arithmetic were non-contiguous, causing cv2.rectangle to fail with "Layout of the output array img is incompatible with cv::Mat". - Ensure self.image is C-contiguous after set_image() - Copy blended array before OpenCV drawing in _render_anns_opencv()
1 parent 04f5ed7 commit 351fdff

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

samgeo/samgeo3.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ def set_image(
344344
"Input image must be either a path, numpy array, or PIL Image."
345345
)
346346

347+
# Ensure the image is C-contiguous for OpenCV compatibility
348+
if not self.image.flags["C_CONTIGUOUS"]:
349+
self.image = np.ascontiguousarray(self.image)
350+
347351
self.image_height, self.image_width = self.image.shape[:2]
348352

349353
# Reset batch processing attributes so that subsequent calls to
@@ -2821,6 +2825,7 @@ def _render_anns_opencv(
28212825
mask_3d = mask_combined[:, :, np.newaxis]
28222826
blended = frame_np * (1 - mask_3d * alpha) + overlay * (mask_3d * alpha)
28232827
blended = np.clip(blended, 0, 255).astype(np.uint8)
2828+
blended = blended.copy()
28242829

28252830
# Draw bounding boxes and labels using OpenCV
28262831
for box, text, color in labels_to_draw:

0 commit comments

Comments
 (0)