comparing 3x3 block with center pixel
이전 댓글 표시
i have a matrix as shown below

i wanted to take 3x3 pixel and take 3 pixels at a time along with the center pixel,
and compare the selected 3 pixel value with the center pixel,
and if 2 or morepixel has value greater than the center pixel and i wanted to assign 1 to it else 0
So in this case i will get 0-1-1-0 and then convert the binary 0110 to its corresponding decimal value = 6
Then the next 3x3 pixel

댓글 수: 4
Matt J
2019년 11월 20일
Do you take the 3x3 blocks in non-overlapping tiles or is it a sliding window?
Elysi Cochin
2019년 11월 20일
KALYAN ACHARJYA
2019년 11월 20일
You did not answered the @Matt J question.
Also is the start pixel at 70 or 200?
What about boundary pixels?
Elysi Cochin
2019년 11월 20일
채택된 답변
추가 답변 (1개)
KALYAN ACHARJYA
2019년 11월 20일
편집: KALYAN ACHARJYA
2019년 11월 20일
Self Declaration: Odd Way but simpler
for i=2:Row-1
for j=2:Col-1
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i,j-1)+mat(i,j)<mat(i+1,j-1))=>2
data(1)=1;
else
data(1)=0;
end
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i-1,j+1))=>2
data(2)=1;
else
data(2)=0;
end
if (mat(i,j)<mat(i+1,j-1)+mat(i,j)<mat(i_1,j)+mat(i,j)<mat(i+1,j+1))=>2
data(3)=1;
else
data(3)=0;
end
if (mat(i,j)<mat(i-1,j+1)+mat(i,j)<mat(i,j+1)+mat(i,j)<mat(i+1,j+1))=>2
data(4)=1;
else
data(4)=0;
end
mat(i,j)=bin2dec(logical(data));
end
end
댓글 수: 7
Elysi Cochin
2019년 11월 20일
편집: Elysi Cochin
2019년 11월 21일
KALYAN ACHARJYA
2019년 11월 20일
mat = [70 20 100 24 89; 230 200 220 78 55; 210 80 10 57 34; ...
110 94 67 88 28; 32 36 89 78 58; 59 73 54 68 48];
[Row, Col] = size(mat);
for i=2:Row-1
for j=2:Col-1
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i,j-1)+mat(i,j)<mat(i+1,j-1))>=2
data(1)=1;
else
data(1)=0;
end
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i-1,j+1))>=2
data(2)=1;
else
data(2)=0;
end
if (mat(i,j)<mat(i+1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i+1,j+1))>=2
data(3)=1;
else
data(3)=0;
end
if (mat(i,j)<mat(i-1,j+1)+mat(i,j)<mat(i,j+1)+mat(i,j)<mat(i+1,j+1))>=2
data(4)=1;
else
data(4)=0;
end
mat(i,j)=bi2de(logical(data))
end
end
KALYAN ACHARJYA
2019년 11월 20일
편집: KALYAN ACHARJYA
2019년 11월 20일
use Logical, no need to combine separately
Jan
2019년 11월 20일
Easier:
for i=2:Row-1
data = zeros(1, 4);
for j=2:Col-1
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i,j-1)+mat(i,j)<mat(i+1,j-1))>=2
data(1) = 8;
end
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i-1,j+1))>=2
data(2) = 4;
end
if (mat(i,j)<mat(i+1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i+1,j+1))>=2
data(3) = 2;
end
if (mat(i,j)<mat(i-1,j+1)+mat(i,j)<mat(i,j+1)+mat(i,j)<mat(i+1,j+1))>=2
data(4) = 1;
end
mat(i,j) = sum(data);
end
end
I think Jan's modification would really need to be as follows,
out=zeros(size(mat));
data = zeros(1, 4);
for i=2:Row-1
for j=2:Col-1
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i,j-1)+mat(i,j)<mat(i+1,j-1))>=2
data(1) = 8;
end
if (mat(i,j)<mat(i-1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i-1,j+1))>=2
data(2) = 4;
end
if (mat(i,j)<mat(i+1,j-1)+mat(i,j)<mat(i-1,j)+mat(i,j)<mat(i+1,j+1))>=2
data(3) = 2;
end
if (mat(i,j)<mat(i-1,j+1)+mat(i,j)<mat(i,j+1)+mat(i,j)<mat(i+1,j+1))>=2
data(4) = 1;
end
out(i,j) = sum(data); data(:)=0;
end
end
Jan
2019년 11월 21일
@Matt J: You are right, modifying mat inside the loop, which uses mat as input also is not wanted. If you declare data before the loops and reset the values to 0 by data(:)=0 or if it is recreated by zeros(1,4) does not really matter. For large arrays one of the methods might be faster than the other.
Matt J
2019년 11월 21일
@Jan: It does matter however, that you were recreating data outside the j-loop. It needs to be reset inside both loops after each (i,j) is processed.
카테고리
도움말 센터 및 File Exchange에서 Lengths and Angles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!