Last active
February 15, 2021 11:53
-
-
Save Tsuk1ko/18416f62fcb0cf4098cde31b95a581a6 to your computer and use it in GitHub Desktop.
Jimp 中值滤波,随便写来测试用的,没优化,效率很低
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jimp.prototype.medianBlur = function () { | |
const pointsOffset = [-1, 0, 1].flatMap(x => [-1, 0, 1].map(y => [x, y])); | |
const newData = Buffer.from(this.bitmap.data); | |
this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) { | |
if (x === 0 || x === this.bitmap.width - 1 || y === 0 || y === this.bitmap.height - 1) return; | |
for (let i = 0; i < 3; i++) { | |
const points = pointsOffset.map(([ox, oy]) => this.bitmap.data[this.getPixelIndex(x + ox, y + oy) + i]); | |
points.sort(); | |
const median = points[Math.floor((points.length - 1) / 2)]; | |
newData[this.getPixelIndex(x, y) + i] = median; | |
} | |
}); | |
this.bitmap.data = newData; | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment