from PIL import Image, ImageDraw # 将图片平移并旋转 gray2 = Image.fromarray(src) width, height = gray2.size # 计算中心点和X轴角度 center = (max_point[0], max_point[1]) angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi img_translated = gray2.transform((width, height), Image.AFFINE, (1, 0, center[0] - width/2, 0, 1, center[1] - height/2), resample=Image.BICUBIC) img_translated_rotated = img_translated.rotate(angle, resample=Image.BICUBIC, expand=True) #img_translated_rotated.show() #裁剪 img4 = Image.fromarray(src) width1, height1 = img4.size width2, height2 = img_translated_rotated.size left = (width2 - width1 )/2 top = (height2 - height1 )/2 right = (width2 - width1 )/2 + width1 bottom = (height2 - height1 )/2 + height1 cropped_image = img_translated_rotated.crop((left, top, right, bottom )) import cv2 GRID_STEP = distance/2 # 设置1010栅格(暂时尝试) grid_num_x = 10 grid_num_y = 10 def transform_point_set(points, max_point, distance, angle): # 平移向量 translation_vector = np.array([distance * np.cos(anglenp.pi/180), distance * np.sin(anglenp.pi/180)]) # 旋转矩阵 rotation_matrix = np.array([[np.cos(anglenp.pi/180), -np.sin(anglenp.pi/180)], [np.sin(anglenp.pi/180), np.cos(angle*np.pi/180)]]) # 将点集转换为 numpy 数组 point_array = np.array(points) max_point_array = np.array(max_point) # 对点集进行平移和旋转 point_array = (point_array - max_point_array) @ rotation_matrix + max_point_array + translation_vector # 将 numpy 数组转换为列表 points2 = point_array.tolist() return points2 points2 = transform_point_set(points, max_point, distance, angle) print(points2) #第2.5部分(用作确认检验) from PIL import Image, ImageDraw #裁剪 img4 = Image.fromarray(src) width1, height1 = img4.size width2, height2 = img_translated_rotated.size left = (width2 - width1 )/2 top = (height2 - height1 )/2 right = (width2 - width1 )/2 + width1 bottom = (height2 - height1 )/2 + height1 cropped_image = img_translated_rotated.crop((left, top, right, bottom )) # 导入图片() img_array = np.asarray(cropped_image) img = Image.fromarray(img_array) draw = ImageDraw.Draw(img) for point in point
时间: 2023-06-18 11:07:58 浏览: 178
这是一段 Python 代码,主要是对图片进行平移和旋转,并进行裁剪,然后将一组点坐标进行平移和旋转后输出。具体实现过程中,代码使用了 PIL 库和 OpenCV 库。首先通过 PIL 库中的 Image 类将源图片转换为灰度图像 gray2,然后计算出中心点和旋转角度,通过 transform 和 rotate 方法对图片进行平移和旋转操作。接着通过 crop 方法进行裁剪,将裁剪后的图片转换为 numpy 数组 img_array。最后使用 PIL 库中的 ImageDraw 类和 for 循环对一组点坐标进行平移和旋转,并输出平移和旋转后的点坐标。
相关问题
from PIL import Image, ImageDraw # 将图片平移并旋转 gray2 = Image.fromarray(src) width, height = gray2.size # 计算中心点和X轴角度 center = (max_point[0], max_point[1]) angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi img_translated = gr
ay2.transform((width, height), Image.AFFINE, (1, 0, -center[0], 0, 1, -center[1])) # 平移图片
img_rotated = img_translated.rotate(angle) # 旋转图片
draw = ImageDraw.Draw(img_rotated) # 获取绘图对象
draw.line([max_point, point2], fill='red', width=2) # 绘制直线
img_rotated.show() # 显示处理后的图片
以上代码是对一张图片进行平移和旋转,并在图片上绘制一条直线。首先利用PIL库中的Image类将数组转换成图片对象,然后计算中心点和旋转角度,利用transform()方法平移图片,利用rotate()方法旋转图片,最后利用Draw类绘制直线。最后通过show()方法显示处理后的图片。
#第四部分 旋转图片 from PIL import Image, ImageDraw # 将图片平移并旋转 gray2 = Image.fromarray(src) width, height = gray2.size # 计算中心点和X轴角度 center = (max_point[0], max_point[1]) angle = np.arctan2(point2[1] - max_point[1], point2[0] - max_point[0]) * 180 / np.pi img_translated = gray2.transform((width, height), Image.AFFINE, (1, 0, center[0] - width/2, 0, 1, center[1] - height/2), resample=Image.BICUBIC) img_translated_rotated = img_translated.rotate(angle, resample=Image.BICUBIC, expand=True) #img_translated_rotated.show() import cv2 GRID_STEP = distance/2 # 设置10*10栅格(暂时尝试) grid_num_x = 10 grid_num_y = 10 def transform_point_set(points, max_point, distance, angle): # 平移向量 translation_vector = np.array([distance * np.cos(angle*np.pi/180), distance * np.sin(angle*np.pi/180)]) # 旋转矩阵 rotation_matrix = np.array([[np.cos(angle*np.pi/180), -np.sin(angle*np.pi/180)], [np.sin(angle*np.pi/180), np.cos(angle*np.pi/180)]]) # 将点集转换为 numpy 数组 point_array = np.array(points) max_point_array = np.array(max_point) # 对点集进行平移和旋转 point_array = (point_array - max_point_array) @ rotation_matrix + max_point_array + translation_vector # 将 numpy 数组转换为列表 points2 = point_array.tolist() return points2 操作之后点和再图上原本的位置不再重合,请分析原因
经过平移和旋转操作后,图像的像素位置发生了变化,导致点集与原本的位置不再重合。这是因为平移和旋转操作会改变图像的坐标系和像素位置,因此点集也需要进行相应的坐标变换才能与图像对应。在代码中,函数 transform_point_set 实现了对点集进行平移和旋转变换的操作,但可能存在一些细节问题导致变换不准确,需要进一步检查和调试。
阅读全文