You are on page 1of 13

COMPUTER VISION & IMAGE

PROCESSING
BS IT Morning part # 4

Group Assignment
BS Information Technology part # 4 Morning
Abdul Qayoom Jat 2K18 / IT / 6
Tanveer Azam 2K18 / IT / 122

JULY 22, 2021


COMPUTER VISION & IMAGE PROCESSING

Contents
Links for projects:................................................................................................................................................................. 2
Introduction of Assignment: ................................................................................................................................................ 2
Canny Edge Detector:........................................................................................................................................................... 3
Report: ................................................................................................................................................................................ 3
Problem: ......................................................................................................................................................................... 3
Solution:.......................................................................................................................................................................... 3
Modification: .................................................................................................................................................................. 3
Conclusion: ..................................................................................................................................................................... 3
Algorithm: ....................................................................................................................................................................... 3
Code: ............................................................................................................................................................................... 4
Histogram: ............................................................................................................................................................................. 7
Report: ................................................................................................................................................................................ 7
Problem: ......................................................................................................................................................................... 7
Solution:.......................................................................................................................................................................... 7
Modification: .................................................................................................................................................................. 7
Conclusion: ..................................................................................................................................................................... 7
Algorithm: ....................................................................................................................................................................... 7
Code ................................................................................................................................................................................ 8
Harris Corner Detector: ....................................................................................................................................................... 9
Report ................................................................................................................................................................................. 9
Problem: ......................................................................................................................................................................... 9
Solution:.......................................................................................................................................................................... 9
Modification: .................................................................................................................................................................. 9
Conclusion: ..................................................................................................................................................................... 9
Algorithm: ....................................................................................................................................................................... 9
Code: ............................................................................................................................................................................. 10
Blue Color Detector: ........................................................................................................................................................... 11
Report: .............................................................................................................................................................................. 11
Problem: ....................................................................................................................................................................... 11
Solution:........................................................................................................................................................................ 11
Modification: ................................................................................................................................................................ 11
Conclusion: ................................................................................................................................................................... 11
Algorithm: ..................................................................................................................................................................... 11
Code: ............................................................................................................................................................................. 12

pg. 1
COMPUTER VISION & IMAGE PROCESSING

Links for projects:


GitHub Link
Google Drive Link

Introduction of Assignment:
• This assignment code is not copied from external sources.
• Corner detector code is copied from provided slides & then modified.
• Other code is done by Concatenating all my knowledge of python, learnt from book (Computer vision by
python 3 ) & by solving the book problems & activities.
• All code is learnt from Book named “ Computer Vision with python 3 ”
• Blue color detector is concatenation of multiple problems done during learning.
• I have use some of libraries like “ CV2 , SKimage, numpy, matplotlib ”.
• I have explained all of code in report for every mini project.

Request:

• This is my first incident to use python & processing images so please go through all projects & I want to
improve my technique to see object & results so I am waiting for acknowledgement.
• There is much more to present but here provided only 4, I have code all of concepts learned during
classes by help of book “ Computer Vision by python 3”.

pg. 2
COMPUTER VISION & IMAGE PROCESSING

Canny Edge Detector:


• Canny edge detector will detect edges of provides image.

Report:
Problem:
▪ I have to detect (weak edges, strong edges, magnitude, guassion) of provided images.
▪ Best detection by using range of values.
▪ Show all used images & result images in different windows, so we can observe changing step
by step.
▪ Save results in disk file so we can observe results without executing results.

Solution:
▪ I have chosen canny edge detector for purpose of getting (weak edges, Strong edges,
Guassion, Magnitude)of provided image.
▪ I have provide range of threshold values to get weak & strong edges.
▪ Show results.
▪ Save all results in disk so we can observe results.

Modification:
▪ This solution is provided by combining the knowledge of multiple simple short problems and
Activities done during learning from Book (Computer Vision by Python 3).
▪ Getting command line arguments
▪ Reading provided images from command line & converting to grey
▪ Create a method to get (Gaussion, Magnitude, Strong & weak edges.).
▪ I have provided (Theta Range & kernel size) to get maximum suppression & quantization
▪ I have provided (low threshold range for weak edges) & (high threshold range for strong edge
detection)
▪ Show images is a short program.
▪ Save images is a short program.

Conclusion:
▪ I have got (Weak edges, strong edges , guassion & magnitude of provided images)
▪ I have get best results by iterating loop over range of values.
▪ I have show results in different windows & save results in disk also.

Algorithm:
i. import CV2 & IO library for basic operations
ii. Set command line arguments for input & output pictures
iii. Read images which are provide in arguments & convert them to Grey Scale.
iv. Canny edge Detection Method:
o This will get as arguments (image, sigma, threshold 1 & 2 )
o This will return (gaussion, magnitude, weak edge & strong edges)
v. set kernel & sigma value range to get best detection of (weak edges, strong edges, quassion,
magnitude).
vi. Save images on disk.
vii. Wait for key to exit.

pg. 3
COMPUTER VISION & IMAGE PROCESSING
Code:
import cv2
from skimage import io

import cv2
import argparse
import numpy as np
import skimage.io as io
from skimage import color
import matplotlib.pyplot as plt

def cannyEdge(img, sigma, th1, th2):


size = int(2 * (np.ceil(3 * sigma)) + 1)

x, y = np.meshgrid(np.arange(-size / 2 + 1, size / 2 + 1),


np.arange(-size / 2 + 1, size / 2 + 1))

normal = 1 / (2.0 * np.pi * sigma ** 2)

kernel = np.exp(-(x ** 2 + y ** 2) / (2.0 * sigma ** 2)) / \


normal # calculating gaussian filter

kern_size, gauss = kernel.shape[0], np.zeros_like(img, dtype=float)

for i in range(img.shape[0] - (kern_size - 1)):


for j in range(img.shape[1] - (kern_size - 1)):
window = img[i:i + kern_size, j:j + kern_size] * kernel
gauss[i, j] = np.sum(window)

kernel, kern_size = np.array(


[[-1, -1, -1], [0, 0, 0], [1, 1, 1]]), 3 # edge detection
gx, gy = np.zeros_like(
gauss, dtype=float), np.zeros_like(gauss, dtype=float)

for i in range(gauss.shape[0] - (kern_size - 1)):


for j in range(gauss.shape[1] - (kern_size - 1)):
window = gauss[i:i + kern_size, j:j + kern_size]
gx[i, j], gy[i, j] = np.sum(
window * kernel.T), np.sum(window * kernel)

magnitude = np.sqrt(gx ** 2 + gy ** 2)
theta = ((np.arctan(gy / gx)) / np.pi) * 180 # radian to degree conversion
nms = np.copy(magnitude)

theta[theta < 0] += 180

# non maximum suppression; quantization and suppression done in same step


for i in range(theta.shape[0] - (kern_size - 1)):
for j in range(theta.shape[1] - (kern_size - 1)):
if theta[i, j] <= 22.5 or theta[i, j] > 157.5:
if (magnitude[i, j] <= magnitude[i - 1, j]) and (magnitude[i, j] <=
pg. 4
COMPUTER VISION & IMAGE PROCESSING
magnitude[i + 1, j]):
nms[i, j] = 0
if 22.5 < theta[i, j] <= 67.5:
if (magnitude[i, j] <= magnitude[i - 1, j - 1]) and (magnitude[i, j] <=
magnitude[i + 1, j + 1]):
nms[i, j] = 0
if 67.5 < theta[i, j] <= 112.5:
if (magnitude[i, j] <= magnitude[i + 1, j + 1]) and (magnitude[i, j] <=
magnitude[i - 1, j - 1]):
nms[i, j] = 0
if 112.5 < theta[i, j] <= 157.5:
if (magnitude[i, j] <= magnitude[i + 1, j - 1]) and (magnitude[i, j] <=
magnitude[i - 1, j + 1]):
nms[i, j] = 0

weak, strong = np.copy(nms), np.copy(nms)

# weak edges
weak[weak < th1] = 0
weak[weak > th2] = 0

# strong edges
strong[strong < th2] = 0
strong[strong > th2] = 1

# plotting multiple images


fig = plt.figure()
a = fig.add_subplot(2, 2, 1)
imgplot = plt.imshow(gauss, cmap='gray')
a.set_title('Gaussian')
a = fig.add_subplot(2, 2, 2)
imgplot = plt.imshow(magnitude, cmap='gray')
a.set_title('Magnitude')
a = fig.add_subplot(2, 2, 3)
imgplot = plt.imshow(weak, cmap='gray')
a.set_title('Weak edges')
a = fig.add_subplot(2, 2, 4)
imgplot = plt.imshow(255 - strong, cmap='gray')
a.set_title('Strong edges')
plt.show()

return gauss, magnitude, weak, strong

def main():
oparser = argparse.ArgumentParser(description="Canny Edge detector")
oparser.add_argument("--input", dest="input_image", required=True,
help="Path containing the image")
oparser.add_argument("--output", dest="output_image", required=True,
help="Path containing the image")
oparser.add_argument("--sigma", dest="sigma", default=3, required=False,
help="Sigma threshold", type=int)

pg. 5
COMPUTER VISION & IMAGE PROCESSING
oparser.add_argument("--th1", dest="lower_thresh", default=50, required=False,
help="Lower threshold for edges", type=int)
oparser.add_argument("--th2", dest="upper_thresh", default=100, required=False,
help="Upper threshold for edges", type=int)
options = oparser.parse_args()

img = cv2.imread(options.input_image)
img = color.rgb2gray(img)

gauss, magnitude, weak, strong = cannyEdge(


img, options.sigma, options.lower_thresh, options.upper_thresh)

io.imsave(f'{options.output_image}_{options.sigma}_{options.lower_thresh}_{options.upper_t
hresh}_gauss.jpg',
gauss)

io.imsave(f'{options.output_image}_{options.sigma}_{options.lower_thresh}_{options.upper_t
hresh}_magnitude.jpg',
magnitude)

io.imsave(f'{options.output_image}_{options.sigma}_{options.lower_thresh}_{options.upper_t
hresh}_weak.jpg', weak)

io.imsave(f'{options.output_image}_{options.sigma}_{options.lower_thresh}_{options.upper_t
hresh}_strong.jpg',
strong)

main()

# wait key for exit


cv2.waitKey(0)
Results:
Google Drive link for Canny Edge Detector results

pg. 6
COMPUTER VISION & IMAGE PROCESSING

Histogram:
• Histogram will provide graphical representation of provided image

Report:

Problem:
▪ I have to detect color variations in image of provided graphical representation of images
pixels.
▪ Use different attributes to setting up histogram tool.
▪ Show all used images & result images in different windows, so we can observe changing step
by step.
▪ Save results in disk file so we can observe results without executing results.

Solution:
▪ I have chosen Histogram for purpose of getting graphical representation of provided image.
▪ Show results & variations in images b/c of each operation (Original to Destination).
▪ I have calculated the histogram value on gray image to get pixel representation of that image.
▪ I set all the attributes which will help us to identify histogram & setting it up.
▪ Save all images (Original to Destination) in disk so we can observe results.

Modification:
▪ This solution is provided by combining the knowledge of multiple simple short problems and
Activities done during learning from Book (Computer Vision by Python 3).
▪ Read Image is a short program.
▪ Resize image is a short program.
▪ Converting the RGB image gray scale image is a short program.
▪ Equalize image is a short program.
▪ Show images is a short program.
▪ Calculate histogram & setting up its attributes is a short program.
▪ Save images is a short program.

Conclusion:
▪ I have successfully calculated the histogram or pixel value of provided image.
▪ I have also provided understandable names to histogram tools so we can identify our result
easily.
▪ I have shown results in different windows & save results in disk also.

Algorithm:
i. import CV2, matplotlib & IO library for basic operations
ii. Read an image from disk & store in variable originalImage.
iii. Resize that original image & store in variable resizedImage.
iv. Convert the resized image to gray scale image & store in variable grayImage.
v. Equalized the gray image & store in variable equalizedImage.
vi. Calculate histogram of gray image & store it in variable calHistogram.
vii. Set other attributes of histogram like title, xLabel, yLabel.
viii. Plot histogram & show in window.
ix. Show all result images in different windows.
x. Save all result images disk, path = “results/Histogram/”.
pg. 7
COMPUTER VISION & IMAGE PROCESSING
xi. Wait for key to exit.

Code
# imports of used libraries
import cv2
from matplotlib import pyplot as plt
from skimage import io

# images
originalImage = cv2.imread("rd.jpeg")
resizedImage = cv2.resize(originalImage, (380, 450))
grayImage = cv2.cvtColor(resizedImage, cv2.COLOR_BGR2GRAY)
equalizedImage = cv2.equalizeHist(grayImage)

# show images
cv2.imshow("Original Image", originalImage)
cv2.imshow("Resized Image", resizedImage)
cv2.imshow("Gray Image", grayImage)
cv2.imshow("Histogram equalized image", equalizedImage)

# Calculate Histogram
calHistogram = cv2.calcHist([grayImage], [0], None, [256], [0, 256])
plt.figure()
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("Number of pixels")
plt.plot(calHistogram)
plt.show()

# save Images
io.imsave("results/histogram/OriginalImage.jpeg", originalImage)
io.imsave("results/histogram/ResizedImage.jpeg", resizedImage)
io.imsave("results/histogram/GrayImage.jpeg", grayImage)
io.imsave("results/histogram/EqualizedImage.jpeg", equalizedImage)

# wait for key to exit


cv2.waitKey(0)
Results:
Google Drive Link for Histogram Results

pg. 8
COMPUTER VISION & IMAGE PROCESSING

Harris Corner Detector:


• Harris Corner detector will detect corners of provided image.

Report
Problem:
▪ I have to detect corner of provided image.
▪ No false edges should be detected.
▪ True edges should be detected & on true place.
▪ Show all used images & result images in different windows, so we can observe changing step
by step.
▪ Save results in disk file so we can observe results without executing results.

Solution:
▪ I have chosen Harris corner detector for purpose of getting corners of provided image.
▪ I have varied kernel values to get corners on sufficient level.
▪ Decrease kernel value to get more understandable corners.
▪ Show results & variations in images b/c of each operation (Original to Destination).
▪ Save all images (Original to Destination) in disk so we can observe results.

Modification:
▪ This solution is provided in Slides, which we have provided, & I have made some of changes in
code & also add some other code to get sufficient knowledge about whole working process of
program.
▪ I have change variable names to understandable names to identify image or result.
▪ I have added another variable to get understanding of dilated image corner detection or
undilated image corner detection.
▪ I have decreased the kernel size from ( 9 to 7 ) to get more visualizable corners.
▪ I have shown all images ( Original to destination ) in different windows so we can identify
results step by step.
▪ I have saved all images ( Original to Destination ) in disk so understand result without running
the program.

Conclusion:
▪ I have got easily visualizable corners by decreasing the kernel size to 7.
▪ I get that undilated image provide thin edges as compare to dilated image.
▪ I have shown & saved results in disk so we can identify results easily.

Algorithm:
i. import CV2, numpy & IO library for basic operations
ii. Read an image from disk & store in variable originalImage.
iii. Resize that original image & store in variable resizedImage.
iv. Resize that original image & store in variable resizedImage2.
v. Convert the resized image to gray scale image & store in variable grayImage.
vi. Detect gray image by Harris corner detector & store in variable cornerDetectedImage.
vii. Dilate the detected image.
viii. Show all result images in different windows.
ix. Save all result images disk, path = “results/harrisCornerDetector/”.
x. Wait for key to exit.
pg. 9
COMPUTER VISION & IMAGE PROCESSING
Code:
import cv2
from skimage import io
import numpy as np

# Images
originalImage = cv2.imread("rd.jpeg")
resizedImage = cv2.resize(originalImage, (350, 500))
resizedImage2 = cv2.resize(originalImage, (350, 500))
grayImage = cv2.cvtColor(resizedImage, cv2.COLOR_BGR2GRAY)

# Steps to detect corners


gray = np.float32(grayImage)
cornerDetectedImage = cv2.cornerHarris(gray, 3, 7, 0, 0.04)
# Threshold for an optimal value, it may vary depending on the image.
resizedImage[cornerDetectedImage > 0.01 * cornerDetectedImage.max()] = [0, 0, 255]

# result is dilated for marking the corners,not important


dilatedImage = cv2.dilate(cornerDetectedImage, None)
# Threshold for an optimal value, it may vary depending on the image.
resizedImage2[dilatedImage > 0.01 * dilatedImage.max()] = [0, 0, 255]

# show imagesS
cv2.imshow("Original Image", originalImage)
cv2.imshow("Gray Image", grayImage)
cv2.imshow("Corner Detected Image", cornerDetectedImage)
cv2.imshow("Corner Detected Image Before Dilated", resizedImage)
cv2.imshow("Dilated Image", dilatedImage)
cv2.imshow("Corner Detected Image After Dilated", resizedImage2)

# save images
io.imsave("results/HarrisCornerDetector/Original Image.jpeg", originalImage)
io.imsave("results/HarrisCornerDetector/Gray Image.jpeg", grayImage)
io.imsave("results/HarrisCornerDetector/Corner Detected Image.jpeg", cornerDetectedImage)
io.imsave("results/HarrisCornerDetector/Corner Detected Image Before Dilated.jpeg",
resizedImage)
io.imsave("results/HarrisCornerDetector/Dilated Image.jpeg", dilatedImage)
io.imsave("results/HarrisCornerDetector/Corner Detected Image after Dilated.jpeg",
resizedImage2)

# wait key to exit


cv2.waitKey(0)
Results:
Google Drive Link for Harris Corner Detector

pg. 10
COMPUTER VISION & IMAGE PROCESSING

Blue Color Detector:


• Blue color detector will detect color in live mode, directly by capturing camera frames.

Report:
Problem:
▪ I have to detect blue color in live captured frames directly by camera.
▪ No other colors should be detected.
▪ Only blue should be detected & on true places.
▪ Show all results ( Live camera, mask, result ) in different windows to get understanding of
results.
▪ Record result to get understanding of results without executing the program.

Solution:
▪ I have captured the camera frames continuously by using infinite loop.
▪ I have converted the color scale to HSV to get colors efficiently.
▪ I have created mask b/w lower range of color & upper range of color ( Provided blue color
range ).
▪ I have operate frames bitwise to get blue color detected individually.
▪ Show results in individual windows & also record the result ( by external software) to
understand the result during runtime & also without executing program.

Modification:
▪ This solution is provided by combining the knowledge of multiple simple short problems and
Activities done during learning from Book (Computer Vision by Python 3).
▪ Capture the camera frame is a short program.
▪ While loop is also a short program.
▪ Reading the captured frames is a short program.
▪ Converting images BGR to HSV to a short program.
▪ Creating mask b/w lower & upper color limit is a short program.
▪ Operate frames bitwise to get results is a short program.
▪ Show results ( Camera, Mask, Result ) individually is a short program

Conclusion:
▪ I have got mask & camera & result window individually to get live detection of camera frames.
▪ I have recorded the video ( available in results folder) to visualize results without executing
the program.

Algorithm:
i. Import libraries “ cv2 & numpy ”.
ii. Capture the video by device camera & store frames in cap variable.
iii. Run infinite loop.
iv. With every loop read captured images frames & convert them to HSV color scale.
v. With every loop create mask for upper & lower limit of image color mask.
vi. Highlight only blue colored objects.
vii. Show camera, mask & result in different windows.
viii. Wait for key to be pressed.
ix. Destroy all windows & stop camera capturing.

pg. 11
COMPUTER VISION & IMAGE PROCESSING
Code:
import cv2
import numpy as np

# Web camera no 0 is used to capture the frames


cap = cv2.VideoCapture(0)
# This drives the program into an infinite loop.
while 1:
# Captures the live stream frame-by-frame
_, frame = cap.read()

# Converts images from BGR to HSV


hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# Here we are defining range of blue color in HSV


# This creates a mask of blue coloured
# objects found in the frame.
lower = np.array([110, 50, 50])
upper = np.array([130, 255, 255])
mask = cv2.inRange(hsvImage, lower, upper)

# The bitwise and of the frame and mask is done so


# that only the blue coloured objects are highlighted
# and stored in res
res = cv2.bitwise_and(frame, frame, mask=mask)

# This displays the frame, mask


# and res which we created in 3 separate windows.
cv2.imshow("Camera", frame)
cv2.imshow("Mask", mask)
cv2.imshow("Result", res)

# wait key for exit


k = cv2.waitKey(5) & 0xFF
if k == 27:
break

# Destroys all of the HighGUI windows.


cv2.destroyAllWindows()

# release the captured frame


cap.release()
Results:
Google Drive Link for Blue Color Detector Result

pg. 12

You might also like