comparing 3x3 block with center pixel

1 回表示 (過去 30 日間)
Elysi Cochin
Elysi Cochin 2019 年 11 月 13 日
コメント済み: Matt J 2019 年 11 月 21 日
i have a matrix as shown below
Untitled.png
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
Untitled1.png
  4 件のコメント
KALYAN ACHARJYA
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
Elysi Cochin 2019 年 11 月 20 日
same as in LBP code
I need overlapping 3x3 blocks
So the above for loop goes overlapping right?
Boundary pixels also needed, i wrote so, thinking to take 3x3 pixel, i can do i-1, j-1, i, j, i+1, j+1
Am i wrong? Or should i rewrite the for loop as
for i=1:Row
for j=1:Col
%"computation"
end
end

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

採用された回答

Matt J
Matt J 2019 年 11 月 20 日
編集済み: Matt J 2019 年 11 月 20 日
Here, X is the input matrix.
result=0;
result=result+1*rot90(fkernel(rot90(X,+1)),-1);
result=result+2*rot90(fkernel(rot90(X,+2)),-2);
result=result+4*fkernel(X);
result=result+8*rot90(fkernel(rot90(X,-1)),+1);
function Y=fkernel(X)
[m,n]=size(X);
Y=nan(m-2,n-2);
for i=1:n-2
B=X(:,i:i+2);
Y(:,i)=sum(B(1:m-2,:)>=B(2:m-1,2),2)>=2;
end
end

その他の回答 (1 件)

KALYAN ACHARJYA
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 件のコメント
Jan
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
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.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by