'salt-and-papper' drop size

1 view (last 30 days)
Oleg Orlov
Oleg Orlov on 2 Jan 2016
Answered: Image Analyst on 2 Jan 2016
Happy New Year! - I need introduce the noise of the ImageJ's 'salt-and-pepper' type in the grey-shade figure, in order to imitate spontaneouse visual noise arising at low illumination. But how could the size of a single drop (random-operated by the 'Process->Noise' in ImageJ) be increased or otherwise defined freehand, as a round unit? Thanks, Oleg Orlov, Inst. for Information Transmission Problems, Moscow Russia

Answers (1)

Image Analyst
Image Analyst on 2 Jan 2016
Use strel() to create a kernel. Then use rand() or randi() or imnoise() to create an image of noise points. Then take that image along with the kernel and pass them to imdilate(). That will enlarge the noise according to the size and shape you specify. Make it binary by casting to logical, if it's not already. Then use it to assign the values there to 255 in the original uint8 gray scale image:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in standard MATLAB demo image.
grayImage = imread('moon.tif');
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
randomPointImage = zeros(rows, columns, 'uint8');
noiseLocations = randperm(numel(randomPointImage), round(0.01 * numel(randomPointImage)));
randomPointImage(noiseLocations) = 1;
% Display the image.
subplot(2, 2, 2);
imshow(randomPointImage, []);
title('Point Noise Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Enlarge the noise.
kernel = strel('disk', 2, 0);
mask = imdilate(randomPointImage, kernel) > 0;
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
title('Noise-only Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Then use it to assign the values there to 255 in the original uint8 gray scale image
noisyImage = grayImage; % Initialize
noisyImage(mask) = 255; % Make 255 where you have noise.
subplot(2, 2, 4);
imshow(noisyImage);
title('Noisy Image', 'FontSize', fontSize, 'Interpreter', 'None');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!