【问题标题】:TypeError: function takes exactly 4 arguments (2 given)TypeError:函数只需要 4 个参数(给定 2 个)
【发布时间】:2021-04-02 04:25:28
【问题描述】:

我目前正在研究 opencv - 模糊俄罗斯车牌,但我每次运行代码时都会收到 error

Traceback (most recent call last):
  File "C:/Users/rohit/Desktop/test.py", line 29, in <module>
    result = detect_plate(img)
  File "C:/Users/rohit/Desktop/test.py", line 26, in detect_plate
    cv2.rectangle(plates,(x,y),(x+w,y+h),(255,0,0),7.5)
TypeError: function takes exactly 4 arguments (2 given)

这是什么问题? 我确实正确加载了所有图像和 xml 文件

import cv2
import numpy as np
import matplotlib.pyplot as plt


img = cv2.imread('car_plate.jpg')



def display(img):
    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    fig = plt.figure(figsize = (12,10))
    ax = fig.add_subplot(111)
    ax.imshow(img)
display(img)



plate_cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml')

def detect_plate(img):
    plates = img.copy()
    plate_rect = plate_cascade.detectMultiScale(plates)
    
    for (x,y,w,h) in plate_rect:
        cv2.rectangle(plates,(x,y),(x+w,y+h),(255,0,0),7.5)
    return plates

result = detect_plate(img)
display(result)



def detect_and_blur_plate(img):
    plate_copy = img.copy()

    plate_rect = plate_cascade.detectMultiScale(plate_copy)

    for (x,y,w,h) in plate_rect:
        plate_img = plate_copy[y:y+h,x:x+w]
        plate_img = cv2.medianBlur(plate_img,8)
        plate_copy[y:y+h,x:x+w] = plate_img
    return plate_copy

result = detect_and_blur_plate(img)
display(result)

请帮助我,我是初学者

【问题讨论】:

  • 错误表示该函数需要 4 个参数,而您为它提供了 2 个参数,我会尝试将表达式移出并将它们存储在单个变量中,然后将变量作为函数参数传递,这可能澄清发生了什么。

标签: python opencv face-detection cv2 cascade


【解决方案1】:

OpenCV git board 上存在一个问题。你可以在这里查看:https://github.com/opencv/opencv/issues/15465

【讨论】:

  • 说明:解决方案是确保传递整数,因为其中一些值可能是浮点数。只需在函数的参数中说int(some_number)。 x、y、w、h 或最后一个参数 (7.5) 可能会导致此错误。
【解决方案2】:

这通常是因为(x,y) 的坐标cv2.rectangle(plates,(x,y),(x+w,y+h),(255,0,0),7.5) 中的非int 值

解决办法是:

x = int(x) 
y = int(y)
w = int(w)
h = int(h)

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多