How can I change the intensities of a certain pixel in an image along with 3*3 surrounding neighbor pixels?

조회 수: 1 (최근 30일)
Lets say I have a image(matrix) as following:
a =
1 1 1 1 1
1 1 1 1 1
1 2 2 2 1
1 2 0 2 1
1 2 2 2 1
1 1 1 1 1
1 1 1 1 1
Finding that 0 in the middle and changing it is easy. I can do this :
a(a==0)=4;
and the result will be this:
a =
1 1 1 1 1
1 1 1 1 1
1 2 2 2 1
1 2 4 2 1
1 2 2 2 1
1 1 1 1 1
1 1 1 1 1
Now I want a code that can find the zero, convert that zero to 4, and also convert the 3*3 neighbors of zero to 4 as well. So after the code that you're suggesting, my output should look something like this:
b =
1 1 1 1 1
1 1 1 1 1
1 4 4 4 1
1 4 4 4 1
1 4 4 4 1
1 1 1 1 1
1 1 1 1 1
thanks in advance, Sina

채택된 답변

Matt J
Matt J 2016년 9월 21일
a( imdilate(a==0, true(3)) ) = 4;
  댓글 수: 3

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2016년 9월 21일
[x, y] = find( a == 0 );
a( x-1:x+1, y-1:y+1 ) = 4;
Obviously you would need to deal with when you are on the border and there is no x-1 or y+1 or whatever, but that is easy enough for you to adapt.
  댓글 수: 1
Matt J
Matt J 2016년 9월 21일
Changoleon commented:
Adam,
Fast and accurate answer. Thanks you. I just realized that I need to change my values circularly which is impossible to make a perfect circle inside a matrix ( since you can not cover half of an element). How to change it so it look like a hexagon or octagon?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by