You are on page 1of 5

CSE 440 Digital Image Processing Lab-04

Connected Component labeling of binary images

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

The most popular method is cv2.connectedComponentsWithStats which returns the following


information:

1. The bounding box of the connected component


2. The area (in pixels) of the component
3. The centroid/center (x, y)-coordinates of the component
4. The first method, cv2.connectedComponents, is the same as the second, only it does not
return the above statistical information. In the vast majority of situations, you will need
the stats, so it’s worth simply using cv2.connectedComponentsWithStats instead.

The third method, cv2.connectedComponentsWithAlgorithm, implements faster, more


efficient algorithms for connected component analysis.

If you have OpenCV compiled with parallel processing support, then both
cv2.connectedComponentsWithAlgorithm and
cv2.connectedComponentsWithStatsWithAlgorithm will run faster than the first two.

But in general, stick with cv2.connectedComponentsWithStats until you are comfortable


working with connected component labeling.

cv2.connectedComponentsWithStats performs connected component analysis with OpenCV.


We pass in three arguments here:

1. The binary thresh image


2. The --connectivity command line argument
3. The data type (which you should leave as cv2.CV_32S)

The cv2.connectedComponentsWithStats then returns a 4-tuple of:

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.

n_components, labels, stats, centroids = cv2.connectedComponentsWithS


tats(binary,connectivity=8)
output = img.copy()0
w = stats[0, cv2.CC_STAT_WIDTH]
h = stats[0, cv2.CC_STAT_HEIGHT]
area = stats[0, cv2.CC_STAT_AREA]

Exercise 1

Design a Python GUI, as shown below, to implement following operations:


1) Load Image: read image ‘coins.png’
2) Label Image: Label the read image using
‘cv2.connectedComponentsWithStats’ function. Populate information about
image width, height and no. of coins in ‘static text’ controls. And display the labeled
image into axes.

Figure 1GUI for Exercise 1


Python OpenCV | cv2.rectangle() method

OpenCV-Python is a library of Python bindings designed to solve computer vision


problems. cv2.rectangle() method is used to draw a rectangle on any image.

Syntax: cv2.rectangle(image, start_point, end_point, color, thickness)

Parameters:

image: It is the image on which rectangle is to be drawn.


start_point: It is the starting coordinates of rectangle. The coordinates are represented
as tuples of two values i.e. (X coordinate value, Y coordinate value).
end_point: It is the ending coordinates of rectangle. The coordinates are represented as
tuples of two values i.e. (X coordinate value, Y coordinate value).
color: It is the color of border line of rectangle to be drawn. For BGR, we pass a tuple.
eg: (255, 0, 0) for blue color.
thickness: It is the thickness of the rectangle border line in px. Thickness of -1 px will fill
the rectangle shape by the specified color.
Return Value: It returns an image.

Python OpenCV | cv2.circle() method


OpenCV-Python is a library of Python bindings designed to solve computer vision problems.
cv2.circle() method is used to draw a circle on any image.

Syntax: cv2.circle(image, center_coordinates, radius, color, thickness)

Parameters:

image: It is the image on which circle is to be drawn.


center_coordinates: It is the center coordinates of circle. The coordinates are
represented as tuples of two values i.e. (X coordinate value, Y coordinate
value). radius: It is the radius of circle.
color: It is the color of border line of circle to be drawn. For BGR, we pass a tuple. eg:
(255, 0, 0) for blue color.
thickness: It is the thickness of the circle border line in px. Thickness of -1 px will fill the
circle shape by the specified color.
Return Value: It returns an image.
Exercise 2

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 2: Localized connected component in the image

Figure 3 Center of first connected component joined with all other components

Powered by TCPDF (www.tcpdf.org)

You might also like