Search code examples
imagematlabnoise

Adding noise to image using localvar


I am not able to find example code of imnoise function of matlab using 'localvar' anywhere. Can anyone help me, as i am having really hard time to locate it?? As per matlab documentation, J = imnoise(I,'localvar',V) adds zero-mean, Gaussian white noise of local variance, V, to the image I. V is an array of the same size as I.


Solution

  • Do you mean something like this:

    clc;
    close all; clear all;
    
    
    A = imread('peppers.png');
    
    B = imnoise(A,'localvar',0.05*rand(size(A)));
    
    imshow(B)
    

    which gives this:

    enter image description here

    EDIT: Using the additional input parameters image_intensity and var, here is what you can get:

    C = imnoise(A,'localvar',rand(1,100),rand(1,100));
    

    The result is the following:

    enter image description here

    Moreover, you can specify a liner relationship between the variance and the image intensity, for example, by defining the variables like so:

    image_intensity = rand(1,100);
    var = ones(1,100); % variance is linearly proportional to image intensity.
    

    which outputs this:

    enter image description here

    Of course you can play around as much as you like with the parameters; just be careful in defining the vectors image_intensity and var since their lengths must match.