值错误:无法将输入数组从形状(1,19)广播到形状(1,20)

2024-09-30 10:42:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图执行简单的numpy数组切片时,我一直会遇到这个错误。很难描述这段代码,但基本上我有一个图像(二进制,按名称和属性),我创建了一个小框窗口(1x20),我试图循环通过这个窗口的每个像素,并计算窗口中黑色像素的数量。但是在每一列的末尾(所以当我从0到h的时候),我想找到黑色像素的局部最小值,然后在这些点上使窗口=0(白色)(这有点不相干)。在

我只是在切片numpy数组时一直有这个错误,它总是说数组太短了一列,我不知道为什么。不管怎样,这是我的密码。。在

import scipy
import numpy as np
import cv2
from scipy.signal import argrelextrema

#%% Import grain image

imagein = cv2.imread('141110_0.35_armt_amb2_0_load_pag_0000.tif',0)
#resized = cv2.resize(imagein,None,fx=0.2,fy=0.2,interpolation =cv2.INTER_AREA)
crop = imagein[1125:1200,1100:1185]
HC = cv2.equalizeHist(crop)
ret, thresh = cv2.threshold(HC,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
thresh = 255- thresh
cv2.imshow('Image',thresh)
cv2.waitKey(0)
cv2.destroyAllWindows() 

#%% Cropped thresholded Image --> Start segmentation

binarised = cv2.erode(thresh,None,iterations = 1)
binarised = cv2.dilate(binarised,None,iterations = 1)  # Opening of particles

h,w = binarised.shape

#%% Start separation loop

windowH = np.zeros(shape=(1,20))
NumNonZero = np.zeros(shape=(h,w))

#windowV = np.zeros(shape=(20,1))


for j in np.arange(0,w,1):
    for i in np.arange(0,h,1):
        windowH[:,:] = binarised[i:i+1,j:j+20] #####ERRROR HERE
        NumNonZero[i,j] = cv2.countNonZero(windowH)

x = argrelextrema(NumNonZero[i,0:h], np.less)  #gives position of min vlaues in array
y = NumNonZero[argrelextrema(NumNonZero[i,0:h], np.less)[0]] #gives min values'

我已经强调了错误的位置(在double for循环中)。任何帮助,谢谢!!!!在

所以很明显,windowH是1x20,我试图输入的“binarised”块也是1x20,但它说它是1x19!???!¬? 在

##########转载

现在我有一个与我创建的函数非常相似的错误,除了这次错误是:

ValueError:无法将输入数组从形状(2,3,3)广播到形状(3,3,3)

这是我的密码。。。我尝试过减少外观中的迭代次数,这样窗口就不会尝试用图像之外的值填充自己,但它仍然无法工作。有什么建议吗?谢谢您。在

^{pr2}$

Tags: importnumpynone错误np像素数组cv2

热门问题