-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_augmentation.py
132 lines (110 loc) · 4.1 KB
/
data_augmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 21 18:59:05 2018
@author: alok
"""
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('0a0c223352985ec154fd604d7ddceabd.jpg')
imgg = cv2.resize(img,(60,60), interpolation = cv2.INTER_CUBIC)
#cv2.imshow('image',photo)
plt.imshow(img)
rows,cols,depth = img.shape
#M = np.float32([[1,0,100],[0,1,50]])
#shft = cv2.warpAffine(img,M,(cols,rows))
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(shft),plt.title('Output')
def rotate_image(img,angle):
rows,cols,depth = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)
rot = cv2.warpAffine(img,M,(cols,rows))
return rot
#M = cv2.getRotationMatrix2D((cols/2,rows/2),45,1)
#rot = cv2.warpAffine(img,M,(cols,rows))
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(rot),plt.title('Output')
def shear_image(img):
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))
return dst
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(dst),plt.title('Output')
#plt.show()
def perspective_image(img):
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,300))
return dst
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(dst),plt.title('Output')
#plt.show()
def average_image(img):
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
return dst
#
#plt.subplot(121),plt.imshow(img),plt.title('Original')
#plt.xticks([]), plt.yticks([])
#plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
#plt.xticks([]), plt.yticks([])
#plt.show()
#blur = cv2.blur(img,(5,5))
#plt.subplot(121),plt.imshow(img),plt.title('Original')
#plt.xticks([]), plt.yticks([])
#plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
#plt.xticks([]), plt.yticks([])
#plt.show()
#
#blur = cv2.GaussianBlur(img,(5,5),0)
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(blur),plt.title('gaussian blur')
#
#median = cv2.medianBlur(img,5)
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(median),plt.title('median blur')
def blur_image(img):
blur = cv2.bilateralFilter(img,9,75,75)
return blur
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(blur),plt.title('bilateralFilter blur')
#laplacian = cv2.Laplacian(img,cv2.CV_64F)
#sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
#sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
#plt.subplot(2,2,1),plt.imshow(img,cmap = 'gray')
#plt.title('Original'), plt.xticks([]), plt.yticks([])
#plt.subplot(2,2,2),plt.imshow(laplacian,cmap = 'gray')
#plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
#plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray')
#plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
#plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray')
#plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
#plt.show()
#rows, cols,c = img.shape
#crow,ccol = rows/2 , cols/2
## create a mask first, center square is 1, remaining all zeros
#mask = np.zeros((rows,cols,2),np.uint8)
#mask[crow-30:crow+30, ccol-30:ccol+30] = 1
## apply mask and inverse DFT
#fshift = cv2.dft_shift*mask
#f_ishift = np.fft.ifftshift(fshift)
#img_back = cv2.idft(f_ishift)
#img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1])
#plt.subplot(121),plt.imshow(img, cmap = 'gray')
#plt.title('Input Image'), plt.xticks([]), plt.yticks([])
#plt.subplot(122),plt.imshow(img_back, cmap = 'gray')
#plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
#plt.show()
def crop_image(img,x = 10,y = 10):
row = img.shape[1] - x
col = img.shape[0] - y
crop_img = img[x:row,y:col]
return crop_img
#
#plt.imshow(crop_img)
#plt.subplot(121),plt.imshow(img),plt.title('Input')
#plt.subplot(122),plt.imshow(crop_img),plt.title('crop_img')