You are on page 1of 15

PORT CITY INTERNATIONAL UNIVERSITY

LAB REPORT

Course Code: CSE 454

Course Title: Digital Image Processing Sessional

Submitted to:
Ms. Taslima Binte Hossain

Lecturer, Computer Science & Engineering

Port City International University

Submitted by:
Jahedul Islam CSE 01506411

B.sc in CSE CSE 15-A (DAY)


Question No: 1
Question Name: Convert the given images to binary images.
Solution:

Source Code: For image (1)

import cv2 as cv

import numpy as np

img = cv.imread('a.png')

image_contours = np.zeros((img.shape[1],

img.shape[0], 1),np.uint8)

image_binary = np.zeros((img.shape[1], img.shape[0], 1), np.uint8)

for channel in range(img.shape[2]):

ret, image_thresh = cv.threshold(img[:, :, channel],38,


255,cv.THRESH_BINARY)

contours = cv.findContours(image_thresh, 1, 1)[0]

cv.drawContours(image_contours,contours, -1, (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,

cv.CHAIN_APPROX_SIMPLE)[0]

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],-1,


(255,
255, 255), -1)

cv.imshow('Binary', image_binary)

cv.waitKey(0) & 0xFF is 27

cv.destroyAllWindows()

Output:

For image (2)

import cv2 as cv

import numpy as np

img = cv.imread('b.png')

image_contours = np.zeros((img.shape[1],

img.shape[0], 1),np.uint8)

image_binary = np.zeros((img.shape[1], img.shape[0], 1), np.uint8)


for channel in range(img.shape[2]):

ret, image_thresh = cv.threshold(img[:, :, channel],38,


255,cv.THRESH_BINARY)

contours = cv.findContours(image_thresh, 1, 1)[0]

cv.drawContours(image_contours,contours, -1, (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,

cv.CHAIN_APPROX_SIMPLE)[0]

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],-1,


(255,

255, 255), -1)

cv.imshow('Binary', image_binary)

cv.waitKey(0) & 0xFF is 27

cv.destroyAllWindows()

Output:
For image (3)

import cv2 as cv

import numpy as np

img = cv.imread('c.png')

image_contours = np.zeros((img.shape[1],

img.shape[0], 1),np.uint8)

image_binary = np.zeros((img.shape[1], img.shape[0], 1), np.uint8)

for channel in range(img.shape[2]):

ret, image_thresh = cv.threshold(img[:, :, channel],38,


255,cv.THRESH_BINARY)

contours = cv.findContours(image_thresh, 1, 1)[0]

cv.drawContours(image_contours,contours, -1, (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,

cv.CHAIN_APPROX_SIMPLE)[0]

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],-1,


(255,

255, 255), -1)

cv.imshow('Binary', image_binary)

cv.waitKey(0) & 0xFF is 27


cv.destroyAllWindows()

Output:

For image (4)

import cv2 as cv

import numpy as np

img = cv.imread('d.png')

image_contours = np.zeros((img.shape[1],

img.shape[0], 1),np.uint8)

image_binary = np.zeros((img.shape[1], img.shape[0], 1), np.uint8)

for channel in range(img.shape[2]):

ret, image_thresh = cv.threshold(img[:, :, channel],38,


255,cv.THRESH_BINARY)

contours = cv.findContours(image_thresh, 1, 1)[0]


cv.drawContours(image_contours,contours, -1, (255,255,255), 3)

contours = cv.findContours(image_contours, cv.RETR_LIST,

cv.CHAIN_APPROX_SIMPLE)[0]

cv.drawContours(image_binary, [max(contours, key = cv.contourArea)],-1,


(255,

255, 255), -1)

cv.imshow('Binary', image_binary)

cv.waitKey(0) & 0xFF is 27

cv.destroyAllWindows()

Output:
Question No: 2

Question Name: The basic built in functions of blob analysis.


Solution:

1) Eccentricity…
2) Ratio1 = Minor Axis length/Major Axis length
3) Ratio2 = perimeter/area
1) Eccentricity: Eccentricity measures the shortest length of the paths from a
given vertex v to reach any other vertex w of a connected graph. ... For a
connected region of a digital image it is defined through its neighborhood graph
and the given metric. The formula to find out the eccentricity of any conic
section is defined as: Eccentricity, e = c/a. Where, c = distance from the center to
the focus. a = distance from the center to the vertex.
Here I have taken the given images again & calculated their eccentricities that are
given below:

Source Code: (Eccentricity)

import cv2

import mahotas

import numpy as np
from pylab import gray, imshow, show

import os

import matplotlib.pyplot as plt

from skimage import measure

img = mahotas.imread('aaaaa.png')

img = img[:, :, 0]

print(img)

imshow(img)

show()

value = mahotas.features.eccentricity(img)

print("Eccentricity value = " + str(value))

Output:

For Image(a):

Eccentricity value: 0.5525392840146862


For Image(b):

Eccentricity value = 0.1848427837176973

For Image(c):

Eccentricity value = 0.8084986476476562


For Image(d):

Eccentricity value = 0.39665573836368084

2) Ratio1 = 'Minor Axis Length' / 'Major Axis Length':


The major axis of a blob is the axis about which it would be the easiest to spin
the blob. The major axis of a blob always passes through the blob's center of
mass. The minor axis of a blob is defined as the axis through the center of
mass about which the second moment of inertia is the largest.
Again to find the Ratio1 of the two axis’s we need to find the major & minor
axis with the help of using blob analysis region properties.
Source Code: (Ratio1)
import cv2

import numpy as np

from skimage import measure

# load image

img = cv2.imread("aaaaa.jpg")

# threshold on color

binary = cv2.inRange(img, (0,0,50), (50,50,255))

# get region properties

region = measure.regionprops(binary)

# get number of regions num = len(region)

print('number of regions:',num) print('')

# print all properties for prop

print(prop, region[0][prop])

Output:
Image1: 1.0586
Image2: 4.7061
Image3: 3.6204
Image4: 1.5425

3)Ratio2 = 'Perimeter' / 'Area':


To find the perimeter of non-circular objects, find the sum of all the side
lengths to determine the distance around the shape & to find the area use the
shape attribute of the image to get the height and width of the image. It
fetches the number of channels in the image. Calculate the area
as, area = height * width. Display the area.
After getting the attributes values from the relevant images the ‘Ratio2’ will be
induced by dividing the perimeter and the area of that used images.

Source Code: (Ratio2)


import os
import cv2
import csv
import mahotas as ms
from skimage.measure
import regionprops dir = "C:\Users\DELL"
f = open('forDIPLABREPORT.csv', 'w') writer = csv.writer(f)
writer.writerow(["Label","Eccentricity","Raito1","Raito2"])
for file_name in os.listdir(dir):
if file_name.endswith(".png"):
img = cv2.imread(os.path.join(dir,file_name))
(thresh,binaryImg) =
cv2.threshold(img,127,255,cv2.THRESH_BINARY) binaryImg
= binaryImg[:,:,0]
cv2.imwrite(f'{file_name[len(file_name) - 5]}.png',binaryImg)
ecvalue = ms.features.eccentricity(binaryImg)
props = regionprops(binaryImg) ratio1,ratio2 = 0,0
for p in props:
ratio1 = p.minor_axis_length / p.major_axis_length ratio2 =
p.perimeter / p.area
writer.writerow([file_name[len(file_name) -
5],ecvalue,ratio1,ratio2])
Output:
Image1: 0.6811
Image2: 0.5253
Image3: 0.5066
Image4: 0.4736

Question No: 3

Question Name: For all types of attributes in a CSV file like


following table and also assign Label to each type of object.
Solution: I have taken the relevant images as my input and after completing
all the conditioned tasks, I have defined all the attributes providing the outputs
in a CSV file which is given below,,,

The End

You might also like