python - 类型错误:预期的 Ptr对于参数“%s”
问题描述
我有这个我自己无法解决的错误。
你能告诉我如何解决吗?
问题出在这个函数中:
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.imread('00.PNG')
histogram_of_pixel_projection(img)
推荐阅读
- sql - 如何在 SQL Sever 中回填缺失的日期
- ios - SwiftUI Picker 不再适用于 Xcode 11 beta 7
- android - Android 将所有视图转移到另一侧
- django - 如何获取用户名而不是 id?
- memory - OpenCL 内存传输函数如何工作?
- react-native - EXPO : 将文件系统导出到文件,进入 android 文件夹
- jmeter - 虽然控制器完成后没有执行下一个线程
- c# - 绑定到页面上 UserControl 的自定义依赖属性不起作用
- apache-kafka - 由于突然终止,kafkastream 重新启动后未收到任何记录
- azure - 如何使用嵌套属性在 cosmos DB 中定义唯一键