Skip to main content

图像平移

图像平移1、实现原理2、实现效果3、实现代码

1、实现原理

使用cv2.warpAffine()函数对图像执行仿射变换变化(平移、旋转、缩放和剪切)。

2、实现效果

x cd ~/opencv x python3 06.image_translate.py

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

image-20250106154840955

3、实现代码

​ x import cv2 import numpy as np def translate_image(input_path, output_path, tx, ty): image = cv2.imread(input_path) if image is None: print("Error: Unable to open image file.") return M = np.float32([[1, 0, tx], [0, 1, ty]]) shifted_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0])) if cv2.imwrite(output_path, shifted_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.") translate_image('/home/jetson/opencv/images/yahboom_logo.png', \ '/home/jetson/opencv/images/yahboom_logo_translate.png', \ 50, 50)