中值滤波 java实现

中值滤波器原理

         如果不在边缘区域,图像的数据是平缓的,没有太大的差值。因此,一个噪声点的值要么过大,要么过小。比如下图,左图是没有处理的原图,250在该区域由为突出,通过对3*3的9个数据进行排序,将中间值150重新填入,即滤波完成,原本的噪声点被去掉,该区域恢复平缓。同理,在边缘区域中,对于边界来说,高频不会影响,而过低数值将会突出,中值的选择将不会受到影响,除非3*3的整块区域都被污染,这时我们可以考虑更大的核来处理。

java 实现

/**
* <p>Title: medfilt2</p>
* <p>Description:中值滤波 </p>
* @param imagesPos
* @param tmp 模板矩阵 2*2
* @return
*/
public static Matrix medfilt2(Matrix imagesPos, Matrix tmp) {
/**imagesPos 计算矩阵外围追加数值为0外围,以防矩阵下表越界*/
Matrix appendPos=DenseMatrix.Factory.emptyMatrix();
appendPos=appendPos.appendVertically(Ret.NEW, imagesPos);
Matrix v= DenseMatrix.Factory.zeros(1,imagesPos.getColumnCount());
appendPos=appendPos.appendVertically(Ret.NEW, v);
Matrix h= DenseMatrix.Factory.zeros(imagesPos.getRowCount()+1,1);
appendPos=appendPos.appendHorizontally(Ret.NEW, h);

Matrix matRt= DenseMatrix.Factory.zeros(imagesPos.getRowCount(),imagesPos.getColumnCount());
for(int i=0;i<imagesPos.getRowCount();i++) {

Matrix matTmp= DenseMatrix.Factory.zeros(tmp.getColumnCount(), tmp.getColumnCount());
for(int j=0;j<imagesPos.getColumnCount();j++) {
/**求和*/
double [] d= {appendPos.getAsDouble(i,j),appendPos.getAsDouble(i,j+1),
appendPos.getAsDouble(i+1,j),appendPos.getAsDouble(i+1,j+1)};
Arrays.sort(d);
/**依据模板矩阵2*2 计算中值*/
double dbMid=(d[1]+d[2])/2;
matRt.setAsDouble(dbMid, i,j);

}
}

return matRt;

}

猜你喜欢

转载自www.cnblogs.com/tlcContent/p/10414046.html
今日推荐