使用Python进行图像旋转

2024-09-27 21:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是: 图像中有两个点,我得到这两点之间的角度,然后旋转图像。我需要得到这些点在图像中的新位置,但是当我试图用旋转矩阵以相同的角度旋转这些点时,这些点不一致,下面的代码有什么问题?在

def rotMat(angle):
  return asarray([[cos(angle), -sin(angle)],[sin(angle),cos(angle)]])

for i in batch2:
  figure(1)
  filename = "../imagens/" + i[-1]
  outputFile = "./output/" + i[-1]
  x1 = float(i[22]) # x coordinate of first point
  y1 = float(i[23]) # y coordinate of first point
  x2 = float(i[34]) # x coordinate of second point
  y2 = float(i[35]) # y coordinate of second point

  # angle of rotation
  angle = arctan((y1-y2)/(x1-x2))

  im = imread(filename)
  im = ndimage.rotate(im, angle*180/pi, reshape=False)

  imshow(im)
  p1 = asarray([x1,y1])
  p2 = asarray([x2,y2])    

  # Rotating the points 
  # [512,680] is the center of the image
  p1n = (p1-[512,680]).dot(rotMat(angle)) + [512,680]

  p2n = (p2-[512,680]).dot(rotMat(angle)) + [512,680]

  print p1n, p2n

  plot(p1n[0],p1n[1],'d')
  plot(p2n[0],p2n[1],'d')

  savefig(outputFile)
  clf()

Tags: of图像coordinatefloatpointx1x2asarray
1条回答
网友
1楼 · 发布于 2024-09-27 21:32:57

我不明白你在做什么。但是,你有没有考虑过,图像中的y轴从顶部的0到较低点的正值。因此,与通常的数学定义相比,方向是相反的。您以通常的方式定义了rotMat,但是您必须将其应用于在oposite方向运行的图像定义中更改的y轴。在

相关问题 更多 >

    热门问题