【问题标题】:TypeError: Expected Ptr<cv::UMat> for argument '%s'类型错误:参数“%s”的预期 Ptr<cv::UMat>
【发布时间】:2021-04-30 04:10:21
【问题描述】:

我遇到了这个我自己无法解决的错误。

你能告诉我怎么解决吗?

问题出在这个函数上:

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

def histogram_of_pixel_projection(img):

    # list that will contains all digits
    caracrter_list_image = list()

    # img = crop(img)

    # Add black border to the image
    BLACK = [0, 0, 0]
    #img = cv2.copyMakeBorder(img, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=BLACK)
    img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT) 

    # change to gray
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Change to numpy array format
    nb = np.array(gray)

    # Binarization
    nb[nb > 120] = 255
    nb[nb < 120] = 0

    # compute the sommation
    x_sum = cv2.reduce(nb, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
    y_sum = cv2.reduce(nb, 1, cv2.REDUCE_SUM, dtype=cv2.CV_32S)

    # rotate the vector x_sum
    x_sum = x_sum.transpose()

    # get height and weight
    x = gray.shape[1]
    y = gray.shape[0]

    # division the result by height and weight
    x_sum = x_sum / y
    y_sum = y_sum / x

    # x_arr and y_arr are two vector weight and height to plot histogram projection properly
    x_arr = np.arange(x)
    y_arr = np.arange(y)

    # convert x_sum to numpy array
    z = np.array(x_sum)

    # convert y_arr to numpy array
    w = np.array(y_sum)

    # convert to zero small details
    z[z < 15] = 0
    z[z > 15] = 1

    # convert to zero small details and 1 for needed details
    w[w < 20] = 0
    w[w > 20] = 1

    # vertical segmentation
    test = z.transpose() * nb

    # horizontal segmentation
    test = w * test

    # plot histogram projection result using pyplot
    horizontal = plt.plot(w, y_arr)
    vertical = plt.plot(x_arr ,z)

    plt.show(horizontal)
    plt.show(vertical)

    f = 0
    ff = z[0]
    t1 = list()
    t2 = list()
    for i in range(z.size):
        if z[i] != ff:
            f += 1
            ff = z[i]
            t1.append(i)
    rect_h = np.array(t1)

    f = 0
    ff = w[0]
    for i in range(w.size):
        if w[i] != ff:
            f += 1
            ff = w[i]
            t2.append(i)
    rect_v = np.array(t2)

    # take the appropriate height
    rectv = []
    rectv.append(rect_v[0])
    rectv.append(rect_v[1])
    max = int(rect_v[1]) - int(rect_v[0])
    for i in range(len(rect_v) - 1):
        diff2 = int(rect_v[i + 1]) - int(rect_v[i])

        if diff2 > max:
            rectv[0] = rect_v[i]
            rectv[1] = rect_v[i + 1]
            max = diff2

    # extract caracter
    for i in range(len(rect_h) - 1):

        # eliminate slice that can't be a digit, a digit must have width bigger then 8
        diff1 = int(rect_h[i + 1]) - int(rect_h[i])

        if (diff1 > 5) and (z[rect_h[i]] == 1):
            # cutting nb (image) and adding each slice to the list caracrter_list_image
            caracrter_list_image.append(nb[int(rectv[0]):int(rectv[1]), rect_h[i]:rect_h[i + 1]])

            # draw rectangle on digits
            cv2.rectangle(img, (rect_h[i], rectv[0]), (rect_h[i + 1], rectv[1]), (0, 255, 0), 1)

    # Show segmentation result
    image = plt.imshow(img)
    plt.show(image)

    return caracrter_list_image

img='00.PNG'
histogram_of_pixel_projection(img)

该代码应该拍摄一张图片,然后执行以下操作:

  • 为图片添加黑色边框
  • 变为灰色
  • 更改为 numpy 数组格式
  • ...

当我运行代码时,我收到以下错误:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    
    <ipython-input-17-eb57438d5985> in <module>()
        184 img='00.PNG'
    
    --> 185 histogram_of_pixel_projection(img)
    
    
    <ipython-input-17-eb57438d5985> in histogram_of_pixel_projection(img)
    
         72     BLACK = [0, 0, 0]
    
         73     #img = cv2.copyMakeBorder(img, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=BLACK)
    
    ---> 74     img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT)
    
         75 
    
         76     # change to gray
    
    
    TypeError: Expected Ptr<cv::UMat> for argument '%s'


编辑:我添加了运行代码所需的导入。

注意:要能够import cv2,您需要pip install opencv-python

【问题讨论】:

  • 希望这可行 img = cv2.copyMakeBorder(np.float32(img), 10, 10, 10, 10, cv2.BORDER_CONSTANT)
  • 在你的函数之外,你基本上是在调用histogram_of_pixel_projection('00.PNG'),所以img只是你函数中的一些字符串。在那里,您没有像 cv2.imread(img) 这样的东西来实际读取图像。因此,当调用img = cv2.copyMakeBorder(img, ...) 时,您传递的是一个字符串(或错误消息中指示的'%s')而不是预期的图像(或错误消息中指示的[可能有点混乱] 的Ptr&lt;cv::UMat&gt;)。跨度>

标签: python numpy opencv matplotlib


【解决方案1】:

#代码

**从 sklearn.preprocessing 导入 LabelEncoder 从 keras.models 导入 model_from_json 将 numpy 导入为 np

加载模型架构、权重和标签

json_file = open('Caracter_Recognition_Train_Model/MobileNets_character_recognition.json', 'r') 加载模型json = json_file.read() json_file.close()

model = model_from_json(loaded_model_json)#加载数字预测模型 model.load_weights("Caracter_Recognition_Train_Model/License_character_recognition.h5") print("[INFO] 模型加载成功...")

标签 = 标签编码器() 标签.classes_ = np.load('Caracter_Recognition_Train_Model/license_character_classes.npy') print("[INFO] 标签加载成功...") **

#错误

**Traceback(最近一次调用最后一次): 文件“C:/Users/Mahdia/PycharmProjects/ProjetFinale_/Caracter_Recognition_Avc_Modele.py”,第 1 行,在 从 sklearn.preprocessing 导入 LabelEncoder 文件“C:\Users\Mahdia\PycharmProjects_ProjetFinale__\venv\lib\site-packages\sklearn_init_.py”,第 82 行,在 从 .base 导入克隆 文件“C:\Users\Mahdia\PycharmProjects_ProjetFinale__\venv\lib\site-packages\sklearn\base.py”,第 17 行,在 从 .utils 导入 IS_32BIT 文件“C:\Users\Mahdia\PycharmProjects_ProjetFinale_\venv\lib\site-packages\sklearn\utils_init_.py”,第 20 行, 在 从 scipy.sparse 导入 issparse 文件“C:\Users\Mahdia\PycharmProjects_ProjetFinale__\venv\lib\site-packages\scipy_init_.py”,第 130 行,在 从 。导入 distributor_init 文件“C:\Users\Mahdia\PycharmProjects_ProjetFinale_\venv\lib\site-packages\scipy_distributor_init.py”,第 61 行,在 WinDLL(os.path.abspath(文件名)) init 中的文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\ctypes_init_.py”,第 364 行 self._handle = _dlopen(self._name, mode) OSError: [WinError 126] Le module spécifié est introuvable **

【讨论】:

    【解决方案2】:

    当它应该是数组图像时,您正在发送一个字符串变量,请使用以下内容:

    img = cv2.imread('00.PNG') 
    histogram_of_pixel_projection(img)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2020-10-06
      • 1970-01-01
      • 2021-08-10
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多