How can I find and replace a pattern inside a matrix
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ricard molins
le 21 Mai 2015
Modifié(e) : James Tursa
le 21 Mai 2015
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 commentaires
Réponse acceptée
Thorsten
le 21 Mai 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 commentaires
Plus de réponses (1)
Joseph Cheng
le 21 Mai 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 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!