Skip to content
Snippets Groups Projects
Commit 4f43a961 authored by ahmeddiderrahat's avatar ahmeddiderrahat
Browse files

Assignment 1 complete

parent 5ca1aeb3
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
No preview for this file type
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:code id:c80c42a7 tags:
 
``` python
import numpy as np
import cv2 as cv
import math
from matplotlib import pyplot as plt
```
 
%% Cell type:markdown id:56a47f64 tags:
 
### Exercise 1.1: Loading images
 
%% Cell type:code id:108d8d7d tags:
 
``` python
# Read the image file
img = cv.imread("../../Data/graffiti.png")
```
 
%% Cell type:code id:e054c412 tags:
 
``` python
# COnvert to gray scale
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
```
 
%% Cell type:code id:00e1b7e9 tags:
 
``` python
# check the shape
print(f'Shape of Original image {img.shape} and GrayScale Image {gray_img.shape}')
```
 
%% Output
Shape of Original image (640, 800, 3) and GrayScale Image (640, 800)
%% Cell type:code id:3c5046bf tags:
 
``` python
# init new array size
new_array = np.full((img.shape[0], img.shape[1]*2, img.shape[2]), 0)
new_array.shape
```
 
%% Output
(640, 1600, 3)
%% Cell type:code id:9c90a34f tags:
 
``` python
# copy original image
new_array[0:img.shape[0], 0:img.shape[1], 0:img.shape[2]] = img
```
 
%% Cell type:code id:79140c56 tags:
 
``` python
# copy gray scale image in all three chanel
for i in range(img.shape[2]):
new_array[0:img.shape[0], img.shape[1]:new_array.shape[1], i] = gray_img
```
 
%% Cell type:code id:5ba1de99 tags:
 
``` python
def cv2_imshow(title, img):
 
cv.startWindowThread()
cv.imshow(title, img)
cv.waitKey(0)
cv.destroyAllWindows()
 
return img
```
 
%% Cell type:code id:2eadd4d6 tags:
 
``` python
# Get the information of the incoming image type
info = np.iinfo(img.dtype)
 
# normalize the data to 0 - 1
new_array = new_array.astype(np.float64) / info.max
 
# Now scale by 255
new_array = 255 * new_array
 
# array to image
new_img = new_array.astype(np.uint8)
 
printed_img = cv2_imshow("new_image", new_img)
```
 
%% Cell type:markdown id:3b424e6f tags:
 
### Exercise 1.2: OpenCV experiments (2 Point)
 
%% Cell type:code id:dd92bab7 tags:
 
``` python
cap = cv.VideoCapture(0)
mode = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
 
# wait for key and switch to mode
ch = cv.waitKey(1) & 0xFF
 
if ch == ord('1'):
mode = 1
elif ch == ord('2'):
mode = 2
 
elif ch == ord('3'):
mode = 3
 
elif ch == ord('4'):
mode = 4
 
elif ch == ord('5'):
mode = 5
elif ch == ord('6'):
mode = 6
elif ch == ord('q'):
break
 
 
if mode == 1:
# Convert BGR to HSV
frame = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
elif mode == 2:
# Convert BGR to LAB
frame = cv.cvtColor(frame, cv.COLOR_BGR2LAB)
elif mode == 3:
# Convert BGR to YUV
frame = cv.cvtColor(frame, cv.COLOR_BGR2YUV)
elif mode == 4:
# Gaussian-Thresholding
img = cv.medianBlur(frame, 5)
 
# convert to gray scale
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
 
frame = cv.adaptiveThreshold(gray_img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
elif mode == 5:
# Otsu's thresholding
 
# convert to gray scale
gray_img = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
 
ret, frame = cv.threshold(gray_img, 0,255, cv.THRESH_BINARY+cv.THRESH_OTSU)
 
elif mode == 6:
# Canny Edge Detection
frame = cv.Canny(frame, 100, 200)
 
# Display the resulting frame
cv.imshow('frame', frame)
 
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
```
 
%% Cell type:markdown id:b202721e tags:
 
### Exercise 1.3: SIFT in OpenCV (1 Point)
 
%% Cell type:code id:73f9952d tags:
 
``` python
cap = cv.VideoCapture(0)
cv.namedWindow('Learning from images: SIFT feature visualization')
while True:
 
# 1. read each frame from the camera (if necessary resize the image)
# and extract the SIFT features using OpenCV methods
# Note: use the gray image - so you need to convert the image
 
# Capture frame-by-frame
ret, frame = cap.read()
 
# wait for key and switch to mode
ch = cv.waitKey(1) & 0xFF
 
if ch == ord('q'):
break
 
# convert to gray scale
gray_img = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
 
features = cv.SIFT_create()
 
# 2. draw the keypoints using cv2.drawKeypoints
# There are several flags for visualization - e.g. DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
 
keypoints = features.detect(gray_img, None)
 
# drawKeypoints function is used to draw keypoints
output_image = cv.drawKeypoints(gray_img, keypoints, 0, (255,0,0), \
flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
 
cv.imshow('frame', output_image)
 
 
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
```
 
%% Cell type:markdown id:d2ae3353 tags:
 
### Exercise 1.4: Convolution (2 Points)
 
%% Cell type:code id:4e357985 tags:
 
``` python
def im2double(im):
"""
Converts uint image (0-255) to double image (0.0-1.0) and generalizes
this concept to any range.
 
:param im:
:return: normalized image
"""
min_val = np.min(im.ravel())
max_val = np.max(im.ravel())
out = (im.astype('float') - min_val) / (max_val - min_val)
return out
```
 
%% Cell type:code id:511ffd65 tags:
 
``` python
def make_gaussian(size, fwhm = 3, center=None):
""" Make a square gaussian kernel.
 
size is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""
 
x = np.arange(0, size, 1, float)
y = x[:,np.newaxis]
 
if center is None:
x0 = y0 = size // 2
else:
x0 = center[0]
y0 = center[1]
 
k = np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / fwhm**2)
return k / np.sum(k)
```
 
%% Cell type:code id:bc8d4cf8 tags:
 
``` python
def convolution_2d(img, kernel):
"""
Computes the convolution between kernel and image
 
:param img: grayscale image
:param kernel: convolution matrix - 3x3, or 5x5 matrix
:return: result of the convolution
"""
# TODO write convolution of arbritrary sized convolution here
# Hint: you need the kernelsize
 
offset = int(kernel.shape[0]/2)
 
#print(f'Offset = {offset}')
 
# Shape of the new image object
new_shape = np.array(img.shape) + (np.array([offset, offset])*2)
 
# define the enw array with offset size
newimg = np.zeros(new_shape)
 
# set the image to the new image array
newimg[offset:img.shape[0]+offset, offset:img.shape[1]+offset] = img
 
#print(newimg.shape, img.shape, kernel.shape)
#print(offset-1, newimg.shape[0]-offset-1)
 
#np.set_printoptions(precision=3)
#print(newimg)
 
new_img = np.zeros(img.shape)
 
#print(img.shape, new_img.shape)
 
for i in range(offset, newimg.shape[0]-offset):
 
#if i==2:
#break
 
for j in range (offset, newimg.shape[1]-offset):
 
#if j==2:
#break
#print(i, j)
 
x_start = i-offset
x_end = x_start+(2*offset)+1
 
y_start = j-offset
y_end = y_start+(2*offset)+1
 
#print(i, x_start, x_end, y_start, y_end)
 
temp_img = newimg[x_start:x_end, y_start:y_end]
 
#print(temp_img.shape, kernel.shape)
 
prod = temp_img * kernel
 
#print((x_start, y_start), (x_end, y_end), temp_img.shape)
 
#np.set_printoptions(precision=3)
 
#print(kernel)
 
#print(temp_img)
 
#print(prod)
 
sop = np.sum(prod)
 
#print(sop)
 
new_img[i-offset][j-offset] = sop
 
#print(temp_img.shape, kernel.shape, prod.shape)
 
#print(newimg == new_img)
 
return new_img
 
#abc = convolution_2d(con_img, gk)
```
 
