Info

This question is closed. Reopen it to edit or answer.

Why are these codes NOT giving 4 biggest connected component in the stack image ???

2 views (last 30 days)
Hi,
I need your help. I have a tiff image that includes 7 slices. I want to find 4 biggest connected component in the tiff image. These components might be in one or two slices; Or each of these components might be in different slices. Therefore, I used bwlabeln to label components in the tiff image and wrote the following codes to get the first biggest connected component (If I find the first connected component then I apply the same codes to find the 2nd, 3th and 4th biggest connected component). But it does not give correct result
Codes are,
[temp77 NumberOfConnComps] = bwlabeln(imgStack);
for j=1:NumberOfConnComps
temp88 = temp77==j;
part(j)=sum(temp88(:));
end
big=max(part);
k2=find(part==big);
biggestPart=temp77==k2;
However, the above codes do NOT give the first biggest connected component. When I write the following codes to see the 1st biggest connected component, then I see that it includes other components :(
for slice=1:7
if (slice == 1)
imwrite(squeeze(biggestPart(:,:,slice)),['biggestPart.tif'],'tif', 'WriteMode', 'overwrite','Compression', 'none');
else
imwrite(squeeze(biggestPart(:,:,slice)),['biggestPart.tif'],'tif', 'WriteMode', 'append','Compression', 'none');
end
end
WHY is it giving false result ? How to solve this problem ?
Urgent help please
Thanks

Answers (2)

S C.Carl
S C.Carl on 9 Jan 2016
I added these codes, but the result does not changed;
for slice=1:7
if temp77(:,:,slice)==k2
biggestPart = temp77(:,:,slice)==k2;
if (slice == 1)
imwrite(squeeze(biggestPart(:,:,slice)),['biggestPart.tif'],'tif', 'WriteMode', 'overwrite','Compression', 'none');
else
imwrite(squeeze(biggestPart(:,:,slice)),['biggestPart.tif'],'tif', 'WriteMode', 'append','Compression', 'none');
end
end
end
When I look at 2D slices separately, I can see components (there are 49 connected components). However, when I run the above codes and open the saved "biggestPart.tif" image, then I saw small connected components. The above codes should give only one (the first biggest connected) component. Right ? What is the wrong in the above codes ?

Image Analyst
Image Analyst on 9 Jan 2016
What you want to do is use bwareafilt(). You can get the largest blob this way
bw1 = bwareafilt(imgStack, 1, 'largest');
Then write it out or do whatever you're going to do with it. Then to get the second largest blob, etc., all by itself in a 3D image, you'd do
bw2 = bwareafilt(imgStack - BW1, 1, 'largest'); % Get second largest blob.
bw3 = bwareafilt(imgStack - BW2, 1, 'largest'); % Get third largest blob.
bw4 = bwareafilt(imgStack - BW3, 1, 'largest'); % Get fourth largest blob.
Another way is to get all the areas, sort them, and then extract them in order with ismember():
% Measure all the volumes.
measurements = regionprops(temp77, 'Area');
% Get all areas
allAreas = [measurements.Area];
% Sort them in descending order
[sortedVolumes, sortIndices] = sort(allAreas, 'Descend');
% Now relabel the labeled image with intlut
labeledImage = intlut(temp77, sortIndices);
% Now get largest blobs
big1 = ismember(labeledImage, 1);
big2 = ismember(labeledImage, 2);
big3 = ismember(labeledImage, 3);
big4 = ismember(labeledImage, 4);
Not sure which way is faster. I have not tested either - it's just off the top of my head so if you run into problems, ask.
  4 Comments
S C.Carl
S C.Carl on 11 Jan 2016
Dear ImageAnalyst,
Thanks for your support, I solved the problem with your help. I will upgrade the matlab version as soon as possible Thanks again Best wishes
Image Analyst
Image Analyst on 11 Jan 2016
If the problem is now solved, and I helped with that, then could you "Accept this answer" to give me credit? Thanks in advance.

Community Treasure Hunt

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

Start Hunting!