Please bookmark this page to avoid losing your image tool!

Image Denoiser

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
/**
 * Denoises an image using a median filter.
 * This method is effective at removing salt-and-pepper noise while preserving edges.
 *
 * @param {HTMLImageElement} originalImg The original image element.
 * @param {number} radius The radius of the median filter window. A larger radius provides more an aggressive denoising effect but is slower and can blur details. A radius of 1 uses a 3x3 window, 2 uses a 5x5 window, etc. The value is clamped between 0 and 10.
 * @returns {HTMLCanvasElement} A new canvas element with the denoised image.
 */
function processImage(originalImg, radius = 1) {
    // Sanitize the radius parameter to be a reasonable integer
    const effectiveRadius = Math.max(0, Math.min(Math.floor(radius), 10));

    // Create a canvas to draw the image and manipulate its pixels
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Get the dimensions from the loaded image
    const width = originalImg.naturalWidth;
    const height = originalImg.naturalHeight;

    // Set canvas dimensions
    canvas.width = width;
    canvas.height = height;

    // Draw the original image onto the canvas
    ctx.drawImage(originalImg, 0, 0, width, height);

    // If radius is 0, no denoising is needed, return the canvas as is
    if (effectiveRadius === 0) {
        return canvas;
    }

    // Get the pixel data from the canvas
    const imageData = ctx.getImageData(0, 0, width, height);
    const srcPixels = imageData.data;
    const destImageData = ctx.createImageData(width, height);
    const destPixels = destImageData.data;

    // Iterate over each pixel of the image
    for (let y = 0; y < height; y++) {
        for (let x = 0; x < width; x++) {
            const i = (y * width + x) * 4;

            // Handle edges: for pixels near the border, just copy the original pixel
            // This is simpler than other edge-handling methods.
            if (x < effectiveRadius || x >= width - effectiveRadius || y < effectiveRadius || y >= height - effectiveRadius) {
                destPixels[i] = srcPixels[i];
                destPixels[i + 1] = srcPixels[i + 1];
                destPixels[i + 2] = srcPixels[i + 2];
                destPixels[i + 3] = srcPixels[i + 3];
                continue;
            }

            // For each pixel, gather the color values of its neighbors in a window
            const reds = [];
            const greens = [];
            const blues = [];

            for (let ky = -effectiveRadius; ky <= effectiveRadius; ky++) {
                for (let kx = -effectiveRadius; kx <= effectiveRadius; kx++) {
                    const neighborX = x + kx;
                    const neighborY = y + ky;
                    const neighborIndex = (neighborY * width + neighborX) * 4;

                    reds.push(srcPixels[neighborIndex]);
                    greens.push(srcPixels[neighborIndex + 1]);
                    blues.push(srcPixels[neighborIndex + 2]);
                }
            }

            // Sort the color values for each channel
            reds.sort((a, b) => a - b);
            greens.sort((a, b) => a - b);
            blues.sort((a, b) => a - b);

            // The median is the value in the middle of the sorted array
            const medianIndex = Math.floor(reds.length / 2);

            // Set the destination pixel's color to the median values
            destPixels[i] = reds[medianIndex];
            destPixels[i + 1] = greens[medianIndex];
            destPixels[i + 2] = blues[medianIndex];
            destPixels[i + 3] = srcPixels[i + 3]; // Preserve the original alpha channel
        }
    }

    // Put the processed pixel data back onto the canvas
    ctx.putImageData(destImageData, 0, 0);

    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Image Denoiser tool allows users to reduce noise in images, particularly effective for removing salt-and-pepper noise while maintaining edge detail. Users can customize the degree of denoising by adjusting the radius parameter, which influences the filter’s aggressiveness. This tool is ideal for photographers, graphic designers, and anyone looking to enhance image quality by smoothing out unwanted artifacts without heavily blurring crucial image details.

Leave a Reply

Your email address will not be published. Required fields are marked *