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

2 次查看(过去 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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by