Assignment 1.1: Image Transformation in Python

 

Images: https://drive.google.com/drive/folders/1yCfFEXnEinsWBNejbDgEsziCHdESol1D?usp=share_link


Code: 

import numpy as np
import matplotlib.pyplot as plt
im = np.arange(256)
im = im[np.newaxis,:]
im = np.repeat(im,100,axis=0)
plt.imshow(im,cmap='gray')

Processing Grayscale images


im = plt.imread(r'albert-einstein_gray.jpg')
type(im)
im.shape
im.dtype
plt.imshow(im,cmap='gray')
im
im[23,300]
im2 = im.copy()
im2[23,100] = 200
im2[23:100,40:100] = 255
plt.imshow(im2,cmap='gray')
im2[300:400,40:100] = 0
plt.imshow(im2,cmap='gray')

# Quiz Solution

im = plt.imread(r'albert-einstein_gray.jpg')
im3 = im.copy()

im3[330:430,300:400] = 255 #for left eye
im3[330:430,440:540] = 255 #for right eye

plt.imshow(im3, cmap='gray')

plt.imsave(r'albert-einstein_gray_modified.jpg',im2,cmap='gray')

# pip install opencv-contrib-python

Grayscale Images in OpenCV


import cv2
img = cv2.imread(r'albert-einstein_gray.jpg',cv2.IMREAD_GRAYSCALE)
type(img)
img.dtype
img.shape
img[23,100] = 200
plt.imshow(img,cmap='gray')

cv2.imshow('Gray',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

img[500:700,500:600] = 255

cv2.imshow('Gray',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite(r'albert-einstein_gray_opencv.jpg',img)

RGB Images


cim = plt.imread(r'tulips.jpg')
plt.imshow(cim)
cim.shape
type(cim)
cim.dtype
# Solution to Quiz 03_06
rgb = np.zeros((100,150,3), dtype='uint8')
rgb[:,0:50,0] = 255 # red
rgb[:,50:100,1] = 255 #green
rgb[:,100:150,2] = 255 #blue
 
plt.imshow(rgb)

cv2.imshow('RGB',rgb)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV reads as BGR


cim = cv2.imread(r'tulips.jpg')
cim = cim[:,:,::-1]
R = cim[:,:,0]
G = cim[:,:,1]
B = cim[:,:,2]

plt.figure(1)

plt.subplot(231)
plt.imshow(cim)
plt.xticks([])
plt.yticks([])

plt.subplot(232)
plt.imshow(cim)
plt.xticks([])
plt.yticks([])

plt.subplot(233)
plt.imshow(cim)
plt.xticks([])
plt.yticks([])

plt.subplot(234)
plt.imshow(R,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.title('Red')

plt.subplot(235)
plt.imshow(G,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.title('Green')

plt.subplot(236)
plt.imshow(B,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.title('Blue')
plt.show()

R[100:400,100:400] = 100
G[100:400,100:400] = 50
B[100:400,100:400] = 70
cim[:,:,0] = R
cim[:,:,1] = G
cim[:,:,2] = B

plt.figure(1)

plt.subplot(231)
plt.imshow(cim)
plt.xticks([])
plt.yticks([])

plt.subplot(232)
plt.imshow(cim)
plt.xticks([])
plt.yticks([])

plt.subplot(233)
plt.imshow(cim)
plt.xticks([])
plt.yticks([])

plt.subplot(234)
plt.imshow(R,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.title('Red')

plt.subplot(235)
plt.imshow(G,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.title('Green')

plt.subplot(236)
plt.imshow(B,cmap='gray')
plt.xticks([])
plt.yticks([])
plt.title('Blue')
plt.show()


RGB to HSV conversion


def f_rgb_to_hsv(r, g, b,scaleFactor): 
    r, g, b = r / 255.0, g / 255.0, b / 255.0
    cmax = max(r, g, b)    # maximum of r, g, b 
    cmin = min(r, g, b)    # minimum of r, g, b 
    diff = cmax-cmin       # diff of cmax and cmin. 
    if cmax == cmin:  
        h = 0
    elif cmax == r:  
        h = (60 * ((g - b) / diff) + 0) % 360
    elif cmax == g: 
        h = (60 * ((b - r) / diff) + 120) % 360
    elif cmax == b: 
        h = (60 * ((r - g) / diff) + 240) % 360
   
    if h < 0:
        h = h + 360
    if cmax == 0: 
        s = 0
    else: 
        s = (diff / cmax) * scaleFactor
    v = cmax * scaleFactor
    return h, s, v 

print(f_rgb_to_hsv(100,200,50,100))

im = cv2.imread(r'tulips.jpg')

HsvIm = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
cv2.cvtColor(im,cv2.COLOR_HSV2BGR)

type(HsvIm)

HsvIm.shape

img = cv2.imread(r'tulips.jpg')

plt.imshow(img[:,:,::-1])

hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

lr = np.array([0,120,70])
ur = np.array([8,255,255])
mask1 = cv2.inRange(hsv,lr,ur)

type(mask1)
mask1.dtype
mask1.shape

lr = np.array([170,120,70])
ur = np.array([180,255,255])
mask2 = cv2.inRange(hsv,lr,ur)

np.unique(mask2)
np.unique(mask1)
mask = mask1 | mask2
np.unique(mask)
res = cv2.bitwise_and(img,img,mask=mask)

plt.figure(1)
plt.subplot(121)
plt.imshow(img[:,:,::-1])
plt.subplot(122)
plt.imshow(res[:,:,::-1])
plt.show()


# solution to quiz

lr = np.array([20,100,100])
ur = np.array([30,255,255])
yellow_mask = cv2.inRange(hsv,lr,ur)
res = cv2.bitwise_and(img,img,mask=yellow_mask)

plt.figure(1)
plt.subplot(121)
plt.imshow(img[:,:,::-1])
plt.subplot(122)
plt.imshow(res[:,:,::-1])
plt.show()

Comments

Popular posts from this blog

Assignment 4: Canny Edge Detection

Assignment 1.2 - Transformation