Python错误集锦:OpenCV resize()缩放图片 error: (-215:Assertion failed) inv_scale_y > 0 in function ‘cv::resize’

原文链接:http://www.juzicode.com/archives/6250

错误提示:

resize()缩放图片 error: (-215:Assertion failed) inv_scale_y > 0 in function ‘cv::resize’

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)

img1 = cv2.imread('..\\lena.jpg')  
img_ret1 = cv2.resize(img1,None,fx=0.5)
print('img_ret1.shape:',img_ret1.shape)
cv2.imshow('lena-resize1',img_ret1)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-73-4b487550ed95> in <module>
      4 
      5 img1 = cv2.imread('..\\lena.jpg')
----> 6 img_ret1 = cv2.resize(img1,None,fx=0.5)
      7 print('img_ret1.shape:',img_ret1.shape)
      8 cv2.imshow('lena-resize1',img_ret1)

error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\imgproc\src\resize.cpp:4054: error: (-215:Assertion failed) inv_scale_y > 0 in function 'cv::resize'

错误原因:

1、resize()缩放图像时,dsize设置为None,fx虽然设置了值,但是fy使用了默认的0值,导致新生成的图像尺寸高度为0.

 

解决方法:

1、将第2个位置参数的None改为目标图像的大小:

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)

img1 = cv2.imread('..\\lena.jpg')  
#img_ret1 = cv2.resize(img1,None,fx=0.5)
img_ret1 = cv2.resize(img1,(300,300))#修改第2个位置参数为目标图片大小
print('img_ret1.shape:',img_ret1.shape)
cv2.imshow('lena-resize1',img_ret1)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
img_ret1.shape: (300, 300, 3)

2、或者增加设置fy的值,指定目标图像的缩放比例:

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)

img1 = cv2.imread('..\\lena.jpg')  
#img_ret1 = cv2.resize(img1,None,fx=0.5)
img_ret1 = cv2.resize(img1,None,fx=0.5,fy=0.5)
print('img_ret1.shape:',img_ret1.shape)
cv2.imshow('lena-resize1',img_ret1)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2 
img_ret1.shape: (256, 256, 3)

扩展内容:

  1.  OpenCV-Python教程:几何空间变换

 


 

如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注