OpenCV Error: Assertion failed (dsize.area() > 0) in cv::resize

在resize一张 34867*15951 的图片到原先的两倍尺寸时报错

OpenCV Error: Assertion failed (dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0)) in cv::resize, file D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\imgwarp.cpp, line 3493
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-5-a9567f11d2e5> in <module>()
----> 1 img = cv2.resize(im, (0, 0), 2, 2, interpolation=cv2.INTER_CUBIC)

error: D:\Build\OpenCV\opencv-3.2.0\modules\imgproc\src\imgwarp.cpp:3493: error: (-215) dsize.area() > 0 || (inv_scale_x > 0 && inv_scale_y > 0) in function cv::resize

这种情况一般是因为读取的图片为空,但是如果将fx、fy设置为1.8,是可以正常出结果的,所以不是这个问题。

之前也遇到过,一直没有解决,今天看了stackoverflow 上的一篇文章

So it turns out that the problem comes from one line in modules\imgproc\src\imgwarp.cpp:
CV_Assert( ssize.area() > 0 );
When the product of rows and columns of the image to be resized is larger than 2^31, ssize.area() results in a negative number. This appears to be a bug in OpenCV and hopefully will be fixed in the future release. A temporary fix is to build OpenCV with this line commented out. While not ideal, it works for me. 
And I just recently found out that the above applies only to image whose width is larger than height. For images with height larger than width, it's the following line that causes error: 
CV_Assert( dsize.area() > 0 );
So this has to be commented out as well. 

介绍到当被处理的图像的size大于2^31时会溢出为负数,所以导致该bug。下面简单验证下

(34867*2*15951*2 = 2,224,654,068) > (2^31 = 2,147,483,648) > (34867*1.8*15951*1.8 = 1801969795.08)

的确是这样。

原文也说了解决办法,在编译opencv的时候注释掉源代码中的几行断言。

博主是windows平台,没有用编译安装,所以采用另一种方法——使用scipy.misc.imsize()函数,调用格式如下:

img = scipy.misc.imresize(img, 200, interp='cubic')

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值