%% Cell type:code id:be452463 tags:
 
``` python
# Calculate the Magnitude of gradiant
def calculate_mog(gx, gy):
 
gx2 = gx * gx # calculate gx^2
 
gy2 = gy * gy # calculate gy^2
 
return np.sqrt(gx2+gy2) # sqrt of gx^2 + gy^2
```
 
%% Cell type:code id:d4a49a2c tags:
 
``` python
def draw_fig(gk_img, sobel_x_img, sobel_y_img, mog_img):
fig = plt.figure(figsize=(10, 10))
 
# draw gausian blur
gk_image = fig.add_subplot(2,2,1)
gk_image.set_title("Applied Gaussian Blur")
gk_image.imshow(gk_img, cmap='gray')
plt.axis('off')
 
# draw sobel-X
sobelx_image = fig.add_subplot(2,2,2)
sobelx_image.set_title("Sobel-X output")
sobelx_image.imshow(sobel_x_img, cmap='gray')
plt.axis('off')
 
# draw sobel-Y
sobely_image = fig.add_subplot(2,2,3)
sobely_image.set_title("Sobel-X output")
sobely_image.imshow(sobel_y_img, cmap='gray')
plt.axis('off')
 
mog_image = fig.add_subplot(2,2,4)
mog_image.imshow(mog_img, cmap='gray')
mog_image.set_title("MOG output")
plt.axis('off')
 
plt.show()
```
 
%% Cell type:code id:1cafe8f6 tags:
 
``` python
if __name__ == "__main__":
 
# 1. load image in grayscale
 
# Read the image file
img = cv.imread("../../Data/graffiti.png")
 
# COnvert to gray scale
gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
 
 
# 2. convert image to 0-1 image (see im2double)
con_img = im2double(gray_img)
 
 
# image kernels
sobelmask_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
sobelmask_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
gk = make_gaussian(11)
 
# 3 .use image kernels on normalized image
sobel_x_img = convolution_2d(con_img, sobelmask_x)
#new_image = cv2_imshow("sobel_x_iamge", sobel_x_img)
 
sobel_y_img = convolution_2d(con_img, sobelmask_y)
#new_image = cv2_imshow("sobel_y_iamge", sobel_y_img)
 
gk_img = convolution_2d(con_img, gk)
#new_image = cv2_imshow("gk_filter_output", gk_img)
 
# 4. compute magnitude of gradients
mog_img = calculate_mog(sobel_x_img, sobel_y_img)
 
 
draw_fig(gk_img, sobel_x_img, sobel_y_img, mog_img)
```
 
%% Output
 
 
%% Cell type:code id:c2b9d6bd tags:
 
``` python
```
......
This diff is collapsed.
import cv2
cap = cv2.VideoCapture(0)
cv2.namedWindow('Learning from images: SIFT feature visualization')
while True:
# 1. read each frame from the camera (if necessary resize the image)
# and extract the SIFT features using OpenCV methods
# Note: use the gray image - so you need to convert the image
# 2. draw the keypoints using cv2.drawKeypoints
# There are several flags for visualization - e.g. DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
# close the window and application by pressing a key
# YOUR CODE HERE
import numpy as np
import cv2
def im2double(im):
"""
Converts uint image (0-255) to double image (0.0-1.0) and generalizes
this concept to any range.
:param im:
:return: normalized image
"""
min_val = np.min(im.ravel())
max_val = np.max(im.ravel())
out = (im.astype('float') - min_val) / (max_val - min_val)
return out
def make_gaussian(size, fwhm = 3, center=None):
""" Make a square gaussian kernel.
size is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""
x = np.arange(0, size, 1, float)
y = x[:,np.newaxis]
if center is None:
x0 = y0 = size // 2
else:
x0 = center[0]
y0 = center[1]
k = np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / fwhm**2)
return k / np.sum(k)
def convolution_2d(img, kernel):
"""
Computes the convolution between kernel and image
:param img: grayscale image
:param kernel: convolution matrix - 3x3, or 5x5 matrix
:return: result of the convolution
"""
# TODO write convolution of arbritrary sized convolution here
# Hint: you need the kernelsize
offset = int(kernel.shape[0]/2)
newimg = np.zeros(img.shape)
# YOUR CODE HERE
return newimg
if __name__ == "__main__":
# 1. load image in grayscale
# 2. convert image to 0-1 image (see im2double)
# image kernels
sobelmask_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
sobelmask_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
gk = make_gaussian(11)
# 3 .use image kernels on normalized image
# 4. compute magnitude of gradients
# Show resulting images
cv2.imshow("sobel_x", sobel_x)
cv2.imshow("sobel_y", sobel_y)
cv2.imshow("mog", mog)
cv2.waitKey(0)
cv2.destroyAllWindows()
\ No newline at end of file
Assignments/lfi-01/graffiti.png

929 KiB

import numpy as np
import cv2
import math
import sys
############################################################
#
# KMEANS
#
############################################################
# implement distance metric - e.g. squared distances between pixels
def distance(a, b):
pass
# YOUR CODE HERE
# k-means works in 3 steps
# 1. initialize
# 2. assign each data element to current mean (cluster center)
# 3. update mean
# then iterate between 2 and 3 until convergence, i.e. until ~smaller than 5% change rate in the error
def update_mean(img, clustermask):
"""This function should compute the new cluster center, i.e. numcluster mean colors"""
# YOUR CODE HERE
pass
def assign_to_current_mean(img, result, clustermask):
"""The function expects the img, the resulting image and a clustermask.
After each call the pixels in result should contain a cluster_color corresponding to the cluster
it is assigned to. clustermask contains the cluster id (int [0...num_clusters]
Return: the overall error (distance) for all pixels to there closest cluster center (mindistance px - cluster center).
"""
overall_dist = 0
# YOUR CODE HERE
return overall_dist
def initialize(img):
"""inittialize the current_cluster_centers array for each cluster with a random pixel position"""
# YOUR CODE HERE
print(current_cluster_centers)
def kmeans(img):
"""Main k-means function iterating over max_iterations and stopping if
the error rate of change is less then 2% for consecutive iterations, i.e. the
algorithm converges. In our case the overall error might go up and down a little
since there is no guarantee we find a global minimum.
"""
max_iter = 10
max_change_rate = 0.02
dist = sys.float_info.max
clustermask = np.zeros((h1, w1, 1), np.uint8)
result = np.zeros((h1, w1, 3), np.uint8)
# initializes each pixel to a cluster
# iterate for a given number of iterations or if rate of change is
# very small
# YOUR CODE HERE
return result
# num of cluster
numclusters = 3
# corresponding colors for each cluster
cluster_colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 255, 255], [255, 255, 255], [0, 0, 0], [128, 128, 128]]
# initialize current cluster centers (i.e. the pixels that represent a cluster center)
current_cluster_centers = np.zeros((numclusters, 1, 3), np.float32)
# load image
imgraw = cv2.imread('./images/Lenna.png')
scaling_factor = 0.5
imgraw = cv2.resize(imgraw, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
# compare different color spaces and their result for clustering
# YOUR CODE HERE or keep going with loaded RGB colorspace img = imgraw
image = imgraw
h1, w1 = image.shape[:2]
# execute k-means over the image
# it returns a result image where each pixel is color with one of the cluster_colors
# depending on its cluster assignment
res = kmeans(image)
h1, w1 = res.shape[:2]
h2, w2 = image.shape[:2]
vis = np.zeros((max(h1, h2), w1 + w2, 3), np.uint8)
vis[:h1, :w1] = res
vis[:h2, w1:w1 + w2] = image
cv2.imshow("Color-based Segmentation Kmeans-Clustering", vis)
cv2.waitKey(0)
cv2.destroyAllWindows()
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
mode = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# wait for key and switch to mode
ch = cv2.waitKey(1) & 0xFF
if ch == ord('1'):
mode = 1
# ...
if ch == ord('q'):
break
if mode == 1:
# just example code
# your code should implement
frame = cv2.GaussianBlur(frame, (5, 5), 0)
# Display the resulting frame
cv2.imshow('frame', frame)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment