我在这个Python代码中遇到错误,我该如何调试它?

问题描述 投票:-1回答:1

我在Python中试图对图像进行旋转变换。但没有得到预期的产量。互联网上的所有解决方案都在C#中。

import cv2
import math
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

img = cv2.imread('lena.jpg')
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow("input",img_gray)
imgcopy=img_gray.copy()

rmax,alpha,xc,yc=input("enter rmax alpha center respectively: ").split()
rmax=float(rmax)
alpha=float(rmax)
xc=float(rmax)
yc=float(rmax)
for x in range(img_gray.shape[0]):
    for y in range(img_gray.shape[1]):
        fie=math.radians(alpha)
        xdiff=x-xc
        ydiff=y-yc
        r=math.sqrt(math.pow(xdiff,2)+math.pow(ydiff,2))
        if r>rmax:
            nx=x
            ny=y
        else:
            angle=math.atan2(ydiff,xdiff)+fie*((rmax-r)/rmax)
            nx=round(xc+r*math.cos(angle))
            ny=round(yc+r*math.sin(angle))
            imgcopy[nx][ny]=img_gray[x][y]

cv2.imshow("output",imgcopy)       
cv2.waitKey(0)
cv2.destroyAllWindows()

我期待一个旋转的图像,但输出与输入相同。

python-3.x image-processing twirl
1个回答
0
投票

你可以用这种方式解决它......

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 26 16:46:20 2019

@author: Hafiz
"""

import cv2
import numpy as np
import math

imgIn=cv2.imread('lena.jpg',0)
imgOut=np.zeros(imgIn.shape,dtype='uint8')

alpha=float(input('Enter alpha: '))
alpha=np.deg2rad(alpha)
rmax=int(input('Enter rmax: '))

centerX=int(imgIn.shape[1]/2)
centerY=int(imgIn.shape[0]/2)

for i in  range(imgOut.shape[0]):
    for j in range(imgOut.shape[1]):
        dX=j-centerX
        dY=i-centerY
        r=math.sqrt(dX*dX+dY*dY)
        if(r<=rmax):
            beta=math.atan2(dY,dX)+alpha*((rmax-r)/rmax)
            x=round(centerX+r*math.cos(beta))
            y=round(centerY+r*math.sin(beta))
        else:
            x=j
            y=i
        if(x>=imgIn.shape[1] or y>=imgIn.shape[0] or x<0 or y<0):
            imgOut.itemset((i,j),0)
        else:
            imgOut.itemset((i,j),imgIn.item(y,x))
cv2.imshow('in',imgIn)       
cv2.imshow('out',imgOut)
cv2.waitKey(0)
cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.