cc2bw
Description
Examples
Read and display an image.
I = imread("blobs.png");
imshow(I)
Create a connected components structure.
CC = bwconncomp(I);
Filter the structure and keep round objects. Display the filtered image.
CC = bwpropfilt(CC,"Circularity",[0.7 1]);
imshow(cc2bw(CC))
Filter the structure again and keep large objects. Display the filtered image.
CC = bwpropfilt(CC,"Area",[20 Inf]);
imshow(cc2bw(CC))
Read a binary image and detect the connected components.
BW = imread("text.png");
CC = bwconncomp(BW);
Measure the area of each connected component and return the results as a table.
p = regionprops("table",CC,"Area");
Create a binary image that contains only the 2nd through 10th largest connected components. Display the result.
[~,idx] = sort(p.Area,"descend");
BWfilt = cc2bw(CC,ObjectsToKeep=idx(2:10));
imshow(BWfilt)
Read a grayscale image of grains of rice, then convert the image to binary.
I = imread("rice.png");
BW = imbinarize(I);
imshow(BW)
Measure the area and bounding box of each region.
CC = bwconncomp(BW); stats = regionprops("table",CC,"Area","BoundingBox");
Select regions for whom these conditions apply:
The area is greater than 50 pixels
The bounding box is less than 15 pixels wide and is greater than or equal to 20 pixels tall.
area = stats.Area; bbox = stats.BoundingBox; selection = (area > 50) & (bbox(:,3) < 15) & (bbox(:,4) >= 20); BW2 = cc2bw(CC,ObjectsToKeep=selection);
Display the filtered image.
imshow(BW2)
Input Arguments
Connected components (objects), specified as a structure with four fields.
Field | Description |
---|---|
Connectivity | Connectivity of the connected components |
ImageSize | Size of the binary image |
NumObjects | Number of connected components in the binary image. |
PixelIdxList | 1-by-NumObjects cell array where the
k-th element in the cell array is a vector containing the
linear indices of the pixels in the k-th object. |
Objects to keep, specified as one of these values.
Positive integer or vector of positive integers — Keep the object or objects whose index is included in
objectsToKeep
. The length ofobjectsToKeep
is less than or equal toCC.NumObjects
.Logical vector — Keep the objects whose corresponding element in
objectsToKeep
istrue
. The length ofobjectsToKeep
must be equal toCC.NumObjects
.
Output Arguments
Binary image, returned as a logical array of the same size as
CC.ImageSize
.
Data Types: logical
Extended Capabilities
Usage notes and limitations:
cc2bw
supports the generation of C and C++ code (requires MATLAB® Coder™). For more information, see Code Generation for Image Processing.If you generate code for the
cc2bw
function directly, thenCC.PixelIdxList
must be homogeneous. This code shows how to makeCC.PixelIdxList
homogeneous by using themakeHomogeneous
(MATLAB Coder) function.CC = bwconncomp(BW); CC2 = bwpropfilt(CC,"Area",10); typeCC = coder.typeof(CC2); typeCC.Fields.PixelIdxList = makeHomogeneous(typeCC.Fields.PixelIdxList); codegen cc2bw -args {typeCC}
If you generate code for a wrapper function, then you do not need to make
CC.PixelIdxList
homogeneous. For example, this command generates code for a wrapper function namedmyFunction
.codegen myFunction -args {BW}
Here is a sample definition for the wrapper function
myFunction
that includes a call to thecc2bw
function.function out = myFunction(BW) %#codegen CC = bwconncomp(BW); CC2 = bwpropfilt(CC,"Area",10); out = cc2bw(CC2); end
Version History
Introduced in R2024acc2bw
now supports the generation of
C code (requires MATLAB
Coder).
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)