USING Python 1) Blur the image using box blur method. Take kernel value as a) 1/
ID: 3590731 • Letter: U
Question
USING Python
1) Blur the image using box blur method. Take kernel value as a) 1/9 ([ 1 1 1; 1 1 1; 1 1 1]) and b) 1/25 ([11111;11111;11111;11111;11111]). Then perform box filtering. Show all 2 image results. Also visually compare the two results. [15 Points] 2) Blur the image using Gaussian blur with standard deviation 2 and 9 respectively. Use kernel size as (3x3) and (7x7). Then perform Gaussian filtering on all images. Show all 4 image results. (Hint: The intensity of the blurred image should be similar to the original image. In other words, you should handle kernel normalization, similar to adding the constant 1/9 in the kernel in question 1) [20 Points] 3) Add random noise to the original image and then do median filtering. Try median filter of 3x3 and 5x5. Show all 4 image results. How does the median blur compare to the Gaussian blur? [15 Points] 4) Edge detection: Convolve the image with the Laplace kernel, and vertical and horizontal Sobel kernels. Show all 3 image results. (Here, you must submit the Laplacian kernel that is used.) [15 Points] 5) Rotation: The rotation code on blackboard uses a rotation matrix to find the corresponding pixel coordinates in the source image for each pixel location in the output image. However, depending on the rotation angle, some coordinates may not be integers, i.e. the coordinates will be “between” pixels. Rounding or converting the coordinates to an integer value will give us a nearby pixel, but interpolating the pixel intensities will give us more accurate intensity values and a nicer image. Add bilinear interpolation to the rotation code on blackboard. [10Points] 6) Implement the histogram of oriented gradients (HOG) algorithm. This should be written as a function that takes an image and an integer n, and returns a feature vector computed with HOG, using histograms of size n. Please do not use an off-the-shelf HOG implementation. You may use opencv and numpy functions (except the built-in HOG from opencv) [25 Points]
Explanation / Answer
As in one-dimensional signals, images also can be filtered with various low-pass filters(LPF), high-pass filters(HPF) etc. LPF helps in removing noises, blurring the images etc. HPF filters helps in finding edges in the images.
OpenCV provides a function cv2.filter2D() to convolve a kernel with an image. As an example, we will try an averaging filter on an image. A 5x5 averaging filter kernel will look like below:
[K = rac{1}{25} egin{bmatrix} 1 & 1 & 1 & 1 & 1 \ 1 & 1 & 1 & 1 & 1 \ 1 & 1 & 1 & 1 & 1 \ 1 & 1 & 1 & 1 & 1 \ 1 & 1 & 1 & 1 & 1 end{bmatrix}]
Operation is like this: keep this kernel above a pixel, add all the 25 pixels below this kernel, take its average and replace the central pixel with the new average value. It continues this operation for all the pixels in the image. Try this code and check the result:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('opencv_logo.png')
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.