gpt4 book ai didi

python - 类型错误 : only size-1 arrays can be converted to Python scalars this error

转载 作者:行者123 更新时间:2023-11-30 21:55:09 24 4
gpt4 key购买 nike

My example is here where am i doing wrong I want to add the elements in the array to a variable, but every time I try, I get the same error. Waiting for your solution suggestions

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

img =cv2.imread("23A96F0E154BE344A4C.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
height = np.size(gray, 0)
width = np.size(gray, 1)
lw=int(width*0.05)
h=int(height*0.05)
rw =int(width-(width*0.05))
#lw = leftwidth rw=rightwitdh

leftcornerdown = gray[h:h+10, lw:lw+1]
leftcornerright =gray[h:h+1, lw:lw+10]
rightcornerdown = gray[h:h+10,rw-1:rw]
rightcornerleft = gray[h:h+1, rw-10:rw]
toplam = 0

for i in leftcornerright:
toplam += i
for i in range(0, 10):
toplam += int(leftcornerdown[i])
toplam += int(leftcornerright[i])
toplam += int(rightcornerdown[i])
toplam += int(rightcornerleft[i])
print(toplam)

Thank you for help

最佳答案

尝试将左侧下角的 lw:lw+1 替换为 lw ,并在后面的 4 行中执行相同的操作。这应该可以解决您的错误。

leftcornerdown = gray[h:h+10, lw]
leftcornerright =gray[h, lw:lw+10]
rightcornerdown = gray[h:h+10,rw-1]
rightcornerleft = gray[h, rw-10:rw]
toplam = 0

for i in leftcornerright:
toplam += i
for i in range(0, 10):
toplam += int(leftcornerdown[i])
toplam += int(leftcornerright[i])
toplam += int(rightcornerdown[i])
toplam += int(rightcornerleft[i])
print(toplam)

关于python - 类型错误 : only size-1 arrays can be converted to Python scalars this error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286354/

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