Skip to content

Instantly share code, notes, and snippets.

@dyrkow
Last active December 30, 2018 12:52
Show Gist options
  • Save dyrkow/c8b154650a38e660407261b7d3a1775f to your computer and use it in GitHub Desktop.
Save dyrkow/c8b154650a38e660407261b7d3a1775f to your computer and use it in GitHub Desktop.
Цифровые фильтры изображений
const fs = require('fs');
const bmp = require('bmp-js');
const FILE_NAME = 'photo.bmp';
const SAVE_NAME = 'new-photo.bmp';
const MAX_VALUE = 255;
const BR = 70; // коэффециент осветления
const CST = 0.6; // коэффециент увеличения контрастности
const NOISE = 0.2;// интенсивность шума
const BLUR = 15;// величина интенсивности смазывания
const HARSMATRIX = [
[-2,-2,-2],
[-2,18,-2],
[-2,-2,-2],
]
fs.readFile(FILE_NAME, (err, buffer) => {
if(err) throw err;
let image = bmp.decode(buffer);
let imagePixels = image.data;
// let newImagePixels = imagePixels.map(noise);
let newImagePixels = bordered(image);
image.data = newImagePixels;
fs.writeFile(SAVE_NAME, bmp.encode(image).data, (err) => {
if(err) throw err;
})
});
function brightnessPlus(pixel){
return lim(pixel+BR);
}
function brightnessMinus(pixel){
return lim(pixel-BR);
}
function invert(pixel){
return lim(MAX_VALUE - pixel);
}
function contrast(pixel){
return lim((pixel - 128) * CST + 128);
}
function brightContrast(pixel){
return lim(lim((pixel - 128) * CST + 128)+BR);
}
function noise(pixel){
return lim(pixel + random(0, 255) * NOISE);
}
function blur(image){
const array = bufferToMultiarray(image);
const div = Math.pow(BLUR + 1, 2);
for(let r = 0, rl = array.length - 15; rl > r; r++){
for(let c = 0, cl = array[r].length - 15*4; cl > c; c++){
let summ = 0;
for(let recRow = 0, recRL = BLUR; recRL > recRow; recRow++){
for (let recCol = 0, recCL = BLUR; recCL > recCol; recCol++){
summ += array[r+recRow][c+recCol]/div;
}
}
array[r][c] = lim(summ);
}
}
return multiarrayToArray(array);
}
function bordered(image){
const array = bufferToMultiarray(image);
for(let r = 0, rl = (array.length - 2); rl > r; r++){
for(let c = 0, cl = (array[r].length - 4); cl > c; c++){
array[r][c] = lim(255 - Math.abs(array[r][c] - array[r+1][c]));
}
}
return multiarrayToArray(array);
}
function harshness(image){
const array = bufferToMultiarray(image);
for(let r = 0, rl = array.length; rl > r; r++){
for(let c = 0, cl = array[r].length; cl > c; c++){
let summ = 0;
for(let recRow = 0, recRL = HARSMATRIX.length; recRL > recRow; recRow++){
for (let recCol = 0, recCL = HARSMATRIX.length; recCL > recCol; recCol++){
summ += array[r][c] * HARSMATRIX[recRow][recCol];
}
}
array[r][c] = lim(summ);
}
}
return multiarrayToArray(array);
}
function multiarrayToArray(array){
let result = [];
array.map(function(subArray){
result = result.concat(subArray);
})
return result;
}
function bufferToMultiarray(obj){
const width = obj.width * 4;// плотность пикселей 4 раза больше
let pixels = [];
let cache = [];
let i = 0;
obj.data.forEach(function(pixel){
if(i === width){
pixels.push(cache);
cache = [];
i = 0;
}
cache.push(pixel);
i++;
});
if(cache.length!==0){
pixels.push(cache);
}
return pixels;
}
function random(min, max){
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
function lim(number){
return number > 255 ? 255 : number <= 0 ? 0 : number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment