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)
最佳答案
尝试用左下角的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)