gpt4 book ai didi

python - exceptions.TypeError : src is not a numpy array, 既不是标量

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:04 25 4
gpt4 key购买 nike

import cv2
import numpy as np

def imageMoments(img):
#Single channel(8 bit or floating point 2D array)
read_original = cv2.imread(img)

ret,thresh = cv2.threshold(img, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]

M = cv2.moments(cnt)
print M

cx = int(M[’m10’]/M[’m00’])
cy = int(M[’m01’]/M[’m00’])
return

我得到了错误

src is not a numpy array, neither a scalar

最佳答案

cv2.threshold 需要一个灰度图像 作为参数,而不是表示文件名的字符串。因此,替换:

read_original = cv2.imread(img)
ret,thresh = cv2.threshold(img, 127, 255, 0)

与:

read_original = cv2.imread(img)
imgray = cv2.cvtColor(read_original,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray, 127, 255, 0)

在原始代码中,字符串 img 作为参数传递给 threshold。在修改后的代码中,threshold 的参数改为灰度图像 imgray

关于python - exceptions.TypeError : src is not a numpy array, 既不是标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28536794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com