Python - Writing your own function with opencv giving an error

802 Views Asked by At
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

Dear Python Council members,

I've been learning Python and OpenCV and I ran into a problem here.

I'm trying to see if it's possible to include an OpenCV function in my own function, but it seems like I'm doing this wrong. The traceback says something something about dsize.area in cv::resize, but this error message means very little too me because I do not know how this works in the smaller picture.

Can someone guide me in the right direction so the program works as I'd expect?

Thank you very much.

1

There are 1 best solutions below

0
On

What you have looks almost right. Just change the last two lines of the function to:

    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

Since fx and fy are not the 3rd and 4th arguments of the function, you have to specify them as keyword arguments.