Skip to main content

图像剪裁

图像剪裁1、实现原理2、实现效果3、实现代码

1、实现原理

对NumPy数组(图像)进行切片操作。

2、实现效果

x cd ~/opencv x python3 05.image_crop.py

注意:选中图片按q可以退出程序!

image-20250106151751452

3、实现代码

​ x import cv2 def crop_image(input_path, output_path, start_row, start_col, end_row, end_col): image = cv2.imread(input_path) if image is None: print("Error: Unable to open image file.") return cropped_image = image[start_row:end_row, start_col:end_col] if cv2.imwrite(output_path, cropped_image): print(f"Image saved to {output_path}") cv2.imshow('Image Preview', cv2.imread(output_path)) cv2.waitKey(0) cv2.destroyAllWindows() else: print("Error: Unable to save image file.") crop_image('/home/jetson/opencv/images/yahboom_logo.png', \ '/home/jetson/opencv/images/yahboom_logo_crop.png', \ 50, 50, 200, 500)