簡體   English   中英

使用Python進行圖像旋轉

[英]Image Rotation using 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()

我不明白您100%在做什么。 但是,您是否認為圖像的y軸從頂部的0到較低點的正值。 因此,與通常的數學定義相比,方向相反。 您可以通過通常的方式定義rotMat ,但是必須將其沿相反方向運行的圖像定義中的y軸采用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM