使用中值滤波器进行图像平滑 [英] Image Smoothing Using Median Filter

查看:82
本文介绍了使用中值滤波器进行图像平滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用中值滤波器进行图像平滑.为此,我没有使用Python库中的内置函数,而是在编写自己的函数.以下代码用于计算中位数.

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:列表索引超出范围

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.

推荐答案

我使用了您的函数和一些虚拟数据进行测试,并且可以正常工作.仅当x或y的值分别等于row或col时,我才使索引超出范围错误.

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.

检查,在x和y的哪个值上得到该错误.

check, on what value of x and y you get that error.

这篇关于使用中值滤波器进行图像平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