ValueError: could not broadcast input array from shape (1,19) into shape (1,20)

时间:2016-04-04 17:50:58

标签: python arrays numpy object-slicing

I keep on getting this error when I am trying to do simple numpy array slicing. It's hard to describe this code but basically I have an image (binarised, by name and properties) and I have created a small box window (1x20), I am trying to loop through every pixel with this window and count the number of black pixels in the window. However at the end of each column (So when i has gone from 0 to h) I want to find the local minima of black pixels, and then I will make the window = 0 (white) at these points (this is a bit irrelevant though).

I just keep having this error when slicing numpy arrays, it always says the array is one column too short and i don't know why. Anyway, heres my code..

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'

I've highlighted where the error is (in the double for loops). Any help appreciated thanks!!!!

So its clear that windowH is 1x20 and the chunk of 'binarised' that I am trying to enter also 1x20 but it says it is 1x 19!???!¬?

########## REPOST

Now I have a very similar error with this function I have created, except this time the error reads:

ValueError: could not broadcast input array from shape (2,3,3) into shape (3,3,3)

Here is my code... I have tried reducing the iterations in the look so that the window does not try and fill itself with values that are outside the image but it still wont work. Any suggestions? Thank you.

#%% Import Libraries
import numpy as np
import cv2

#%% 3D Median Filter Function

def Median_Filter_3D(image,kernel):

    window = np.zeros(shape=(kernel,kernel,kernel), dtype = np.uint8)
    n = (kernel-1)/2    #Deals with Image border
    imgout = np.empty_like(image)
    w,h,l = image.shape

#Start Loop over each pixel

    for x in np.arange(0,(h-kernel-3),1):
        for y in np.arange(0,(w-kernel-3),1):
            for z in np.arange(0,(l-kernel-3),1):
                window[:,:,:] = image[x:x+kernel,y:y+kernel,z:z+kernel]
                med = np.median(window)
                imgout[x+n,y+n,z+n] = med 
    return(imgout)

1 个答案:

答案 0 :(得分:0)

Your for loop for j should range from 0 to w-20. Otherwise j+20 will go over the width of the image causing it to crop it at the end.