2015-06-07 19 views
0
import numpy as np 
import cv2 


def resize(image, percentage): 
    img = image 
    fy=percentage 
    fx=percentage 
    img2 = cv2.resize(img, (0,0), fx, fy) 
    return cv2.img2 

img = cv2.imread('test.png') 
img2 = resize(img, 0.45) 


cv2.imshow('image',img2) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

Traceback (most recent call last): 
    File "C:\Users\Jay\Desktop\Portable Python\opencvprogram_ver1.py", line 14, in <module> 
    img2 = resize(img, 0.45) 
    File "C:\Users\Jay\Desktop\Portable Python\opencvprogram_ver1.py", line 10, in resize 
    img2 = cv2.resize(img, (0,0), fx, fy) 
error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\imgwarp.cpp:3209: error: (-215) dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0) in function cv::resize 

親愛的Python安理會成員,Python的 - 用OpenCV中給了一個錯誤

我一直在學習Python和OpenCV中,我遇到了一個問題,在這裏書寫自己的函數。

我想看看是否有可能在我自己的函數中包含一個OpenCV函數,但似乎我做錯了。 traceback在cv :: resize中說了一些關於dsize.area的內容,但是這個錯誤信息對我來說意味着很少,因爲我不知道這是如何在較小的圖片中工作的。

有人可以引導我在正確的方向,所以程序工作正如我所料?

非常感謝。

回答

2

你有什麼看起來幾乎正確。只要改變函數的最後兩行:

img2 = cv2.resize(img, (0,0), fx=fx, fy=fy) # enter fx and fy as keyword arguments 
    return img2 # cv2.img2 was just a typo 

由於fxfy不是函數的第三和第四個參數,則必須將其指定爲關鍵字參數。

+1

哇,工作!謝謝師父! – user111264