import numpy as np # numerical python
from PIL import Image # Python Image Library
import matplotlib.pyplot as plt # displaying images
import matplotlib
import cv2
%matplotlib inline
PIL, Matplotlib: BBG
OpenCV: BGR
# Reading image using PIL
img = Image.open('./data/test.jpg')
img
# loading using matplotlib : (RBG Red Blue Green)
img_mat = matplotlib.image.imread("./data/test.jpg")
img_mat
array([[[98, 52, 26], [94, 48, 22], [89, 43, 17], ..., [11, 12, 0], [13, 14, 0], [14, 15, 1]], [[95, 49, 23], [94, 48, 22], [91, 45, 19], ..., [12, 13, 0], [14, 15, 1], [15, 16, 2]], [[91, 45, 19], [93, 47, 21], [93, 47, 21], ..., [13, 14, 0], [14, 15, 1], [15, 16, 2]], ..., [[96, 66, 40], [93, 63, 37], [91, 61, 35], ..., [73, 94, 0], [76, 97, 2], [76, 97, 2]], [[94, 64, 38], [91, 61, 35], [91, 61, 35], ..., [70, 91, 0], [74, 95, 0], [74, 95, 0]], [[91, 61, 35], [90, 60, 34], [92, 62, 36], ..., [70, 91, 0], [76, 97, 2], [75, 96, 1]]], dtype=uint8)
# read image using opencv : BGR (Blue Green and Red)
img_cv = cv2.imread('./data/test.jpg')
img_cv
array([[[26, 52, 98], [22, 48, 94], [17, 43, 89], ..., [ 0, 12, 11], [ 0, 14, 13], [ 1, 15, 14]], [[23, 49, 95], [22, 48, 94], [19, 45, 91], ..., [ 0, 13, 12], [ 1, 15, 14], [ 2, 16, 15]], [[19, 45, 91], [21, 47, 93], [21, 47, 93], ..., [ 0, 14, 13], [ 1, 15, 14], [ 2, 16, 15]], ..., [[40, 66, 96], [37, 63, 93], [35, 61, 91], ..., [ 0, 94, 73], [ 2, 97, 76], [ 2, 97, 76]], [[38, 64, 94], [35, 61, 91], [35, 61, 91], ..., [ 0, 91, 70], [ 0, 95, 74], [ 0, 95, 74]], [[35, 61, 91], [34, 60, 90], [36, 62, 92], ..., [ 0, 91, 70], [ 2, 97, 76], [ 1, 96, 75]]], dtype=uint8)
# display image
plt.imshow(img_mat) # rgb
<matplotlib.image.AxesImage at 0x7f9430e712e0>
# display image
plt.imshow(img_cv)
<matplotlib.image.AxesImage at 0x7f9450b80d00>
img_mat.shape
(1200, 1600, 3)
# split image into three colors
# split array into three parts
r = img_mat[:,:,0] # red array
g = img_mat[:,:,1] # green array
b = img_mat[:,:,2] # blue array
r.shape,g.shape,b.shape
((1200, 1600), (1200, 1600), (1200, 1600))
plt.figure(figsize=(10,6))
plt.subplot(1,3,1)
plt.title('Red region')
plt.imshow(r,cmap='gray')
plt.subplot(1,3,2)
plt.title('Green region')
plt.imshow(g,cmap='gray')
plt.subplot(1,3,3)
plt.title('Blue region')
plt.imshow(b,cmap='gray')
plt.show()
# extract information from the image
# gray scale : 2d (grayscale = 0.3 * red array + 0.59 * green array + 0.11 * blue array)
gray_MAT = cv2.cvtColor(img_mat,cv2.COLOR_RGB2GRAY)
# gray scale : 2d
gray_CV = cv2.cvtColor(img_cv,cv2.COLOR_BGR2GRAY)
gray_MAT.shape, gray_CV.shape
((1200, 1600), (1200, 1600))
plt.imshow(gray_MAT,cmap='gray')
<matplotlib.image.AxesImage at 0x7f944182d880>
plt.imshow(gray_CV,cmap='gray')
<matplotlib.image.AxesImage at 0x7f9450f64520>
gray_CV
array([[63, 59, 54, ..., 10, 12, 13], [60, 59, 56, ..., 11, 13, 14], [56, 58, 58, ..., 12, 13, 14], ..., [72, 69, 67, ..., 77, 80, 80], [70, 67, 67, ..., 74, 78, 78], [67, 66, 68, ..., 74, 80, 79]], dtype=uint8)
arr = np.arange(0,100,1)
arr1 = arr.reshape((10,10))
plt.imshow(arr1,cmap='gray')
<matplotlib.image.AxesImage at 0x7f9471529520>
arr2 = np.random.randint(0,150,(10,10))
plt.imshow(arr2,cmap='gray')
<matplotlib.image.AxesImage at 0x7f94315a6820>
img = cv2.imread('data/test.jpg') # bgr
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
plt.imshow(gray,cmap='gray')
<matplotlib.image.AxesImage at 0x7f9471486bb0>
slice = gray[0:10,0:10]
plt.imshow(slice,cmap='gray')
<matplotlib.image.AxesImage at 0x7f94512c3ca0>
cv2.INTER_AREA
cv2.INTER_CUBIC
(slow & good) or cv2.INTER_LINEAR
(fast & ok)# reading image
img = cv2.imread('./data/test.jpg') # bgr
# convert into grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(gray.shape)
plt.imshow(gray,cmap='gray')
plt.show()
(1200, 1600)
# shrink (w=1600,h=1200)
# (w=500,h=400)
img_re = cv2.resize(gray,(500,400),cv2.INTER_AREA)
print(img_re.shape)
plt.imshow(img_re,cmap='gray')
plt.show()
(400, 500)
# englarge (w=2000, h = 1600)
img_en = cv2.resize(gray,(2000,1600),cv2.INTER_CUBIC)
print(img_en.shape)
plt.imshow(img_en,cmap='gray')
plt.show()
(1600, 2000)
# read image
img = cv2.imread('./data/root_female.jpg')
# convert into gray scale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# apply haar cascade classifier
haar = cv2.CascadeClassifier('./data/haarcascade_frontalface_default.xml')
# detect face
faces = haar.detectMultiScale(gray,1.3,5) # gray scale, scale size = 1.3, 5 neighbors
# [x, y, width, height] of the found face
print(faces)
[[127 55 205 205]]
cv2.rectangle(img,(127,55),(127+205,55+205),(0,255,0),3)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f94418a9b50>
# pop up the image
# cv2.imshow('object_detect',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# crop
face_crop = img[55:55+205,127:127+205]
plt.imshow(face_crop)
<matplotlib.image.AxesImage at 0x7f94715b4610>
# extract the face from the image and save
cv2.imwrite('./data/root_female_extracted.png',face_crop)
True
# read image
img = cv2.imread('./data/multi-people.jpg')
# convert into gray scale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# apply haar cascade classifier
haar = cv2.CascadeClassifier('./data/haarcascade_frontalface_default.xml')
# detect face
faces = haar.detectMultiScale(gray,1.3,2) # gray scale, scale size = 1.3, 5 neighbors
print(gray.shape)
plt.imshow(gray,cmap='gray')
plt.show()
(200, 600)
print(faces)
for face in faces:
image = cv2.rectangle(img,(face[0],face[1]),(face[0]+face[2],face[1]+face[3]),(0,255,255),3)
plt.imshow(image)
[[ 46 19 38 38] [276 33 41 41] [129 42 37 37] [379 40 39 39] [537 40 38 38]]
# haar = cv2.CascadeClassifier('./data/haarcascade_frontalface_default.xml')
# def face_detect(img):
# gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# faces = haar.detectMultiScale(gray,1.3,5)
# for x,y,w,h in faces:
# cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)
# return img
# cap = cv2.VideoCapture('./data/video.mp4')
# while True:
# ret,frame = cap.read()
# if ret == False:
# break
# frame = face_detect(frame)
# cv2.imshow('object_detect',frame)
# #cv2.imshow('gray',gray)
# if cv2.waitKey(20) == 27:
# break
# cv2.destroyAllWindows()
# cap.release()