1
我想在我定義的函數中旋轉圖像並將結果保存在主函數中額外使用的參數中。使用Python在我定義的函數中通過opencv旋轉圖像
的代碼如下:
import cv2
def rotate(img1, img2): # rotate img1 and save it in img2
angle = 30 # rotated angle
h, w, c = img1.shape
m = cv2.getRotationMatrix2D((w/2, h/2), angle, 1)
img2 = cv2.warpAffine(img1, m, (w, h)) # rotate the img1 to img2
cv2.imwrite("rotate1.jpg", img2) # save the rotated image within the function, successfully!
img = cv2.imread("test.jpg")
img_out = None
rotate(img, img_out)
cv2.imwrite("rotate2.jpg", img_out) # save the rotated image in the main function, failed!
print("Finished!")
結果 「IMG2」 保存功能 「旋轉」 是確定的。 但是函數參數中的一個「img_out」無法保存。
它有什麼問題?我怎樣才能解決它而不使用全局變量?謝謝!