Assignment has more non-singleton rhs dimensions than non-singleton subscripts
8 views (last 30 days)
Show older comments
Hi
I have problem with this program, it should be primitive image denoising filter, but I'm not very experienced at matlab and I'm getting this error
clear all;
clc;
%Source image load
IM = imread('LenaNoise.png');
S=size(IM);
MaxR=S(1);
MaxC=S(2);
FIM=IM;
for i=1 : MaxR
for j=1:MaxC
x=impixel(FIM,i,j);
x1=impixel(FIM,(i+1),j);
x2=impixel(FIM,(i-1),j);
x3=impixel(FIM,i,(j+1));
x4=impixel(FIM,i,(j-1));
if x==0|255
if x1==x | x2==x | x3==x | x4==x
AVG=(x1+x2+x3+x4)/4;
FIM(i,j)=AVG;
end
end
end
end
imshow(FIM);
0 Comments
Answers (1)
Image Analyst
on 3 Sep 2012
Edited: Image Analyst
on 3 Sep 2012
x through x4 are RGB triplets so you need to compare them correctly. Or else just convert from RGB to single or double like below:
%Source image load
rgbImage = imread('LenaNoise.png');
[rows columns numberOfColorChannels] = size(rgbImage)
FIM= single(rgb2gray(rgbImage));
for column = 2 : columns-1
for row = 2 : rows-1
x=FIM(row,column);
x1=FIM((row+1),column);
x2=FIM((row-1),column);
x3=FIM(row,(column+1));
x4=FIM(row,(column-1));
if x == 0 || x == 255
%if x1==x | x2==x | x3==x | x4==x
AVG=(x1+x2+x3+x4)/4;
FIM(row,column)=AVG;
%end
end
end
end
imshow(FIM, []);
title('Denoised Image', 'FontSize', 25);
Anyway, this could be done better with conv2.
Or, if you want to see how I do modified median filter of salt and pepper noise, see below:
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);
uiwait(warndlg(errorMessage));
return;
end
end
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);
imshow(rgbImage);
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);
imshow(redChannel);
title('Red Channel', 'FontSize', fontSize);
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel', 'FontSize', fontSize);
subplot(3, 4, 4);
imshow(blueChannel);
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);
imshow(noisyRGB);
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);
imshow(redChannel);
title('Noisy Red Channel', 'FontSize', fontSize);
subplot(3, 4, 7);
imshow(greenChannel);
title('Noisy Green Channel', 'FontSize', fontSize);
subplot(3, 4, 8);
imshow(blueChannel);
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);
imshow(rgbFixed);
title('Restored Image', 'FontSize', fontSize);
0 Comments
See Also
Categories
Find more on Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!