Skip to content

Commit

Permalink
conda rotation of image
Browse files Browse the repository at this point in the history
  • Loading branch information
dipsywong98 committed Jul 6, 2018
1 parent f4cdffa commit fe5fcd6
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 26 deletions.
51 changes: 25 additions & 26 deletions conda/extract_frame.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import cv2
import numpy as np

path = 'img/test2/test2.mp4'

vidcap = cv2.VideoCapture(path)
read_success = True
prev = None
count = 0
while read_success:
read_success,image = vidcap.read()
flag = False
if(read_success):
if type(prev) == type(None):
flag = True
else:
diff = cv2.subtract(prev, image)
if np.any(diff):
diff = np.linalg.norm(diff)
print('frameid %d diff %d'%(count,diff))
if diff > 2000:
flag = True
if flag:
filename = path+"_frame%d.jpg" % count
cv2.imwrite(filename , image) # save frame as JPEG file
count += 1
print('output a new frame', filename)
prev = image
#print('Read a new frame: ', read_success)
def extract(path = 'img/test2/test2.mp4'):
vidcap = cv2.VideoCapture(path)
read_success = True
prev = None
count = 0
while read_success:
read_success,image = vidcap.read()
flag = False
if(read_success):
if type(prev) == type(None):
flag = True
else:
diff = cv2.subtract(prev, image)
if np.any(diff):
diff = np.linalg.norm(diff)
print('frameid %d diff %d'%(count,diff))
if diff > 2000:
flag = True
if flag:
filename = path+"_frame%d.jpg" % count
cv2.imwrite(filename , image) # save frame as JPEG file
count += 1
print('output a new frame', filename)
prev = image
#print('Read a new frame: ', read_success)
158 changes: 158 additions & 0 deletions conda/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 18 21:39:57 2018
generate world view transformation matrix
@author: dipsy
"""
import numpy as np
from PIL import Image
import json


path = "tilted.jpg"
newsize = (200,240)
src = [[43,34], [111,35], [22,84], [128,86]]
tgt = [[50,100],[140,100],[50,190],[140,190]]

"""
pj = np.squeeze(np.asarray(np.matrix('''
1 0 0;
0 1 0;
0 0 1
''')))
"""

"""
brief: generate a static array which map newsize to oldsize
param: coord in newsize
return: coord in oldsize
"""
def generate(h,name):
f = open(name+".py","w")
f2 = open("../openMV/"+name+".py","w")
h=list(h)
h[0] = list(h[0])
h[1] = list(h[1])
h[2] = list(h[2])
pyContent = name+" = "+json.dumps(list(h)).replace("[","(").replace("]",")")
f.write(pyContent)
f2.write(pyContent)
f.close()
f2.close()

"""
brief: given arrays of source points and target points
return a projection matrix that map source points to target points
link: https://stackoverflow.com/questions/35819142/calculate-a-2d-homogeneous-perspective-transformation-matrix-from-4-points-in-ma
link: https://math.stackexchange.com/questions/96662/augmented-reality-transformation-matrix-optimization
"""
def getProjMatrix(s,t):
N = [[0] * 13 for i in range(13)]
#[[x1,y1],[x2,y2],[x3,y3],[x4,y4]] = s
#[[_x1,_y1],[_x2,_y2],[_x3,_y3],[_x4,_y4]] = t
N[0][0] = N[1][3] = N[2][6] = s[0][0] #x1
N[3][0] = N[4][3] = N[5][6] = s[1][0] #x2
N[6][0] = N[7][3] = N[8][6] = s[2][0] #x3
N[9][0] = N[10][3] = N[11][6] = s[3][0]#x4
N[0][1] = N[1][4] = N[2][7] = s[0][1]
N[3][1] = N[4][4] = N[5][7] = s[1][1]
N[6][1] = N[7][4] = N[8][7] = s[2][1]
N[9][1] = N[10][4] = N[11][7] = s[3][1]
N[0][2] = N[1][5] = N[2][8] = 1
N[3][2] = N[4][5] = N[5][8] = 1
N[6][2] = N[7][5] = N[8][8] = 1
N[9][2] = N[10][5] = N[11][8] = 1
N[0][9] = -t[0][0]
N[1][9] = -t[0][1]
N[2][9] = -1
N[3][10] = -t[1][0]
N[4][10] = -t[1][1]
N[5][10] = -1
N[6][11] = -t[2][0]
N[7][11] = -t[2][1]
N[8][11] = -1
N[9][12] = -t[3][0]
N[10][12] = -t[3][1]
N[11][12] = -1
N[12][8] = 1
y = [0]*13
y[12] = 1
y = np.transpose(y)
x = np.linalg.solve(N,y)
print(x)
p = [[x[0],x[1],x[2]],[x[3],x[4],x[5]],[x[6],x[7],x[8]]]
return p

def projmap (pj,src):
[[a,b,c],[d,e,f],[g,h,_]] = pj
[x,y] = src
_x = int((a*x+b*y+c)//(g*x+h*y+1))
_y = int((d*x+e*y+f)//(g*x+h*y+1))
#if(_x < 0 or _y < 0): print (src,(x,y))
return (_x,_y)

def pm2(h,s):
[x,y]=s
return (int((h[0][0]*x+h[0][1]*y+h[0][2])/(h[2][0]*x+h[2][1]*y+h[2][2])),\
int((h[1][0]*x+h[1][1]*y+h[1][2])/(h[2][0]*x+h[2][1]*y+h[2][2])))

"""
link: link: https://math.stackexchange.com/questions/96662/augmented-reality-transformation-matrix-optimization
"""
def getProjMatrix2(s,t):
[[x1,y1],[x2,y2],[x3,y3],[x4,y4]] = s
[[_x1,_y1],[_x2,_y2],[_x3,_y3],[_x4,_y4]] = t
#N = [[x1,y1,1,0,0,0,-x1*_x1,-y1*_x1],[x2,y2,1,0,0,0,-x2*_x2,-y2*_x2],[x3,y3,1,0,0,0,-x3*_x3,-y3,_x3],[x4,y4,1,0,0,0,-x4*_x4,-y4,_x4],[0,0,0,x1,y1,1,-x1*_y1,-y1*_y1],[0,0,0,x2,y2,1,-x2*_y2,-y2*_y2],[0,0,0,x3,y3,1,-x3*_y3,-y3*_y3],[0,0,0,x4,y4,1,-x4*_y4,-y4*_y4]]
#y = [0]*8
#for i in range(4): y[i]=t[i][0];y[i+4]=t[i][1]
#y = np.transpose(y)
#print(N,y)
#print(y.dtype)
N = [[x1, y1, 1, 0, 0, 0, -x1 * _x1, -y1 * _x1],\
[x2, y2, 1, 0, 0, 0, -x2 * _x2, -y2 * _x2],\
[x3, y3, 1, 0, 0, 0, -x3 * _x3, -y3* _x3],\
[x4, y4, 1, 0, 0, 0, -x4 * _x4, -y4* _x4],\
[0, 0, 0, x1, y1, 1, -x1 * _y1, -y1 * _y1],\
[0, 0, 0, x2, y2, 1, -x2 * _y2, -y2 * _y2],\
[0, 0, 0, x3, y3, 1, -x3 * _y3, -y3 * _y3],\
[0, 0, 0, x4, y4, 1, -x4 * _y4, -y4 * _y4]]
print(N)
print([_x1, _x2, _x3, _x4, _y1, _y2, _y3, _y4])
x = np.linalg.solve(N,[_x1, _x2, _x3, _x4, _y1, _y2, _y3, _y4])
print(x)
p = [[x[0],x[1],x[2]],[x[3],x[4],x[5]],[x[6],x[7],1]]
return p

pj = getProjMatrix2(src,tgt)
print(pm2(pj,[0,0]))
print(pm2(pj,[0,1]))
print(pm2(pj,[1,1]))
print(pm2(pj,[1,0]))
pj_ = np.linalg.inv(pj)

jpgfile = Image.open(path)
print(jpgfile.bits, jpgfile.size, jpgfile.format)
print('new size',newsize)
newfile = Image.new('RGB',newsize)
for x in range(newsize[0]):
for y in range(newsize[1]):
try:
p = pm2(pj_,(x,y))
newfile.putpixel((x,y),jpgfile.getpixel(p))
except:
x

#for x in range(jpgfile.size[0]):
# for y in range(jpgfile.size[1]):
# try:
# p = pm2(pj,(x,y))
# newfile.putpixel(p,jpgfile.getpixel((x,y)))
# except Exception as e:
# x
#print ((x,y),p,e)
#raise
newfile.show()
newfile.save(path+"_fixed.jpg")
generate(pj_,"pj")
generate(pj,"jp")
46 changes: 46 additions & 0 deletions conda/perform_rotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 6 16:50:50 2018
@author: dipsy
"""

