2017-10-10 9 views
2

필자는 FITS 파일에서 읽은 이미지에 대해 질적 인 배열을 가지고 있습니다. scipy.ndimage.interpolation.rotate을 사용하여 N도 회전 시켰습니다. 그런 다음 회전되지 않은 원본 프레임의 일부 지점 (x, y)이 회전 된 이미지에서 끝나는 지점, 즉 회전 된 프레임 좌표 (x ', y')가 어디인지 파악하고 싶습니다.scipy.ndimage.interpolation.rotate 이후의 회전 된 이미지 좌표?

이것은 매우 간단한 회전 행렬 문제입니다. 그러나 일반적인 수학적 또는 프로그래밍 기반 회전 방정식을 사용하면 새 (x ', y')는 원래 있던 위치로 끝나지 않습니다. Scipy 회전 함수는 이미지 배열의 실제 중심보다는 원점 (0,0)을 기반으로하기 때문에 번역 매트릭스가 필요하다는 점과 관련이 있다고 생각합니다.

누군가 회전 된 프레임 (x ', y')을 가져 오는 방법을 알려주실 수 있습니까? 예를 들어, 다음을 사용할 수 있습니다.

from scipy import misc 
from scipy.ndimage import rotate 
data_orig = misc.face() 
data_rot = rotate(data_orig,66) # data array 
x0,y0 = 580,300 # left eye; (xrot,yrot) should point there 

P. 다음 두 가지 관련 질문에 '답변이 날 도움이되지 않습니다

+0

당신은 우리 솔루션의 정확성을 테스트 할 수있는 예 (또는 예)을 제공 할 수 있습니까? – unutbu

+0

나는 scipy 자신의 기타 racoon 얼굴과 원래의 (x, y) 점을 왼쪽 눈에 사용하여 예제를 추가했습니다. 새로운 프레임 (xrot, yrot)은 회전 된 프레임에서 66도 왼쪽 눈을 가리켜 야합니다. – quantumflash

답변

2

은, 하나는 원점으로 변환해야 다음, 다음, 회전 다시 번역하십시오. 여기서 우리는 이미지의 중심을 원점으로 삼을 수 있습니다.

import numpy as np 
import matplotlib.pyplot as plt 
from scipy import misc 
from scipy.ndimage import rotate 

data_orig = misc.face() 
x0,y0 = 580,300 # left eye; (xrot,yrot) should point there 

def rot(image, xy, angle): 
    im_rot = rotate(image,angle) 
    org_center = (np.array(image.shape[:2][::-1])-1)/2. 
    rot_center = (np.array(im_rot.shape[:2][::-1])-1)/2. 
    org = xy-org_center 
    a = np.deg2rad(angle) 
    new = np.array([org[0]*np.cos(a) + org[1]*np.sin(a), 
      -org[0]*np.sin(a) + org[1]*np.cos(a) ]) 
    return im_rot, new+rot_center 


fig,axes = plt.subplots(2,2) 

axes[0,0].imshow(data_orig) 
axes[0,0].scatter(x0,y0,c="r") 
axes[0,0].set_title("original") 

for i, angle in enumerate([66,-32,90]): 
    data_rot, (x1,y1) = rot(data_orig, np.array([x0,y0]), angle) 
    axes.flatten()[i+1].imshow(data_rot) 
    axes.flatten()[i+1].scatter(x1,y1,c="r") 
    axes.flatten()[i+1].set_title("Rotation: {}deg".format(angle)) 

plt.show() 

enter image description here

+1

좋은 해결책! 알고 싶으 시다면, [ndimage.rotate 소스 코드] (https://github.com/scipy/scipy/blob/master/scipy/ndimage/interpolation.py#L728)는 이미지는 0.5 감소. – unutbu

+1

@unutbu 나는 그것이 현미경으로는 생각하지 않았지만 당신은 물론 맞습니다. [중요합니다] 이미지 (https://i.stack.imgur.com/5bbKL.png)는 어쨌든 회전하기에 적합하지 않으며 새로운 matplotlib 버전의 스 캐터 글리프는 어쨌든 부부에 의해 떨어져 있습니다 픽셀 수 어쨌든 솔루션을 업데이트했습니다. – ImportanceOfBeingErnest

+0

아름다운 감사합니다! – quantumflash