RGB to Grayscale

The lightness method averages the most prominent and least prominent colors: (max(R, G, B) + min(R, G, B)) / 2.
The lightness method tends to reduce contrast.

The average method simply averages the values: (R + G + B) / 3.

The luminosity method is a more sophisticated version of the average method. It also averages the values, but it forms a weighted average to account for human perception. We’re more sensitive to green than other colors, so green is weighted most heavily. The formula for luminosity is 0.21 R + 0.72 G + 0.07 B.
The luminosity method works best overall.

Advertisements

Inside Matlab Wiener2 Function

The Wiener filter requires a prior knowledge of the power spectral density of original image which is unavailable in practice.

wiener2 lowpass-filters a grayscale image that has been degraded by constant power additive noise. wiener2 uses a pixelwise adaptive Wiener method based on statistics estimated from a local neighborhood of each pixel.

The wiener2 function applies a Wiener filter (a type of linear filter) to an image adaptively, tailoring itself to the local image variance. Where the variance is large, wiener2 performs little smoothing. Where the variance is small, wiener2 performs more smoothing.

This approach often produces better results than linear filtering. The adaptive filter is more selective than a comparable linear filter, preserving edges and other high-frequency parts of an image. In addition, there are no design tasks; the wiener2 function handles all preliminary computations and implements the filter for an input image. wiener2, however, does require more computation time than linear filtering.

Continue reading Inside Matlab Wiener2 Function

Matlab Noise Implementations

Poisson Noise:

Matlab’s Algorithm:

Adding Poisson Noise into an Image using ‘imnoise’

I = imread('2.jpg');

J = rgb2gray(I);

J = IMNOISE(J,'poisson')

Adding Poisson Noise Manually

I = imread('2.jpg');

a = rgb2gray(I);

a = im2double(a);

sizeA = size(a); % Matrix in MxN form

a = a(:);

b=zeros(size(a));

idx1=find(a<50);

% Matrix Positions whose pixels intensities are less than 50

t=ones(size(idx1));

% Matrix having all ones, the size is equal to idx size

em=-ones(size(idx1));

%Matrix having all -ones, the size is equal to idx size

idx2= (1:length(idx1))';

% Put values in idx2 equal to length of idx1

if (~isempty(idx1)) % if such pixels exists then

g=exp(-a(idx1)); % take Exponential of the values at those pixel

positions

while ~isempty(idx2)

em(idx2)=em(idx2)+1;

t(idx2)=t(idx2).*rand(size(idx2));

idx2 = idx2(t(idx2) > g(idx2));

end

b(idx1)=em;

end

idx1=find(a>=50);

% Cases where pixel intensities are more than 49 units

if (~isempty(idx1))

b(idx1)=round(a(idx1)+sqrt(a(idx1)).*randn(size(idx1)));

end

b = reshape(b,sizeA);

imshow(b)

Salt and pepper noise:

Matlab’s Algorithm:

Adding Salt n Pepper Noise into an Image using ‘imnoise’

I = imread('2.jpg');

J = rgb2gray(I);

K = IMNOISE(J,'salt & pepper',0.05);

figure, imshow(J), figure, imshow(K)

MANUAL Addition OF Salt n Pepper Noise

% p3 = density

I = imread('2.jpg');

J = rgb2gray(I);

p3= 0.05;

x = rand(size(J));

d = find(x < p3/2);

J(d) = 0; % Minimum value

d = find(x >= p3/2 & x < p3);

J(d) = 255; % Maximum (saturated) value

imshow(J)

Gaussian Noise:

Gaussian noise is an idealized form of white noise, which is caused by random

fluctuations in the signal. We can observe white noise by watching a television

which is slightly mistuned to a particular channel. Gaussian noise is white noise

which is normally distributed. The effect can again be demonstrated by the

imnoise function.

Matlab’s Algorithm:

b = a + sqrt(p4)*randn(sizeA) + p3;

ADDING GAUSSIAN NOISE INTO AN IMAGE USING IMNOISE

I = imread('2.jpg');

J = rgb2gray(I);

K = imnoise(J,'gaussian', 0.05);

figure, imshow(J), figure, imshow(K)

MANUAL ADDITION OF GAUSSIAN NOISE

% p3 mean

% p4 variance

I = imread('2.jpg');

J = rgb2gray(I);

p3= 0;

p4 = 0.05;

J = im2double(J);

b = J + sqrt(p4)*randn(size(J)) + p3;

imshow(b)

Speckle Noise:

Matlab’s Algorithm:

MANUAL ADDITION OF SPECKLE NOISE

% p3 = variance

I = imread('2.jpg');

J = rgb2gray(I);

p3= 0.05;

J = im2double(J);

b = J + sqrt(12*p3)*J.*(rand(size(J))-.5);

imshow(b)

ADDING SPECKE NOISE AUTOMATICALLY

I = imread('2.jpg');

J = rgb2gray(I);

K = imnoise(J,'speckle', 0.5);

figure, imshow(J), figure, imshow(K)

Random Numbers: Normally Distributed OR Uniformly Distributed

uniform-normalGreen line: Uniform distribution over the range [-5,5]

(MATLAB rand generates uniformly distributed pseudorandom numbers between 0 and 1)

Red Line: Normal distribution with mean of 0 and standard deviation of 1

(MATLAB randn generates normally distributed pseudorandom numbers between approximately -7 and +7)