Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions python/030.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 06 20:57:23 2016

@author: Parshu Rath
"""

import os
import sys
#from IPython.display import Image
import matplotlib.pyplot as plt
#import time
import numpy as np
import scipy
import scipy.misc
from scipy import ndimage
from scipy import misc
from skimage import transform as tf
import math
import re

"""
Define transformation functons.
"""

#Flip the image
def flipt(f):
return(np.flipud(f))

#Rotate at some random angle transform
def rotatet(f):
angle = math.floor(np.random.uniform(-90, 90, 1))
return (ndimage.rotate(f, angle, reshape=False))

#Add some random noise transform
def noisyt(f):
noisy = f + 0.9 * f.std() * np.random.random(f.shape)
return (f)

#Add Gaussian transform Filter
def gausst(f):
f1 = ndimage.gaussian_filter(f, sigma=3)
return(f1)

#Add Uniform transform Filter
def uniformt(f):
f1 = ndimage.uniform_filter(f, size=10)
return(f1)

#Add Median transform Filter
def mediant(f):
f1 = ndimage.median_filter(f, size=10)
return(f1)

#Add Fourrier ellipsoid transform Filter
def fouriert(f):
f1 = ndimage.fourier_ellipsoid(f, size=1.0)
return(f1)

#Add Fourrier uniform transform Filter
def fourierut(f):
f1 = ndimage.fourier_uniform(f, size=1.25)
return(f1)

#Add Affine transform Filter
def affinet(f):
H = np.array([[1.4,0.05,-100],[0.05,1.4,-100],[0,0,1]])
f1 = ndimage.affine_transform(f,H[:2,:2],(H[0,2],H[1,2]))
return(f1)

#Add Geometric transform Filter
def shift_func(output_coordinates):
return (output_coordinates[0] - 10.5, output_coordinates[1] - 10.5)
def geomt(f):
H = np.array([[1.4,0.05,-100],[0.05,1.4,-100],[0,0,1]])
f1 = ndimage.geometric_transform(f, shift_func)
return(f1)

"""
Below is an example of applying the above transformations to images in a directory
and saving the transformed images.
"""
#List of trsansformations
transforms = [flipt, rotatet, noisyt, gausst, uniformt, mediant, fouriert, fourierut, affinet, geomt]
#Source image directory
imgdir = r'C:\pr\CUNY\MSDA Fall 2016\DATA 622\Kaggle Project\img10'
#Get names of the images
file_list = os.listdir(imgdir)
#Directory to save the transformed images
img_savedir = r'C:\pr\CUNY\MSDA Fall 2016\DATA 622\Kaggle Project\trans_img'

#Transform the files and save.

#import re
for f in file_list:
#use one transformation at a time
for i in range(0, len(transforms)):
#read the image file
f1 = misc.imread(''.join([imgdir, '\\', f]))
#transform the image
f1t = transforms[i](f1)
#save the image after adding '-[i]t' in the file name
scipy.misc.imsave(''.join([img_savedir, '\\', re.sub(r'.jpg', '-', f), str(i),'t.jpg']), f1t)

113 changes: 113 additions & 0 deletions python/nmg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#transform 500 images 10 times using opencv library
import cv2
import numpy as np
from matplotlib import pyplot as plt
from os import listdir
from os.path import isfile, join

mypath='/users/nathangroom/desktop/new_docker/500_photos_2'
#first transformation - rotate image 90 degrees
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos1/newphoto1_'+str(i)+'.jpg', dst)

#second transformation - shift image
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows,cols = img.shape
M = np.float32([[1,0,200],[0,1,90]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos2/newphoto2_'+str(i)+'.jpg', dst)

#third transformation -- affine transformation
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i])
rows,cols,ch = img.shape
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))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos3/newphoto3_'+str(i)+'.jpg', dst)

#fourth transformation -- perspective transformation
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i])
rows,cols,ch = img.shape
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))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos4/newphoto4_'+str(i)+'.jpg', dst)

#fifth transformation -- rotation 190 degrees
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),190,1)
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos5/newphoto5_'+str(i)+'.jpg', dst)

#sixth transformation -- combination of transformations 1 and 2
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows, cols=img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst = cv2.warpAffine(img,M,(cols,rows))
M2=M = np.float32([[1,0,100],[0,1,45]])
dst2= cv2.warpAffine(dst,M2,(cols,rows))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos6/newphoto6_'+str(i)+'.jpg', dst2)

#seventh transformation -- combination of transformations 1 and 3
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),270,1)
dst = cv2.warpAffine(img,M,(cols,rows))
rows,cols=dst.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M2=cv2.getAffineTransform(pts1,pts2)
dst2=cv2.warpAffine(dst,M2,(cols,rows))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos7/newphoto7_'+str(i)+'.jpg', dst2)

#eighth tranformation -- a combination of transformations 1 and 4
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),200,1)
dst = cv2.warpAffine(img,M,(cols,rows))
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M2 = cv2.getPerspectiveTransform(pts1,pts2)
dst2 = cv2.warpPerspective(dst,M2,(300,300))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos8/newphoto8_'+str(i)+'.jpg', dst2)

#ninth transformation -- combination of transformations 2 and 4
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i],0)
rows,cols = img.shape
M = np.float32([[1,0,95],[0,1,55]])
dst = cv2.warpAffine(img,M,(cols,rows))
pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M2 = cv2.getPerspectiveTransform(pts1,pts2)
dst2= cv2.warpPerspective(img,M2,(300,300))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos9/newphoto9_'+str(i)+'.jpg', dst2)

#tenth transformation--combination of transformations 3 and 4
for i in range(1,500):
img=cv2.imread('/users/nathangroom/desktop/new_docker/500_photos_2/'+listdir(mypath)[i])
rows,cols,ch = img.shape
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))

pts3 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pts4 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M2 = cv2.getPerspectiveTransform(pts3,pts4)
dst2= cv2.warpPerspective(dst,M2,(300,300))
cv2.imwrite('/users/nathangroom/desktop/new_docker/new_photos10/newphoto10_'+str(i)+'.jpg', dst2)