How can I make diamond shape with a matrix?

조회 수: 12 (최근 30일)
Coeing
Coeing 2019년 11월 25일
답변: Stephen23 2019년 11월 26일
I want to make diamond image with a matrix
ex. background image is zeros(1000,1000)
from center, make this matrix [1 2 ; 3 4] like a diamond..
.
.
.
0 0 1 2 0 0...
0 0 3 4 0 0
1 2 1 2 1 2
3 4 3 4 3 4
0 0 1 2 0 0
0 0 3 4 0 0 ...
.
.
.
plz help me.. thanks!!

답변 (3개)

Stephen23
Stephen23 2019년 11월 26일
Without the image toolbox:
>> N = 5;
>> V = round((N:-1:1)/(N+1));
>> M = toeplitz(V);
>> M = M & rot90(M)
M =
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
>> kron(M,[1,2;3,4])
ans =
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
1 2 1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4 3 4
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0

Andrei Bobrov
Andrei Bobrov 2019년 11월 25일
편집: Andrei Bobrov 2019년 11월 26일
a = strel('diamond',250);
out = kron(a.Neighborhood,[1 2 ; 3 4]);

Akira Agata
Akira Agata 2019년 11월 26일
How about the following?
N = 5; % <- Should be odd number
A = repmat([1 2;3 4],N);
se = strel('diamond',floor(N/2));
idx = imresize(se.Neighborhood,2);
A(~idx) = 0;
>> A
A =
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
1 2 1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4 3 4
0 0 1 2 1 2 1 2 0 0
0 0 3 4 3 4 3 4 0 0
0 0 0 0 1 2 0 0 0 0
0 0 0 0 3 4 0 0 0 0

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by