To find the occurences

조회 수: 1 (최근 30일)
Mark Wood
Mark Wood 2021년 5월 23일
편집: DGM 2021년 5월 24일
How to find the occurences,
For example: For the given matrix, our threshold conditon is given as 4
We need to find the number of coherent elements of each element present in the matrix (In this example there are only 3 elements ie. 0,1,2)
Now for element 0, we need to find number of connecting 0's chain whose length is equal to or greater than out threshold given value.
Now, as we have 2 chains of connecting 0's, So, the number of coherent elements of 0 is 8(which is count of elements in chain)
Now, Similarily for element 1,
Now, here as we have only 1 chain of connecting 1's, So the number of coherent elements of 1 is 8
Now, finally for element 2,
Now, in this we have 2 chain of connecting 2's, But the number of coherent elements of 2 will be 6 as the count of elements in left chain is less than our threshold value.
At last display all the number of elements with their respective number of chains and number of coherent elements.
So our final answer should become this,
***********************************************************************************************
NOTE : If there is only 1 occurence of an element, consider that also a seperate chain.
For eg, If there is a single element 3 in the above matrix, then we will have 1 chain(only single element in that chain) of connecting 3's, But the number of Number of coherent elements of 3 will be 0 as this do not satisfy our threshold value condition.
************************************************************************************************
Please help with the MATLAB code of this approach.
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 5월 23일
See regionprops() and bwareafilt()
... for any one value, finding the matching chains can be done as a single expression. Doing everything in single expression would require using a helper anonymous function.

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

채택된 답변

DGM
DGM 2021년 5월 23일
편집: DGM 2021년 5월 24일
You could use image processing tools easily enough: Since this appears to be using 8-connectivity:
A = [1 1 1 1 1;
2 1 1 2 2;
2 0 2 1 0;
2 0 2 2 0;
0 0 2 0 0];
threshold = 4;
U = unique(A); % find unique elements of A
objectcount = zeros(numel(U),1);
totalarea = zeros(numel(U),1);
for c = 1:numel(U)
S = regionprops(A==U(c),'area');
area = [S.Area];
objectcount(c) = numel(area); % obj count before truncation
totalarea(c) = sum(area(area>=threshold));
end
% display results
table(U,objectcount,totalarea)
ans = 3×3 table
U objectcount totalarea _ ___________ _________ 0 2 8 1 1 8 2 2 6
EDIT: I adapted this to be a bit more generalized, automatically finding unique elements and building masks as needed.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by