cc2bw
Description
Examples
Filter on Multiple Image Properties
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))
Create Binary Image with Subset of Filtered Objects
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)
Isolate Vertically Oriented Regions
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
CC
— Connected components
structure
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. |
objectsToKeep
— Objects to keep
positive integer | vector of positive integers | logical vector
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
BW
— Binary image
logical array
Binary image, returned as a logical array of the same size as
CC.ImageSize
.
Data Types: logical
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
cc2bw
supports the generation of 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 R2024aR2024a: Generate C code using MATLAB Coder
cc2bw
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)