Filter löschen
Filter löschen

[Ask Help] - Erode/Erosion Matrix

4 Ansichten (letzte 30 Tage)
Hilmi
Hilmi am 10 Dez. 2022
Bearbeitet: DGM am 10 Dez. 2022
I've script and matrix 20x20 below, and i would like to search Erode/Erosion.
a = [1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0;
1 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0;
1 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0;
0 0 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1;
0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 1;
1 0 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 1 0 1;
1 1 0 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1;
1 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 1 1 0;
0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0;
0 0 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 1 0;
0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 1 1 1 0;
1 0 0 1 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0;
1 1 1 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 0 0;
0 1 1 1 0 1 0 0 1 1 1 0 0 1 1 0 0 1 1 1;
0 1 1 0 0 1 0 0 1 1 1 0 1 1 1 0 0 1 1 1;
0 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 1 1 1 1;
1 1 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 1 0;
1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0];
b = [1 1 1;
0 1 0;
1 1 1];
What should i do?
Thanks....

Antworten (2)

Walter Roberson
Walter Roberson am 10 Dez. 2022
a = [1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0;
1 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0;
1 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0;
0 0 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1;
0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 1;
1 0 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 1 0 1;
1 1 0 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1;
1 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 1 1 0;
0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0;
0 0 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 1 0;
0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 1 1 1 0;
1 0 0 1 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0;
1 1 1 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 0 0;
0 1 1 1 0 1 0 0 1 1 1 0 0 1 1 0 0 1 1 1;
0 1 1 0 0 1 0 0 1 1 1 0 1 1 1 0 0 1 1 1;
0 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 1 1 1 1;
1 1 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 1 0;
1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0];
b = [1 1 1;
0 1 0;
1 1 1];
diffcount = nlfilter(a, [3 3], @(block) nnz(block~=b))
mindiff = min(diffcount(:));
[r, c] = find(diffcount == mindiff)
bestmatches = arrayfun(@(R,C) a(R:R+2, C:C+2), r, c, 'uniform', 0)
There is no exact match, but there are places where there is only 1 difference.

DGM
DGM am 10 Dez. 2022
Bearbeitet: DGM am 10 Dez. 2022
If the goal is to look for a certain neighborhood, you can use bwlookup() for this.
A = [1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0;
1 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0;
1 1 1 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0;
0 0 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1;
0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 1;
1 0 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 1 0 1;
1 1 0 1 1 1 1 0 1 0 1 0 0 0 1 0 0 0 0 1;
1 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 1 1 0;
0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0;
0 0 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 1 0;
0 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 1 1 1 0;
1 0 0 1 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 0;
1 1 1 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 0 0;
0 1 1 1 0 1 0 0 1 1 1 0 0 1 1 0 0 1 1 1;
0 1 1 0 0 1 0 0 1 1 1 0 1 1 1 0 0 1 1 1;
0 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 1 1 1 1;
1 1 0 0 0 1 1 0 0 1 1 1 0 0 0 1 0 0 1 0;
1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 0];
b = [1 1 1;
0 1 0;
1 1 1];
% find matches
f = @(x) isequal(x,b);
lut = makelut(f,3);
B = bwlookup(A,lut);
% compare the input/output
% note that the 'I' pattern is present at the two match locations
imshow(imfuse(A,B))
If instead of looking only for exact matches, you wanted to count the number of matching pixels in any given nhood, you can just use a different definition of f.
f = @(x) nnz(x==b); % the number of matched pixels (9 is a full match)
If you don't want to use bwlookup(), you can do the prior matching with a basic linear filter. Note that the inputs to imfilter() must be numeric class (not logical). Cast them as necessary. This could also be done with conv2() instead of imfilter().
seb = 2.^([1 4 7; 2 5 8; 3 6 9]-1); % index weighting array
B = imfilter(A,seb) == sum(sum(seb.*b)); % logical map of matches
% compare the input/output
imshow(imfuse(A,B))
If the goal is to erode the image using b as a structuring element, I guess you could do that instead.
% do erosion instead of pattern searching
C = imerode(A,b);
imshow(C)

Community Treasure Hunt

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

Start Hunting!

Translated by