Basic Image Processing
& Connected Component Analysis
Objective:
The objective of this whole topic is to introduce you with some transformation especially with
respect to image processing and perform connected component labelling in images and to get an
understanding of intensity level resolution.
Description:
1) Rotation:
2) Connected Component Analysis:
Connected Component Analysis
Connected Component Analysis or Labelling enables us to detect different objects from a binary
image. Once different objects have been detected, we can perform a number of operations on
them: from counting the number of total objects to counting the number of objects that are
similar, from finding out the biggest object of the bunch to finding out the smallest and from
finding out the closest pair of objects to finding out the farthest etc.
Connected Component labelling procedure is as follows: Process the image from left to right, top to bottom:
If the next pixel to process is If only one from top or left
is 1, copy its label. then If both top and left are one and have the
same label, copy it. If top and left they have different labels then Copy the smaller label now Update the equivalence table .Otherwise, assign a new label. simply Re-label with the smallest of equivalent labels.
Now there some task you have to perform let's start:
Task No 01:
1:Create a generic code that create a border around any landscape image as shown below. The
length of right and left borders must be 10% of the original horizontal length of the image. The
length of upper and lower border must be such that the image now have same number of rows as
columns. Save the image.
2:Read any image that you want and save it in gray scale. Now rotate the image that you have
read. Write the image to the disk.
Hint: you can use built in functions of opencv
3: For the image given below (provided with the lab handout), apply the connectected
component labelling and count the total number of white objects. First threshold the images and
then do connected component analysis
Task 01
Code
import cv2 as cv
borderType = cv.BORDER_CONSTANT
img= cv.imread('Picture5.png') #read input image
vertical_length = img.shape[0] #number of rows
horizontal_length = img.shape[1] #number of columns
left = int(0.10 *
horizontal_length) #10 percent of horizontal length
top = 2*left
bottom = top
right = left
#add broder to image
new_img = cv.copyMakeBorder(img, top, bottom,
left, right, borderType, None)
#save image with border image
cv.imwrite('image\img_with_Border_1.jpg',new_img)
Output
Task 02
(a)
Code
import cv2 as cv
img = cv.imread('pic.png') #read original image
gray_img =
cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#convert
image to grayscale
cv.imwrite('image\img_in_gray_1.jpg',gray_img) #save gray scaled image
Output
(b)
Code
import cv2 as cv
img = cv.imread('pic.png') #read original image
#rotate image by 90 degrees clockwise
img_rotate_90_clockwise = cv.rotate(img,
cv.ROTATE_90_CLOCKWISE)
cv.imwrite('image\Rotate_90_1.jpg',img_rotate_90_clockwise) #save rotated image
Output
THINK!!
1. What will be the number of dimension of a grayscale image if opened as colored?
2. Is image a list, tuple or an array?
3. A black and white image can only have what gray levels?
4. Is it possible to count objects which are inside an object using connected component
analysis?
5. Can we apply connected component analysis any image without doing any preprocessing
No comments:
Post a Comment