フィルターのクリア

how to adress neighbours in matrix

2 ビュー (過去 30 日間)
Jakub
Jakub 2013 年 11 月 4 日
コメント済み: Jakub 2013 年 11 月 4 日
Imagine I have matrix A=ones(5). I want to change center of the matrix to 17, A(3,3)=17; In next iteration I want to change all points that are next to this 20 to 5. i will have:
1 1 1 1 1
1 5 5 5 1
1 5 17 5 1
1 5 5 5 1
1 1 1 1 1
In next step i want to change all ones to 10. And so on...
10 10 10 10 10
10 5 5 5 10
10 5 17 5 10
10 5 5 5 10
10 10 10 10 10
Any idea how to do this to large matrix? How to adress only the neighbours but not the points "inside"?

採用された回答

Image Analyst
Image Analyst 2013 年 11 月 4 日
This works for any size matrix, large or not, as long as it's bigger than 5x5. Also you can't get a center element if there is an even number of rows or columns.
A = ones(9)
centerRow = ceil(size(A, 1)/2)
centerCol = ceil(size(A, 2)/2)
A(centerRow-2:centerRow+2, centerCol-2:centerCol+2) = 20;
A(centerRow-1:centerRow+1, centerCol-1:centerCol+1) = 10;
A(centerRow, centerCol) = 17
  5 件のコメント
Image Analyst
Image Analyst 2013 年 11 月 4 日
編集済み: Image Analyst 2013 年 11 月 4 日
You'd have to set the 4 "sides" one at a time. For example to set the outlines at 2 away from the middle:
A(centerRow-2, centerCol-2:centerCol+2) = 20; % Top line
A(centerRow+2, centerCol-2:centerCol+2) = 20; % Bottom line
A(centerRow-2:centerRow+2, centerCol-2) = 20; % Left line
A(centerRow-2:centerRow+2, centerCol+2) = 20; % Right line
If they're not perfect rectangles, but irregular shapes, or if you want the dot to expand out to an octagon or something, then you'd have to use mathematical morphological dilation with a structuring element of the desired shape. See bwmorph() in the Image Processing Toolbox.
Or you could do it by computing the distance transform with bwdist() and then thresholding.
Jakub
Jakub 2013 年 11 月 4 日
Thank you very much.

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by