Image area search

I used the marking algorithm for connected components (bwconncomp) to indicate different parts of a binary image (MATLAB). Now I need to calculate the area of โ€‹โ€‹different labels and remove labels with a smaller area. Can I use the default search command or is there any specific commands for this in matlab ... Help ..

+4
source share
3 answers

In the documentation:

CC = bwconncomp (BW) returns the connected CC components found in BW. The BW binary image can have any dimension. CC - structure with four fields ...

The final field in CC is PixelIdxList , which is:

[a] array of 1-by-NumObjects cells, where the k-th element in the cell array is a vector containing linear pixel indices in the k-th object.

You can find the area of โ€‹โ€‹each label by looking at the length of the corresponding record in the array of cells. Sort of:

 areas_in_pixels = cellfun(@length, CC.PixelIdxList); 

A PixelIdxList is an array of cells, each of which contains linear indexes of pixels present in this connected component. The line of code above defines the length of each cell in an array of cells - that is, the number of pixels in each connected component.

I used cellfun to make the code short and efficient. Another way to write the same thing:

 areas_in_pixels = nan(1, length(CC.PixelIdxList); for i = 1:length(CC.PixelIdxList) areas_in_pixels(i) = length(CC.PixelIdxList{i}); end 

For each connected component, you can find the size of this component in pixels by accessing the element in the injection_ areas:

 areas_in_pixels(34) %# area of connected component number 34 
+9
source

If you do not want to write a lot of code, as indicated above, just use the MATLAB built-in functions to define the area. Designate your components and properties of the component, you can find out the area of โ€‹โ€‹this component. Let Bw be a binary image:

 [B,L] = bwboundaries(Bw,'noholes'); stats = regionprops(L,'Area','perimeter'); for k = 1:length(B) area(k)=stats.Area; end 
+4
source

You can do this even better by avoiding the for loop with the following:

 [B,L] = bwboundaries(Bw,'noholes'); stats = regionprops(L,'Area','perimeter'); area = [stats.Area]; 

Best, -Will

+2
source

Source: https://habr.com/ru/post/1393795/


All Articles