nested if-else inside nested if-else
5 views (last 30 days)
Show older comments
In the following program it showed error in line 22.If elseif statement can't be used in this way,please suggest alternative way.
%code
function output = blur(img,w)
img(:) = double(img(:))+1;
[row col] = size(img);
output = zeros(row, col);
for i=1:row
for j=1:col
if (i-w)<1
if (j-w)<1
A = img([1:i+w,1:j+w]);
k = mean( img ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([1:i+w,j-w:col]);
k = mean( img ,'all');
output(i,j) = k;
else
A = img([1:i+w,j-w:j+w]);
k = mean( img ,'all');
output(i,j) = k;
end
elseif (i+w)>row
if (j-w)<1
A = img([i-w:row,1:j+w]);
k = mean( img ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([i-w:row,j-w:col]);
k = mean( img ,'all');
output(i,j) = k;
else
A = img([i-w:row,j-w:j+w]);
k = mean( img ,'all');
output(i,j) = k;
end
else
if (j-w)<1
A = img([i-w:i+w,1:j+w]);
k = mean( img ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([i-w:i+w,j-w:col]);
k = mean( img ,'all');
output(i,j) = k;
else
output(i,j) = mean(img(i-w:i+w,j-w:j+w));
end
end
end
end
output(:) = uint8(round(output(:)-1));
Error: File: blur.m Line: 22 Column: 5
Illegal use of reserved keyword "elseif".
0 Comments
Accepted Answer
DGM
on 13 May 2021
Edited: DGM
on 13 May 2021
I don't have the same problem when I run it. There was some other issue though.
img = rgb2gray(imread('sources/table.jpg'));
w = 10;
img(:) = double(img(:))+1;
[row col] = size(img);
output = zeros(row, col);
for i=1:row
for j=1:col
if (i-w)<1
if (j-w)<1
% why are you calculating A, but never using it?
A = img([1:i+w,1:j+w]);
k = mean( img ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([1:i+w,j-w:col]);
k = mean( img ,'all');
output(i,j) = k;
else
A = img([1:i+w,j-w:j+w]);
k = mean( img ,'all');
output(i,j) = k;
end
elseif (i+w)>row
if (j-w)<1
A = img([i-w:row,1:j+w]);
k = mean( img ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([i-w:row,j-w:col]);
k = mean( img ,'all');
output(i,j) = k;
else
A = img([i-w:row,j-w:j+w]);
k = mean( img ,'all');
output(i,j) = k;
end
else
if (j-w)<1
A = img([i-w:i+w,1:j+w]);
k = mean( img ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([i-w:i+w,j-w:col]);
k = mean( img ,'all');
output(i,j) = k;
else
% mean() of a 2D array is a vector
k = img(i-w:i+w,j-w:j+w);
output(i,j) = mean(k(:));
end
end
end
end
% using (:) in this assignment screws up class inheritance
% you end up with a 'double' image with incorrectly-scaled data
output = uint8(round(output-1));
I don't know why you would be getting that error
More Answers (0)
See Also
Categories
Find more on Characters and Strings 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!