2016-09-23 130 views
0

我使用python 2.7和opencv。Python/OpenCV:使用cv2.resize()我得到一個錯誤

我試圖使用方法:

cv2.resize(src, dst, Size(), 0.5, 0.5, interpolation=cv2.INTER_LINEAR); 

here拍攝。但是當我運行這個代碼時,我得到了NameError: global name 'Size' is not defined.

你能幫助我嗎?

+0

你需要提供一個目的地大小參數爲'Size':'cv2.resize(src,dst,Size(100,100),0.5,0.5,interpolation = cv2.INTER_LINEAR);'例如 – EdChum

+0

@EdChum但是在它說:「//指定fx和fy,並讓函數計算目標映像大小。」#: – zinon

+0

yes我剛剛意識到這一點,你應該使用Python簽名:'cv2.resize(src,dsize [,dst [,fx [ ,fy [,插值]]]]]→dst'請參閱Zdar的回答 – EdChum

回答

4

您可能正在查看resize方法的C++ API。

Python API中看起來是這樣的:

dst = cv2.resize(src, dsize) 

其中

src - Original image 
dsize - a tuple defining the final size of the image. 

如果你想傳遞額外的參數,那麼你可以使用API​​的命名參數爲:

dst = cv2.resize(src, dsize, fx = 0.5, fy=0.5, interpolation = cv2.INTER_LINEAR) 

由於dsize是必需的參數,但是如果您仍想要resize m方法來爲你計算dsize然後你可以通過參數None

dst = cv2.resize(src, None, fx = 0.5, fy=0.5) 
+0

謝謝!而已!這個問題是因爲我沒有在'Size()'中使用'None'。 – zinon

0

以前的答案是確定的,但我想重新使用存儲 - 來調整image以適應現有的存儲resized(包括3維uint8 numpy的陣列),我需要使用

ret = cv2.resize(image, dsize=resized.shape[:2][::-1], dst=resized) 

請注意,dsize參數是向後的 - 它不是(行,列),但(寬度,高度),因此[::-1]

我只返回ret只能確認(ret == resized).all()

相關問題