opencv TypeError:“插值”是此函数的无效关键字参数

Posted

技术标签:

【中文标题】opencv TypeError:“插值”是此函数的无效关键字参数【英文标题】:opencv TypeError: 'interpolation' is an invalid keyword argument for this function 【发布时间】:2020-04-23 04:32:43 【问题描述】:

当我尝试使用“interpolation=cv2.INTER_CUBIC”调整图像大小时发生错误。我不知道发生了什么。我只是按照https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_geometric_transformations/py_geometric_transformations.html?highlight=resize的指南进行操作

# from PIL import Image
import cv2

def read_img(frompath):
    # return Image.open(frompath)
    return cv2.imread(frompath)

def resize_one(img, size, outpath):
    # out = img.resize(size)
    # out.save(outpath)
    out = cv2.resize(img, size)
    cv2.imwrite(outpath, out, interpolation=cv2.INTER_CUBIC)

def resize_all(img, tasks):
    for (size, outpath) in tasks:
        resize_one(img, size, outpath)

def build_tasks(prefix, sizes):
    t = []
    for (x, y) in sizes:
        t.append(((x, y), prefix + '_' + str(x) + '_' + str(y) + '.png'))
    return t

def square_tasks(widths):
    t = []
    for w in widths:
        t.append((w, w))
    return t

def main():
    s = [72, 48, 96, 144, 192]
    p = 'logo'
    i = './logo_1280.png'
    t = build_tasks(p, square_tasks(s))
    img = read_img(i)
    resize_all(img, t)

if __name__ == '__main__':
    main()
libpng warning: iCCP: known incorrect sRGB profile
Traceback (most recent call last):
  File ".\main.py", line 39, in <module>
    main()
  File ".\main.py", line 36, in main
    resize_all(img, t)
  File ".\main.py", line 16, in resize_all
    resize_one(img, size, outpath)
  File ".\main.py", line 12, in resize_one
    cv2.imwrite(outpath, out, interpolation=cv2.INTER_CUBIC)
TypeError: 'interpolation' is an invalid keyword argument for this function

我正在使用 opencv-contrib-python 4.2.0.34

【问题讨论】:

【参考方案1】:

您必须在 resize 而不是您的 imwrite 中进行插值。

def resize_one(img, size, outpath):
    # out = img.resize(size)
    # out.save(outpath)
    out = cv2.resize(img, size,  interpolation=cv2.INTER_CUBIC )
    cv2.imwrite(outpath, out)

【讨论】:

以上是关于opencv TypeError:“插值”是此函数的无效关键字参数的主要内容,如果未能解决你的问题,请参考以下文章

获取“first_name”的Django是此函数的无效关键字参数“创建模型类实例时的TypeError”

类型错误:“作者”是此函数的无效键参数

OpenCV中resize函数插值算法的实现过程(五种)

短道速滑一OpenCV中cvResize函数使用双线性插值缩小图像到长宽大小一半时速度飞快(比最近邻还快)之异象解析和自我实现。

opencv FisherFaceRecognizer 的 train() 函数显示 TypeError: src is not a numpy array, not a scalar

OpenCV中的resize缩放使用最邻近插值INTER_NEAREST仍会产生新值