nested if-else inside nested if-else

5 ビュー (過去 30 日間)
Kazmir Fahrier
Kazmir Fahrier 2021 年 5 月 13 日
コメント済み: Kazmir Fahrier 2021 年 5 月 15 日
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));
Kazmir Fahrier
Kazmir Fahrier 2021 年 5 月 15 日
I have solved it.Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by