Professional Documents
Culture Documents
Name: _________________________________
Enrollment #: _________________________________
Class: _________________________________
Objective
The aim of today’s lab is to introduce you to connected component labeling of binary
images and means of finding different properties of binary objects.
Submission Requirements
You are expected to complete the assigned tasks within the lab session and show them to
the lab engineer/instructor. Get the lab journal signed by your instructor and submit it by the
end of lab session.
Image Labeling
Labeling is process of clustering the pixels into components on basis of pixel neighborhood
(e.g. 4-connectivity) such that all connected pixels share similar intensity value and are in some
manner connected with each other. Once all the groups have been identified, each pixel is
labeled with a gray-level or a color (pseudo coloring).
Usually labeling is used to identify the number of objects and their respective area (i.e. no.
of pixels).
1. OpenCv built-in function to perform image labeling is cv2.connectedComponents
, defined as follows:
OpenCV provides four connected component analysis functions:
2. cv2.connectedComponents
3. cv2.connectedComponentsWithStats
4. cv2.connectedComponentsWithAlgorithm
5. cv2.connectedComponentsWithStatsWithAlgorithm
If you have OpenCV compiled with parallel processing support, then both
cv2.connectedComponentsWithAlgorithm and
cv2.connectedComponentsWithStatsWithAlgorithm will run faster than the first two.
1. The total number of unique labels (i.e., number of total components) that
were detected
2. A mask named labels has the same spatial dimensions as our input thresh image. For
each location in labels, we have an integer ID value that corresponds to the
connected component where the pixel belongs.
3. stats: Statistics on each connected component, including the bounding box
coordinates and area (in pixels).
4. The centroids (i.e., center) (x, y)-coordinates of each connected component.
Exercise 1
Parameters:
Parameters:
Modify the GUI you developed in Exercise 1 to incorporate the following features.
1. Load the image ‘coins.png’ once the user clicks the ‘Load’ button. You
can directly load the image without showing the File Open Dialog to
the user.
2. Clicking the ‘Label’ button, show the binarized image in the axes, find
the number of components in the image and the properties of these
components. A rectangle must also be drawn over each detected
component (as shown in Figure 1).
Bonus Marks: Using the ‘Centroid’ property, join the center of the first
connected component with the centers of all connected components
using the ‘line’ function (Figure 2).
Figure 3 Center of first connected component joined with all other components