MATLAB Answers


How to remove pixels in an image

Asked by Jim
on 21 Sep 2011


I need to remove pixels with zero pixel value in an image(dead pixels)

I have to fill the pixel value with zero with the average of the neighbourhood pixel values.

Can anyone suggest me something about this

Thanks in advance


Log in to comment.


2 Answers

Answer by Image Analyst
on 21 Sep 2011
 Accepted answer

Detect the zero pixels to create a mask. Then use conv2() to get the average. Then make an output image the same as your input image. Then assign the "zero pixel locations" of your output image to the zero pixel locations of your convolved image. Is that good enough explanation for you to figure it out, or do you require the full code (which I don't have time to give you right now)?


Very good explanation IA, exactly the first thing to come to my mind. Convolve the image with a kernel, K, such that, K = ones(3)./8;K(5)=0; and then replace the dead pixels in each. zombies = ~I; (I(zombies) = Iconved(zombies);


I find the position of dead pixels by using the command (not(find))

find will give the list of pixels with non zero value in the image

here I am using (not(find)) to give the list of pixels with zero
value in the image

After that by using conv2() one of the surrounding pixel value wiil replace the dead pixel(pixel value with zero value)

Can u suggest any other method

Thanks in advance

Hi sean de,
Thank you for ur reply

How to take the kernel matrix K=[1 1 1;1 0 1;1 1 1] or
K=[1/8 1/8 1/8;1/8 0 1/8;1/8 1/8 1/8]

Thanks in advance

Log in to comment.

Answer by Image Analyst
on 22 Sep 2011

Here is some code I made up for another question. It's basically the same except that it used the median filter instead of the conv2 filter. It's trivial to modify:

clc;	% Clear command window.
clear;	% Delete all variables.
close all;	% Close all figure windows except those created by imtool.
imtool close all;	% Close all figure windows created by imtool.
workspace;	% Make sure the workspace panel is showing.
fontSize = 15;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
	% Didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist.', fullFileName);
rgbImage = imread(fullFileName);
% Get the dimensions of the image.  numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(3, 4, 1);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize')); 
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the individual red, green, and blue color channels.
subplot(3, 4, 2);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
title('Blue Channel', 'FontSize', fontSize);
% Generate a noisy image.  This has salt and pepper noise independently on
% each color channel so the noise may be colored.
noisyRGB = imnoise(rgbImage,'salt & pepper', 0.05);
subplot(3, 4, 5);
title('Image with Salt and Pepper Noise', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = noisyRGB(:, :, 1);
greenChannel = noisyRGB(:, :, 2);
blueChannel = noisyRGB(:, :, 3);
% Display the noisy channel images.
subplot(3, 4, 6);
title('Noisy Red Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
title('Noisy Green Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
title('Noisy Blue Channel', 'FontSize', fontSize);
% Median Filter the channels:
redMF = medfilt2(redChannel, [3 3]);
greenMF = medfilt2(greenChannel, [3 3]);
blueMF = medfilt2(blueChannel, [3 3]);
% Find the noise in the red.
noiseImage = (redChannel == 0 | redChannel == 255);
% Get rid of the noise in the red by replacing with median.
noiseFreeRed = redChannel;
noiseFreeRed(noiseImage) = redMF(noiseImage);
% Find the noise in the green.
noiseImage = (greenChannel == 0 | greenChannel == 255);
% Get rid of the noise in the green by replacing with median.
noiseFreeGreen = greenChannel;
noiseFreeGreen(noiseImage) = greenMF(noiseImage);
% Find the noise in the blue.
noiseImage = (blueChannel == 0 | blueChannel == 255);
% Get rid of the noise in the blue by replacing with median.
noiseFreeBlue = blueChannel;
noiseFreeBlue(noiseImage) = blueMF(noiseImage);
% Reconstruct the noise free RGB image
rgbFixed = cat(3, noiseFreeRed, noiseFreeGreen, noiseFreeBlue);
subplot(3, 4, 9);
title('Restored Image', 'FontSize', fontSize);


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!