nested if-else inside nested if-else

4 次查看(过去 30 天)
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".

采纳的回答

DGM
DGM 2021-5-13
编辑:DGM 2021-5-13
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
  2 个评论
Kazmir Fahrier
Kazmir Fahrier 2021-5-15
Sorry for being late.You are right.But I could not understand why the type conversion didn't happen
%question
% 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(A ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([1:i+w,j-w:col]);
k = mean( A ,'all');
output(i,j) = k;
else
A = img([1:i+w,j-w:j+w]);
k = mean( A ,'all');
output(i,j) = k;
end
elseif (i+w)>row
if (j-w)<1
A = img([i-w:row,1:j+w]);
k = mean( A ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([i-w:row,j-w:col]);
k = mean( A ,'all');
output(i,j) = k;
else
A = img([i-w:row,j-w:j+w]);
k = mean( A ,'all');
output(i,j) = k;
end
else
if (j-w)<1
A = img([i-w:i+w,1:j+w]);
k = mean( A ,'all');
output(i,j) = k;
elseif (j+w)>col
A = img([i-w:i+w,j-w:col]);
k = mean( A ,'all');
output(i,j) = k;
else
A = img([i-w:i+w,j-w:j+w]);
k = mean(A, 'all');
output(i,j) = k;
end
end
end
end
output(:) = uint8(round(output(:)-1));

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by