4
import numpy as np
import cv2

img = cv2.imread(r"C:\Users\User\Documents\sypder\try\bird.jpg", cv2.IMREAD_UNCHANGED)
print('Original Dimension:',img.shape)

scale_percentage = 30
width = int(img.shape[0] * scale_percentage/100)
height = int(img.shape[0] * scale_percentage/100)
dim = (width,height)
resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)
print('Resized image',resized.shape)

cv2.imwrite('resized.jpg',resized)
cv2.imshow("Resized image",resized)

cv2.waitKey(0)

error:ile "C:/Users/User/Documents/sypder/try/resize.py", line 18, in resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA) error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4055: error: (-215:Assertion failed) inv_scale_x > 0 in function 'cv::resize'

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
lala
  • 47
  • 1
  • 2
  • 2
    `print(dim)`, what does it say exactly -- also, your `width` calculation is wrong. width is `shape[1]`. – Christoph Rackwitz Apr 09 '22 at 14:06
  • 4
    Another plausible cause: when executing `int(img.shape[0] * scale_percentage/100)` if the value inside of `int()` is smaller than 1, then you will have `width=0` and `height=0`. Thus raising the same error. – Javier TG Apr 25 '22 at 19:00

2 Answers2

0

could you check whether the img path is correct or not? if the path is incorrect, the img might be null and report an error while resizing. Could you add a line after line 123 to check whether the img is read successfully? assert img!=None, "Image reading error. Check whether your image path is correct or not."

If this is the case, please double check your image path.

NIAN L
  • 1
0

We observed this particular error & CV Out of memory error randomly even though input image data is provided correctly. We were upgrading from OpenCV library from version4.4.0 to 4.7.0. Basically input arument value was getting corrupted inside Open CV API function cv::resize. Later issue root cause was spotted in our VC++ project setting--> C\C++ include header dir. Open CV old header dir path (version 4.4.0) was mentioned first followed by the new header dir path (version 4.7.0). After removing old dir path and our project build, issue got addressed. Hope my answer helps if someone is facing similar problem.

SrikanthS
  • 74
  • 1
  • 6