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 ofobjectsToKeepis less than or equal toCC.NumObjects.Logical vector — Keep the objects whose corresponding element in
objectsToKeepistrue. The length ofobjectsToKeepmust 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:
cc2bwsupports 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
cc2bwfunction directly, thenCC.PixelIdxListmust be homogeneous. This code shows how to makeCC.PixelIdxListhomogeneous 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.PixelIdxListhomogeneous. For example, this command generates code for a wrapper function namedmyFunction.codegen myFunction -args {BW}
Here is a sample definition for the wrapper function
myFunctionthat includes a call to thecc2bwfunction.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.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- 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)