from PIL import Image
from math import cos, sin, pi
import json
from pprint import pprint

json_path = "img/test2/frame_rotations2.txt"
img_path = "img/test2/test2.mp4_frame60.jpgfixed.jpg"
old_size = (200,250)
rotation_origin = (95, 270)
frame_id = 17

with open(json_path) as f:
data = json.load(f)

rotations = data

fixedfile = Image.new('RGB',(500,500))
jpgfile = Image.open(img_path)

theta = -0.158650429 #rotations[str(frame_id)]
co = cos(theta)
si = sin(theta)

def rotateTransform(p):
x = p[0] - rotation_origin[0]
y = rotation_origin[1] - p[1]
x1 = co*x - si*y
y1 = si*x + co*y
return (int(x1)+2*rotation_origin[0], rotation_origin[1] - int(y1))

for x in range(old_size[0]):
for y in range(old_size[1]):
p=rotateTransform((x,y))
try:
fixedfile.putpixel(p,jpgfile.getpixel((x,y)))
except:
x

fixedfile.show()
52 changes: 52 additions & 0 deletions conda/perform_transform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 22 14:56:59 2018
@author: dipsy
"""

from PIL import Image
from jp import jp
from pj import pj

def fix(path = "img/test2.jpg", oldsize = (160,120), fixedsize = (200,250), preview = True):

jpgfile = Image.open(path)
fixedfile = Image.new('RGB',fixedsize)

def pm2(h,s):
[x,y]=s
return (int((h[0][0]*x+h[0][1]*y+h[0][2])/(h[2][0]*x+h[2][1]*y+h[2][2])),\
int((h[1][0]*x+h[1][1]*y+h[1][2])/(h[2][0]*x+h[2][1]*y+h[2][2])))

for x in range(fixedsize[0]):
for y in range(fixedsize[1]):
try:
fixedfile.putpixel((x,y),jpgfile.getpixel(pm2(pj,(x,y))))
except:
x

if preview:
fixedfile.show()
fixedfile.save(path+"fixed.jpg")
print("done fixing "+path)

#path = "fixed.jpg"
#jpgfile = Image.open(path)

#ofile = Image.new('RGB',oldsize)

#for x in range(oldsize[0]):
# for y in range(oldsize[1]):
# try:
# ofile.putpixel((x,y),jpgfile.getpixel(pm2(jp,(x,y))))
# except:
# x

#ofile.show()

def fixforeach(path = "img/test2/test2.mp4", index = 0, end = 95, preview = True):
while(index < end):
fix(path+"_frame"+str(index)+".jpg", preview = preview)
index+=1
print("done fix for each")
58 changes: 58 additions & 0 deletions conda/single_rotation_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 23 20:25:17 2018
@author: dipsy
"""

from PIL import Image
from math import cos, sin

path = "tilted_square.jpg"
jpgfile = Image.open(path)

fixedfile = Image.new('RGB',(24,24))

#jpgfile.show()
def dist(p1,p2):
return sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)

theta = 30

corners = [[38,28],[66,45],[49,73],[21,57]]
l = dist(corners[0],corners[1])

dy = 10000
dx=0

co = cos(theta*2*3.14/360)
si = sin(theta*2*3.14/360)

for c in corners:
if(c[1]<dy):
dy=c[1]
dx=c[0]


def pm2(p):
[x,y] = p
#print(c*x0)
x=x*l/24
y=y*l/24
x1 = co*x - si*y
y1 = si*x + co*y
x0 = (x1+dx)
y0 = y1+dy
return (int(x0), int(y0))

for x in range(100):
for y in range(100):
p=pm2((x,y))
try:
fixedfile.putpixel((x,y),jpgfile.getpixel(p))
except:
x
#print((x,y),p)

fixedfile.show()
fixedfile.save("tsquarefixed.jpg")
7 changes: 7 additions & 0 deletions conda/whole_rotation_fix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 5 23:06:21 2018
@author: dipsy
"""

0 comments on commit fe5fcd6

Please sign in to comment.