问题
I am working on image smoothing using median filter. For that, I am not using the inbuilt functions within the Python library, rather I am writing my own functions. The following code is for calculating the median.
def CalcMedian(Image, x, y, gridSize): #x and y are nested loops, that run over the entire image.
medianList = []
row, col = Image.shape;
k = int(gridSize/2);
for i in range(gridSize-1):
for j in range(gridSize-1):
if (i+x-k)<0 or (j+y-k)<0 or (i+x-k)>row or (j+y-k)>col:
break;
medianList.append(Image[(i+x-k),(j+y-k)]);
medianList.sort();
length = len(medianList);
if length%2 != 0:
return float(medianList[length/2]);
return float((medianList[int((length-1)/2)] + medianList[int(length/2)]) / 2.0);
I am getting an error in the last line.
IndexError: list index out of range
I can't figure out as to what the problem is, as this is a standard code used for finding the median, and I don't understand where exactly the index would be out of range.
回答1:
I used you function and some dummy data to test and it works. I get the index out of bounds error only when the value of x or y is equal to row or col respectively.
check, on what value of x and y you get that error.
来源:https://stackoverflow.com/questions/58154630/image-smoothing-using-median-filter