How can I find and replace a pattern inside a matrix
1 view (last 30 days)
Show older comments
I have a binary matrix with large zones of white "1". I need to crop the white zones by one pixel. For example
A =[
0 0 0 0 0 ;
0 1 1 1 0 ;
0 1 1 1 0 ;
0 1 1 1 0 ;
0 1 1 1 0 ;
0 0 0 0 0 ;
]
then after
B = crop1white(A)
B =[
0 0 0 0 0 ;
0 0 0 0 0 ;
0 0 1 0 0 ;
0 0 1 0 0 ;
0 0 0 0 0 ;
0 0 0 0 0 ;
]
I was thinking in doing something like find(01) and then at the index changing the value. However I have the feeling this is not the best way to proceed.
0 Comments
Accepted Answer
Thorsten
on 21 May 2015
The is the morphological operation 'erode' on black-and-white images. If you have the image processing toolbox, you can use
B = bwmorph(A, 'erode')
0 Comments
More Answers (1)
Joseph Cheng
on 21 May 2015
Well in the case that you do not have the image processing toolbox
A =[
0 0 0 0 0 ;
0 0 1 1 0 ;
0 1 1 1 0 ;
0 1 1 0 0 ;
0 0 1 1 0 ;
0 0 0 0 0 ;
];
B = ones(3,3)/9;
B=conv2(A,B,'same');
B(B<.75)=0;
B(B>=.75)=1
so by using the convolution we can determine the edges or portions of the center. Then by adjusting the threshold in the last two lines you can figure out how many 1's need to be in the rolling 3x3 window before the center value changes to a 1 or 0.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!