For solving this problem set, I wrote a small library of helper functions called ps0. It contains some useful functions to help in better solving the problems. It's imported in the next cell below.
This file was assignment was done in a super rush, with the assumption that Done is better than Good. So it might be buggy.
However, if you spot anything you don't understand, I'm super glad to help. You can email me at ralphwantek add gmail dot com. Cheers!
In [3]:
# First, import some useful libraries.
import numpy
import cv2
import os
from ps0 import *
import matplotlib.pyplot as plt
%matplotlib inline
In [4]:
man_path = 'input/ps0-1-a-2.jpeg'
driver_path = 'input/ps0-1-a-1.jpg'
driver = cv2.imread(driver_path)
man = cv2.imread(man_path)
#print man
show_img(man)
In [4]:
show_img(driver)
In [5]:
driver_bgr = driver[:,:,::-1]
cv2.imwrite('output/ps0-2-a-1.png', driver_bgr)
Out[5]:
In [6]:
show_img(driver_bgr)
In [6]:
driver_green = driver[:,:,1]
# man_green = man[:,:,1]
cv2.imwrite('output/ps0-2-b-1.png', driver_green)
Out[6]:
In [8]:
show_img(driver_green)
In [9]:
driver_red = driver[:,:,0]
cv2.imwrite('output/ps0-2-c-1.png', driver_red)
Out[9]:
In [10]:
show_img(driver_red)
In [11]:
show_multiple_images({'Driver_green': driver_green,
'Driver_red': driver_red})
I'd expect the Green Image to look more like a monochrome image. I'd expect a Computer Vision Algorithm to work better on the Green image, because the distinction between pixel intensities seem to be better.
In [62]:
driver_man = replace_100_pix(driver, man)
cv2.imwrite('output/ps0-3-a-1.png', driver_man)
Out[62]:
In [64]:
show_img(driver_man)
In [216]:
min_driver_green = np.amin(driver_green)
max_driver_green = np.amax(driver_green)
mean_driver_green = np.mean(driver_green)
std_img1_green = np.std(driver_green)
print mean_driver_green
I tried to solve it in two ways:
Based on my approaches, could anyone tell me if I'm right or wrong? Thank you!
Update: I could get the numpy version to work, using np.clip
.
In [227]:
driver_norm_float = driver_green - mean_driver_green
driver_green_norm = np.empty(driver_green.shape, dtype='uint8')
driver_green_norm = np.clip(driver_norm_float, 0, 255, out=driver_green_norm)
In [214]:
show_img(driver_norm)
In [232]:
#driver_green_norm_2 = np.empty(driver_green.shape, dtype='uint8')
driver_green_norm_float_2 = driver_green_norm*10.0
#print driver_green_norm_float_2
driver_green_norm_2 = np.empty(driver_green.shape, dtype='uint8')
driver_green_norm_2 = np.clip(driver_green_norm_float_2, 0, 255, out=driver_green_norm_2)
show_img(driver_green_norm_2)
In [235]:
print center_100_pixels(driver_green_norm_3), '\n'
print center_100_pixels(driver_green_norm_2), '\n'
print center_100_pixels(driver_green_norm), '\n'
print center_100_pixels(driver_green), '\n'
In [238]:
driver_green_norm_3_float = driver_green_norm_2 + mean_driver_green
driver_green_norm_3 = np.empty(driver_green.shape, dtype='uint8')
driver_green_norm = np.clip(driver_green_norm_3_float, 0, 255, out=driver_green_norm_3)
show_multiple_images({'driver_green_norm_3':driver_green_norm_3,
'driver_green_norm_2':driver_green_norm_2})
In [239]:
cv2.imwrite('output/ps0-4-b-1.png', driver_green_norm_3)
Out[239]:
In [11]:
driver_green_shifted = shift_img(driver_green)
In [18]:
show_multiple_images({'driver_green_shifted':driver_green_shifted,
'driver_green':driver_green})
In [20]:
cv2.imwrite('output/ ps0-4-c-1.png', driver_green_shifted)
Out[20]:
In [21]:
driver_subtracted = cv2.subtract(driver_green, driver_green_shifted)
In [22]:
show_img(driver_subtracted)
In [34]:
print driver_green[-10:,-10:]
In [33]:
print driver_green_shifted[-10:,-10:]
In [35]:
print driver_subtracted[-10:,-10:]
In [36]:
cv2.imwrite('output/ ps0-4-d-1.png', driver_subtracted)
Out[36]:
Negative Pixels mean that the resulting pixel of the image will be zero, using 8-bits unsigned integers.
In [101]:
grid = np.random.randn(driver.shape[0], driver.shape[1])*0.4
grid = grid.astype('uint8')
driver_green_gaussian = cv2.add(driver_green, grid)
In [118]:
noise = np.empty(driver_green.shape, np.uint8)
noise = cv2.randn(noise, 0, 20)
driver_green_gaussian = cv2.add(driver_green, noise)
In [119]:
show_img(driver_green_gaussian)
In [120]:
cv2.imwrite('output/ ps0-5-a-1.png', driver_green_gaussian)
Out[120]:
In [104]:
driver_blue = driver[:,:,2]
driver_blue_gaussian = cv2.add(driver_green, grid)
In [105]:
show_img(driver_blue_gaussian)
In [106]:
show_multiple_images({'driver_green_gaussian':driver_green_gaussian,
'driver_blue_gaussian':driver_blue_gaussian})
In [107]:
cv2.imwrite('output/ ps0-5-b-1.png', driver_blue_gaussian)
Out[107]: