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(2: draw.ellipse((point[0]-5, point[1]-5, point[0]+5, point[1]+5), fill="red", outline="red") x, y = max_point[0], max_point[1] #注意这里坐标轴的顺序 # 显示图片 img.show()操作之后点和在图上原本的位置不再重合,请给出调试代码
时间: 2023-06-18 20:07:43 浏览: 93
你可以在代码中添加以下调试代码,来观察点的位置是否正确:
1. 在平移向量计算完成后添加以下代码:
```python
# 绘制平移后的点
for point in points:
draw.ellipse((point[0]-5, point[1]-5, point[0]+5, point[1]+5), fill="blue", outline="blue")
```
2. 在旋转完成后添加以下代码:
```python
# 绘制旋转后的点
for point in points:
rotated_point = tuple(np.dot(np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]), np.array(point) - np.array(max_point)) + np.array(max_point))
draw.ellipse((rotated_point[0]-5, rotated_point[1]-5, rotated_point[0]+5, rotated_point[1]+5), fill="green", outline="green")
```
这样可以在图片上绘制出平移前、平移后、旋转后的点,方便观察点的位置是否正确。
相关问题
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 实现了对点集进行平移和旋转变换的操作,但可能存在一些细节问题导致变换不准确,需要进一步检查和调试。
阅读全文
相关推荐















