最大最小值滤波

最大最小值滤波

最大最小值滤波是一种比较保守的图像处理手段,与中值滤波类似,首先要排序周围像素和中心像素值,然后将中心像素值与最小和最大像素值比较,如果比最小值小,则替换中心像素为最小值,如果中心像素比最大值大,则替换中心像素为最大值。一个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;
        }

  

 

posted @ 2019-05-22 10:33  dnoyeb  阅读(3416)  评论(0编辑  收藏  举报