How to creat a structure out of a non-fixed cell?
1 次查看(过去 30 天)
显示 更早的评论
I have a binary image that I want to omit objects which their length/width ratio is more than a threshold (let's assume threshold=7). First, I used codes below to extract all connected components and major and minor axis length:
labeledImage = bwlabel(my_image, 8);
Length = regionprops(labeledImage , 'MajorAxisLength');
Width = regionprops(labeledImage , 'MinorAxisLength');
Then I calculated the length/width ratio for each component and placed them in matrix X_mat
L_cell=struct2cell(Length); % convert structure "Length" to a cell
W_cell=struct2cell(Width); % convert structure "Width" to a cell
L_mat=cell2mat(L_cell); % convert cell "Length" to a matrix
W_mat=cell2mat(W_cell); % convert cell "Width" to a matrix
X_mat=(L_mat./W_mat); % calculate the ratio (Length/Width)and put
them in a matrix named "X_mat"
My idea was to convert matrix X_mat to a structure named X_struct, and then replace values of Length structure with those of X_struct. So I could use the codes I copied (from Image Segmentation Tutorial by Image Analyst) to extract desired objects.
M=ones(1,length(L_mat));
X_cell=mat2cell(X_mat,1,M); % convert the matrix "X_mat" to a cell named
"X_cell"
I tried to produce field names for X_struct using a for loop:
the names are supposed to be like this: length(1,1), length(1,2)...and so on to length(1,length(L_mat))
for jj=1:length(L_mat)
field_name{1,jj}=sprintf('length(1,%d)',jj);
end
%[. . . in here I need to convert the cell "X_cell" to the structure "X_struct". I'm stuck in this part! . . .]
% the followings are the coppied codes:
All_Building_Ratio=[Length.MajorAxisLength];
Allowable_Ratio_Index=(All_Building_Ratio<7);
Keeper_Indexes=find(Allowable_Ratio_Index);
Keeper_Building_Image=ismember(labeledImage,Keeper_Indexes);
figure,imshow(Keeper_Building_Image);title('thresholded image by R');
1. How can I exactly create a structure X_struct out of the cell X_cell ???
X_struct must be exactly the same as the Length structure (which I assume is a 1-by-1 structure with p=length(L_mat) fields).
My every attempt in this part ends up with an error!!!
2. Is this idea practical at all??
Aren't there any better ways to extract objects from an image according to their length/width ratio?
Thanks in advance
0 个评论
采纳的回答
Walter Roberson
2013-9-16
Might I suggest,
regioninfo = regionprops(labeledImage , 'MajorAxisLength', 'MinorAxisLength', 'PixelIDList');
lengths = [regioninfo.MajorAxisLength]; %array
widths = [regioninfo.MinorAxisLength]; %array
logidx = (lengths ./ widths) > threshold;
extracted_objs = regioninfo(logidx);
then you can use the PixelIDList values to show the parts remaining.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!