I'm working on an example where I'm getting a logical error possibly. I want to replace every element of a matrix by the minimum of its neighborhood.

1 回表示 (過去 30 日間)
I've written the following piece of code. But it's not running and giving an error which I'm unable to figure out.
z = rgb2gray(imread('gantrycrane.png')); figure, imshow(z)
for i = 2:263
N = 0;
for j = 2:399
for k = (i-1)+N:(i+1)+N
for l = j-1:j+1
if(z(k,l)>z(i,j))
z(i,j)=z(k,l);
end
end
end
N = N+1;
end
end

採用された回答

Simon Chan
Simon Chan 2021 年 8 月 21 日
The value of N will be very large in the loop and gives an error.
Try the following to replace each element of the matrix by by the minimum value of its neighborhood (including the element itself).
for r = 2:263
for c = 2:399
region = z(r-1:r+1,c-1:c+1); % Extract the neighborhood for each element
z(r,c) = min(min(region));
end
end
  6 件のコメント
Simon Chan
Simon Chan 2021 年 8 月 21 日
Thanks for your comment, please accept the answer if you consider my reply useful.
Muhammad Hamza Shahid
Muhammad Hamza Shahid 2021 年 8 月 21 日
It's my first time on Mathworks, I didn't know how it worked. Otherwise, I'll accept it 100 times.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by