Python编写自己的函数,opencv提供

2024-04-19 18:44:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

尊敬的Python理事会成员:

我一直在学习Python和OpenCV,在这里遇到了一个问题。在

我想看看是否有可能在我自己的函数中包含一个OpenCV函数,但似乎我做得不对。回溯表明D尺寸面积在cv::resize中,但这个错误消息对我来说意义不大,因为我不知道在较小的图片中是如何工作的。在

有人能引导我朝正确的方向走吗?这样程序就如我所期望的那样工作了?在

非常感谢。在


Tags: inimageimportimgcv2usersfilefx
1条回答
网友
1楼 · 发布于 2024-04-19 18:44:58

你的东西看起来差不多是对的。只需将函数的最后两行更改为:

    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

由于fx和{}不是函数的第3个和第4个参数,因此必须将它们指定为关键字参数。在

相关问题 更多 >