This notebook covers:

Lesson 2A-L1 Images as Functions - Lesson 2C-L3 Aliasing

Import Libraries and Dependencies

In [2]:
import cv2
import os
import matplotlib.pyplot as plt
%matplotlib inline

IMG = 'imgs/'
Some Helper Functions

In [26]:
def show_img(img):
    """
    Function takes an image, and shows the image using pyplot.
    The image is shown in RGB
    """
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(img)
Load and Display an Image

In [4]:
img_path = os.path.join(IMG, 'sea.jpeg')
img = cv2.imread(img_path)
# What is the size of the Image?
print img.shape
# What is the class of the image?
print img.dtype
show_img(img)


(3264, 4928, 3)
uint8
Inspect Image Values

My image has three channels: Red, Green, and Blue. That's why when selecting the 50th row and 100th column, three values are returned.


In [5]:
img[50,100]


Out[5]:
array([238, 217, 196], dtype=uint8)

In [6]:
plt.plot(img[1500,:,2])


Out[6]:
[<matplotlib.lines.Line2D at 0x10d4a2a50>]

TODO: Extract a 2D slice between rows 101 to 103 and columns 201 to 203 (inclusive)


In [7]:
extraction = img[101:103,201:203]
Crop Image

In [8]:
cropped = img[1700:,300:,:]

In [9]:
show_img(cropped)



In [55]:
# Size of Cropped Image
cropped.shape


Out[55]:
(1564, 4628, 3)
Color Planes

In [36]:
img_green = img[:,:,1]


Out[36]:
(3264, 4928, 3)

In [40]:
plt.imshow(img_green, cmap='gray')


Out[40]:
<matplotlib.image.AxesImage at 0x1105f42d0>
Adding Pixels

In [48]:
# Load the Dog Image
dog_path = os.path.join(IMG, 'dog.jpeg')
dog = cv2.imread(dog_path)
#dog = cv2.resize
dog = cv2.resize(dog, (img.shape[1], img.shape[0]))
show_img(dog)



In [49]:
summed = dog + img

In [ ]: