In [ ]:

Midterm Exam

Each task of the exam is performed on the original images, unless the reverse is said

At the end of the exam, you need to commit Jupiter's notebook into GitHab in the machine vision repository in a separate folder called Midterm. Each task must be commited in separate corresponding file, e.g. Task1.ipynb.

Task 1

Apply each filter to image, specify which properties corresponds to filters

(1) corresponds to a low-pass filter in the frequency domain; (2) less than the other specified noise-canceling filters, blurs edges; (3) increases the sharpness of the image (4) works slower than the other specified noise-canceling filters; (5) is not a noise canceling filter; (6) poorly removes noise such as "salt and pepper"; (7) will destroy all lines of one pixel thickness in the image

Task 2

Construct a histogram of the brightness of the image. Apply the operation "linear stretching of brightness" and "the gray world" to the image. Operation "the gray world" needs to be realized independently. The mean can be called three times.

Task 3

Shake the image with a noise like "salt and pepper". Suppress noise with a median filter. Map the difference between the original and noisy images, and between the adjusted and the original.

Task 4

Apply the Canny and Sobel edge selection filters to the image. Try different options. Output the result of the selection of edges on the original color image in red.


In [1]:
from scipy import ndimage
from skimage import color
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
import random
import cv2
%matplotlib inline
im1 = img.imread('./images1.jpg')
greyim1 =color.rgb2gray(im1)
fig = plt.figure(figsize=(20,20))

def sp_noise(image,prob):

# Add salt and pepper noise to image
# prob: Probability of the noise

    output = np.zeros(image.shape,np.uint8)
    thres = 1 - prob 
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            ran = random.random()
            if ran < prob:
                output[i][j] = 0
            elif ran > thres:
                output[i][j] = 100
            else:
                output[i][j] = image[i][j]
    return output

imagee = cv2.imread('./images1.jpg',0) # Only for grayscale image
image =color.rgb2gray(imagee)

noise_img = sp_noise(image,0.05)
cv2.imwrite('sp_noise.jpg', noise_img)

k1 = np.array([[1 / 16,2 / 16,1 / 16],[2 / 16,4 / 16,2 / 16],[1 / 16,2 / 16,1 / 16]])
flt1 = ndimage.convolve(noise_img, k1, mode='nearest', cval=0.0)

k2 = np.array([[0 / 5, 1 / 5, 0 / 5], [1 / 5, 1 / 5, 1 / 5],[0 / 5, 1 / 5, 0 / 5]])
flt2 = ndimage.convolve(noise_img, k2, mode='nearest', cval=0.0)

k3 = np.array([[-1, -2, -1], [-2, 12, -2],[-1, -2, -1]])
flt3 = ndimage.convolve(noise_img, k3, mode='nearest', cval=0.0)


plt.subplot(131)
plt.imshow(noise_img,cmap='gray')

plt.subplot(132)
plt.imshow(flt1,cmap='gray')

plt.subplot(133)
plt.imshow(flt2,cmap='gray')


Out[1]:
<matplotlib.image.AxesImage at 0x45482914e0>

In [ ]:


In [7]:
#Task 4 canny
from skimage import color
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi

from skimage import io
from skimage import data
from skimage import feature




# Generate noisy image of a square

im = simg.imread('./canveyim.jpg') #why is not reading?
im = ndi.gaussian_filter(im, 4)

# Compute the Canny filter for two values of sigma
edges1 = feature.canny(im)
edges2 = feature.canny(im, sigma=3)

# display results
fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3),
                                    sharex=True, sharey=True)

ax1.imshow(im, cmap=plt.cm.gray)
ax1.axis('off')
ax1.set_title('noisy image', fontsize=20)

ax2.imshow(edges1, cmap=plt.cm.gray)
ax2.axis('off')
ax2.set_title('Canny filter, $\sigma=1$', fontsize=20)

ax3.imshow(edges2, cmap=plt.cm.gray)
ax3.axis('off')
ax3.set_title('Canny filter, $\sigma=3$', fontsize=20)

fig.tight_layout()

plt.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-c7c7627b10bd> in <module>()
     14 # Generate noisy image of a square
     15 
---> 16 im = simg.imread('./canveyim.jpg') #why is not reading?
     17 im = ndi.gaussian_filter(im, 4)
     18 

NameError: name 'simg' is not defined

In [8]:
#Task4 sobol

from skimage.filters import sobel
edge_sobel = sobel(data.camera())
plt.imshow(edge_sobel, cmap = 'gray')


Out[8]:
<matplotlib.image.AxesImage at 0x454b029a90>

In [9]:
#Task 2 histogramma
import numpy as np
from skimage import data
img1 = img.imread('C:/Users/HP/Desktop/GazizMV/midterm/images1.jpg')
values, bins = np.histogram(img1, bins=np.arange(256))
fig, ax1 = plt.subplots(figsize=(8, 4))
ax1.plot(bins[:-1], values, lw=2, c='k')
ax1.set_xlim(xmax=256)
ax1.set_yticks([0, 400])
ax1.set_aspect(.2)
ax1.set_title('Histogram', fontsize=24)


Out[9]:
<matplotlib.text.Text at 0x454b2a4b70>

In [1]:
import matplotlib.pyplot as plt
import matplotlib.image as img
from scipy import ndimage as nimg

pic = img.imread('img3.jpg')
gflt = nimg.median_filter(pic, 3)

fig = plt.figure(figsize=(15, 10))
plt.subplot(131)
plt.imshow(pic, cmap='gray')
plt.subplot(132)
plt.imshow(gflt, cmap='gray')
plt.subplot(133)
plt.imshow((pic - gflt), cmap='gray')

plt.show()



In [ ]:


In [ ]: