Merge the contents of neighboring non-zero cells

조회 수: 3 (최근 30일)
LL
LL 2021년 11월 30일
댓글: LL 2021년 12월 2일
Hi i have only one cell array and, what i would like to do is to merge the contents of neighboring non-zero cells.
So, in the figure all the contents of the groups with the blue outline will be "merged" into one cell.
i.e. in the final cell array in the col n.8 row n.6 it will be [9;9;9;9;1;1;.....9;2;1;1;2;1;......2;1;9;2;9;9;.....].
Thanks

답변 (2개)

Image Analyst
Image Analyst 2021년 11월 30일
편집: Image Analyst 2021년 11월 30일
Why do you need to to this? I'm thinking you think you need to do this to accomplish some task, but you really don't. That's why I was wanting to know the use case -- what you're going to do once you have this.
It's possible but not easy. You'd have to get a map of what cells are non-zero, then label that map with bwlabel() to determine what cells are contiguous. Then you could identify the upper left cell (if that's what you want) of each region, and replace it with the concatenation of all the other cells inside that contiguous region. Then replace the other cells with 0 (I guess).
So it is possible but a bit of work so I need to know if it's really necessary.
Here's start, but let's hope you don't really need this:
[rows, columns] = size(ca); % Get size of the cell array variable, ca.
binaryImage = true(rows, columns);
for col = 1 : columns
for row = 1 : rows
cellContents = ca{row, col};
if isequal(cellContents, 0)
binaryImage(row, col) = false;
end
end
end
[labeledImage, numRegions] = bwlabel(binaryImage);
for k = 1 : numRegions
thisRegion = ismember(labeledImage, k);
[r, c] = find(thisRegion);
% Collect contents of all cells in this region and concatenate them.
% You do this. Loop over all cells defined by r and c getting contents and concatenating them.
% Then store result into ca(r(1), c(1));
% You do this. Something like ca{r(1), c(1)} = concatenatedResults.
% Other cells set to zero if you want.
end
  댓글 수: 10
LL
LL 2021년 12월 2일
In (6,8) i would like [9;9;9;9;1;1;.....9;2;1;1;2;1;......2;1;9;2;9;9;.....] so the merge of the three contigent cells, in the other two cells (6,9) and (7,9) only 0; the same practice for the other aggregate of non zero cells because they don't have the same concatenated vector.
LL
LL 2021년 12월 15일
Hi @Image Analyst, I thought I could check the LabelIm array, especially for the first loop I initialize number = 1 then I check all cells = 1, find their rows and columns and get their value from CIRC_agg when I have all the contents of the contingent cells I have concatenated them and studied the contents to verify if the aggregates are internal, external or boundary of hydroxypatite.
I show you the code.
[etichettaIm, numAgg] = bwlabel(binaryIm);
contenuto=zeros();
numero=1
for k=1:size(etichettaIm)
regione=ismember(etichettaIm,numero);
[r, c]=find(regione);
for y=1:r
for i=1:c
%%%i would like to cycling but i don't know how i tried with cont=[CIRC_agg(r(y),c(i))]; without results
cont=[CIRC_agg(r(1),c(1)),CIRC_agg(r(2),c(2)),CIRC_agg(r(3),c(3))];
contenuto=cat(1, cont{:});
for j=1:length(contenuto)
idrox=contenuto(find(contenuto==2));
perc_idrox=(length(idrox)*100)/length(contenuto);
%%%%%%%%%%%%
%%%EXTERNAL%%%
%%%%%%%%%%%%
if length(idrox)==0
WHI_ext_agg=0;
WHI_ext_agg=WHI_ext_agg+1;
%%%%%%%%%%%%
%%%INTERNAL%%%
%%%%%%%%%%%%
elseif perc_idrox>65
WHI_int_agg=0;
WHI_int_agg=WHI_int_agg+1;
%%%%%%%%%%%%%%
%%%BOUNDARIES%%%
%%%%%%%%%%%%%%
else
WHI_bound_agg=0;
WHI_bound_agg= WHI_bound_agg+1;
end
numero=numero+1;
%numero=numero+1;
if numero>numAgg
fprintf('Non ci sono più aggregati da controllare')
end
end
end
end
end

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


Awais Saeed
Awais Saeed 2021년 11월 30일
Remove the cells that has 0 in it.
c = {25,0,'anyString','aChar',[12 5 48 7];0,0,'anyString','bChar',[10 01]}
c = 2×5 cell array
{[25]} {[0]} {'anyString'} {'aChar'} {[12 5 48 7]} {[ 0]} {[0]} {'anyString'} {'bChar'} {[ 10 1]}
nonzero_indices = cellfun(@(x) ~isequal(x, 0), c);
M = c(nonzero_indices)
M = 7×1 cell array
{[ 25]} {'anyString'} {'anyString'} {'aChar' } {'bChar' } {[12 5 48 7]} {[ 10 1]}
  댓글 수: 1
LL
LL 2021년 11월 30일
Sorry @Awais Saeed, I need that the final array has the same size of the initial one.
So when i find a cell non-zero, i need to check around this cell to find and merge the contents of the other non-zero cells and put inside these one the 0 or NaN.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by