最大最小值滤波

最大最小值滤波

最大最小值滤波是一种比较保守的图像处理手段,与中值滤波类似,首先要排序周围像素和中心像素值,然后将中心像素值与最小和最大像素值比较,如果比最小值小,则替换中心像素为最小值,如果中心像素比最大值大,则替换中心像素为最大值。一个Kernel矩阵为3X3的最大最小值滤波如下:

 

 

  //最大最小值滤波
        function maxMin(imgData, size) {
            pixelData = tmppixelData = imgData.data,
                size = size || 3;
            for (var i = 0; i < canvas.height; i++) {
                for (var j = 0; j < canvas.width; j++) {
                    var tempR = [],
                        tempG = [],
                        tempB = [];
                    for (var dx = 0; dx < size; dx++) {
                        for (var dy = 0; dy < size; dy++) {
                            var x = i + dx;
                            var y = j + dy;
                            var p = x * canvas.width + y;
                            if (!(dx == ~~(size / 2) && dy == ~~(size / 2))) {
                                tempR.push(tmppixelData[p * 4 + 0])
                                tempG.push(tmppixelData[p * 4 + 1])
                                tempB.push(tmppixelData[p * 4 + 2])
                            }
                        }
                    }
                    tempR.sort();
                    tempG.sort();
                    tempB.sort();
                    var p = i * canvas.width + j;
                    pixelData[p * 4 + 0] = tmppixelData[p * 4 + 0] > tempR[tempR.length - 1] ? tempR[tempR.length - 1] : tmppixelData[p * 4 + 0] < tempR[0] ? tempR[0] : tmppixelData[p * 4 + 0];
                    pixelData[p * 4 + 1] = tmppixelData[p * 4 + 1] > tempG[tempG.length - 1] ? tempG[tempG.length - 1] : tmppixelData[p * 4 + 1] < tempG[0] ? tempG[0] : tmppixelData[p * 4 + 1];
                    pixelData[p * 4 + 2] = tmppixelData[p * 4 + 2] > tempB[tempB.length - 1] ? tempB[tempB.length - 1] : tmppixelData[p * 4 + 2] < tempB[0] ? tempB[0] : tmppixelData[p * 4 + 2];
                }
            }
            imgData.data = pixelData;
            return imgData;
        }

  

 

转载于:https://www.cnblogs.com/ckAng/p/10904407.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值