Skip to content

Commit 3297689

Browse files
committed
a new branch of Olaybeauty
1 parent c9331b0 commit 3297689

31 files changed

+403
-18
lines changed

CVwithPy/InteractiveInovation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pylab import *
33

44
def InputPoints():
5-
im = array(Image.open('test.jpg'))
5+
im = array(Image.open('CVwithpy/test.jpg'))
66
imshow(im)
77
print('Please click 3 points')
88
x = ginput(3)

CVwithPy/Olaybeauty.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from PIL import Image
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
from scipy.ndimage import filters
5+
import os
6+
import cv2
7+
import cv2.cv as cv
8+
9+
def beauty(src,dst,center,radius,strength,m_strike):
10+
#area to calculate
11+
left = 0 if (center[0]-radius<0) else (center[0]-radius)
12+
right = (src.shape[1]-1) if (center[0]+radius>src.shape[1]) else (center[0]+radius)
13+
top = 0 if (center[1]-radius<0) else (center[1]-radius)
14+
bottom = (src.shape[0]-1) if (center[1]+radius>src.shape[0]) else (center[1]+radius)
15+
#radius
16+
powerRadius = radius*radius
17+
print "left,right,top,bottom",left,right,top,bottom
18+
#implement the scale
19+
i = left
20+
j = top
21+
cout = 0
22+
print i,j
23+
for i in range(left,right):
24+
offsetX = i - center[0]
25+
#if (i>=center[0] and i<=right):
26+
#print i
27+
#print "offsetX",offsetX
28+
for j in range(top,bottom):
29+
offsetY = j - center[1]
30+
XY = offsetX*offsetX+offsetY*offsetY
31+
if XY <= powerRadius:
32+
scaleFactor = 1.0 - float(XY)/float(powerRadius)
33+
#print "scaleFactor01",scaleFactor
34+
scaleFactor = 1 - float(strength)/100*scaleFactor
35+
posX = int(offsetX * scaleFactor) + center[0]
36+
posY = int(offsetY * scaleFactor) + center[1]
37+
if posX < 0 :
38+
posX = 0
39+
if posX > src.shape[1]:
40+
posX = src.shape[1]-1
41+
if posY < 0 :
42+
posY = 0
43+
if posY > src.shape[0]:
44+
posY = src.shape[0]-1
45+
#dst[j][i] = src[posY][posX]
46+
if i>center[1]:
47+
dst[j][i] = src[posY][posX]
48+
#dst[j][i] = src[posY][posX]
49+
else:
50+
dst[posY][posX] = src[j][i]
51+
print "cout",cout
52+
53+
def Mopi(src,dst,value1,value2,p):
54+
#
55+
dx = value1*5
56+
fc = value1*12.5
57+
temp1 = cv2.bilateralFilter(dst,dx, fc, fc)
58+
temp2 = (temp1-dst+128)
59+
temp3 = cv2.GaussianBlur(temp2,(2 * value2 - 1, 2 * value2 - 1),0,0)
60+
temp4 = dst + 2 * temp3 - 255;
61+
dst = (dst*(100 - p) + temp4*p) / 100;
62+
#dst.copyTo(image);
63+
64+
65+
66+
#im = np.array(Image.open('CVwithPy/test.jpg').convert('L'))
67+
im = cv2.imread("CVwithPy/testImage05.jpg")
68+
origin = cv2.imread("CVwithPy/testImage05.jpg")
69+
#im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY);
70+
#im2 = filters.gaussian_filter(im,5)
71+
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
72+
origin = cv2.cvtColor(origin,cv2.COLOR_BGR2RGB)
73+
src = im.copy()
74+
dst = im.copy()
75+
print "im",im.shape
76+
print "src",src.shape
77+
print "dst",dst.shape
78+
#center(x,y)
79+
#center1 = (340,480)
80+
center1 = (260,430)
81+
#center = (415,368)
82+
print "center",center1
83+
#beauty(src,dst,center,radius,strength,m_strike)
84+
beauty(src,dst,center1,80,-20,5)
85+
center2=(460,460)
86+
src = dst.copy()
87+
beauty(dst,src,center2,80,-20,5)
88+
#smooth image
89+
#src = dst.copy()
90+
kernel = np.ones((5,5),np.float32)/25
91+
dst = cv2.filter2D(src,-1,kernel)
92+
#Mopi(src,dst,value1,value2,p)
93+
Mopi(dst,dst,3,4,50)
94+
dst = cv2.bilateralFilter(dst,7,0, 70, 10)
95+
#src = cv2.cv.CreateImage(dst,dst,dst.channels())
96+
#cv2.cv.Smooth(src,dst,cv2.cv.CV_GAUSSIAN_5x5)
97+
#kernel_erode = np.ones((5,5),np.uint8)
98+
#dst = cv2.erode(dst,kernel,1)
99+
#Mopi(dst,dst,3,4,50)
100+
center2 = (400,240)
101+
plt.subplot(121)
102+
#origin[center1[1]+50][center1[0]-50] = (255,255,255)
103+
plt.imshow(origin)
104+
plt.subplot(122)
105+
plt.imshow(dst)
106+
plt.show()
107+
#image write into disk
108+
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
109+
dst = cv2.cvtColor(dst,cv2.COLOR_BGR2RGB)
110+
cv2.imwrite("./before.png", im, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
111+
cv2.imwrite("./after.png", dst, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])

CVwithPy/PCA.pyc

939 Bytes
Binary file not shown.

CVwithPy/pca_new_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pca_new import pca
55
from PCA import pca_book
66

7-
imX = np.array(Image.open('test.jpg').convert('L'))
7+
imX = np.array(Image.open('CVwithPy/test.jpg').convert('L'))
88
n,m = imX.shape[0:2]
99
points = []
1010
for i in range(n):

CVwithPy/testImage.jpg

128 KB
Loading

CVwithPy/testImage01.jpg

64.7 KB
Loading

CVwithPy/testImage02.jpg

53.6 KB
Loading

CVwithPy/testImage03.jpg

113 KB
Loading

CVwithPy/testImage04.jpg

104 KB
Loading

CVwithPy/testImage05.jpg

108 KB
Loading

FeatureTest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from SimpleCV import Image
2+
import cv2
3+
4+
img = Image("./OPENCV/test.jpg")
5+
nutBolts = img.binarize().findBlobs()
6+
nutBolts.image = img
7+
nutBolts.show()
8+
9+
"""
10+
while True:
11+
nutBolts.show()
12+
if cv2.waitKey(10) == 27:
13+
break
14+
"""

OPENCV/CalcOpticalFlow.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import cv2
2+
import numpy as np
3+
4+
"""draw the flow"""
5+
def draw_flow(im,flow,step=16):
6+
""""""
7+
h,w = im.shape[:2]
8+
y,x = np.mgrid[step/2:h:step,step/2:w:step].reshape(2,-1)
9+
fx,fy = flow[y,x].T
10+
11+
lines = np.vstack([x,y,x+fx,y+fy]).T.reshape(-1,2,2)
12+
lines = np.int32(lines)
13+
14+
vis = cv2.cvtColor(im,cv2.COLOR_GRAY2BGR)
15+
for (x1,y1),(x2,y2) in lines:
16+
cv2.line(vis,(x1,y1),(x2,y2),(0,255,0),1)
17+
cv2.circle(vis,(x1,y1),1,(0,255,0),-1)
18+
19+
return vis
20+
21+
22+
#the main video capture segment
23+
cap = cv2.VideoCapture(0)
24+
cap.set(CV_CAP_PROP_FRAME_WIDTH,640)
25+
cap.set(CV_CAP_PROP_FRAME_HEIGHT,360)
26+
ret,im = cap.read()
27+
prev_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
28+
29+
while True :
30+
ret,im = cap.read()
31+
cap.set(CV_CAP_PROP_FRAME_WIDTH,640)
32+
cap.set(CV_CAP_PROP_FRAME_HEIGHT,360)
33+
print ret
34+
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
35+
36+
#flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)
37+
flow = cv2.calcOpticalFlowFarneback(prev_gray,gray,0.5,3,15,3,5,1.2,0)
38+
prev_gray = gray
39+
40+
cv2.imshow('Optical Flow',draw_flow(gray,flow))
41+
if cv2.waitKey(10) == 27:
42+
break

OPENCV/Cartoonize.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,8 @@
2121
edge = cv2.Laplacian(median_blur,cv2.CV_8U,5)
2222
#threshold mask : return [retval,dst]
2323
mask = cv2.threshold(edge,8,255,cv2.THRESH_BINARY_INV)
24-
#mask = cv2.threshold(mask[1],8,255,cv2.THRESH_BINARY_INV)
25-
#mask = cv2.threshold(mask[1],8,255,cv2.THRESH_BINARY_INV)
26-
#print mask
27-
#blur = cv2.GaussianBlur(im,(0,0),0)
28-
29-
#colorize and cartoonify
30-
#smallSize = (im.shape[0],im.shape[1])
31-
#smallImg = cv2.Mat(smallSize,cv2.CV_8UC3)
32-
#smallImg = M(smallSize,cv2.CV_8UC3)
33-
34-
#resize the video : notice that the shape is [height,width]
35-
smallSize = im.shape[1]/2,im.shape[0]/2,im.shape[2]
36-
smallImg = cv2.resize(mask[1],smallSize[:2],cv2.INTER_LINEAR)
37-
cv2.imshow('video capture test',smallImg)
24+
#show the image
25+
cv2.imshow('video capture test',mask[1])
26+
#char keypress = cv2.waitKey(20)
3827
if cv2.waitKey(10) == 27:
39-
break
28+
break

OPENCV/SURF.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import cv2
2+
from PIL import Image
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
6+
im = cv2.imread("./OPENCV/bookexampleimg.png",1)
7+
plt_originim = Image.open("./OPENCV/bookexampleimg.png")
8+
#get the image width and height
9+
h,w = im.shape[:2]
10+
#down sampler
11+
im_lower = cv2.pyrDown(im)
12+
#gray image
13+
gray = cv2.cvtColor(im_lower,cv2.COLOR_RGB2GRAY)
14+
#detection the feature point
15+
#s = cv2.SURF(400)
16+
surf = cv2._getRawKeypoints()
17+
mask = np.uint8(np.ones(gray.shape))
18+
keypoints = surf.detect(gray,mask)
19+
#show the key points
20+
vis = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
21+
22+
for k in keypoints[::10]:
23+
cv2.Circle(vis,(int(k.pt[0]),int(k.pt[1]),2,(0,255,0),-1))
24+
cv2.Circle(vis,(int(k.pt[0]),int(k.pt[1]),int(k.size),(0,255,0),2))
25+
26+
"""使用pyplot显示对比图像"""
27+
28+
plt_im = cv2.cvtColor(vis,cv2.COLOR_BGR2RGB)
29+
plt_originim = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
30+
plt.subplot(121)
31+
plt.imshow(plt_im)
32+
plt.subplot(122)
33+
plt.imshow(plt_originim)
34+
plt.show()
35+
36+
37+
#write the result into a jpg image file
38+
cv2.imwrite("./OPENCV/floodfill result.jpg",vis)
39+
40+
#cv2 image show
41+
while True:
42+
cv2.imshow("the flood fill image",vis)
43+
if cv2.waitKey(10) == 27:
44+
break
45+
46+

OPENCV/VideoFrames.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import cv2
2+
import numpy as np
3+
4+
cap = cv2.VideoCapture(0)
5+
6+
frames = []
7+
while True:
8+
ret, im = cap.read()
9+
cv2.imshow('video capture',im)
10+
frames.append(im)
11+
if cv2.waitKey(10) == 27:
12+
break
13+
14+
frames = np.array(frames)
15+
print im.shape
16+
print frames.shape
17+

OPENCV/lktrack.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import cv2
2+
import numpy as np
3+
4+
#useful default parameters
5+
lk_params = dict(winSize=(15,15),maxLevel=2,criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,0.03))
6+
subpix_params = dict(zeroZone=(-1,-1),winSize=(10,10),criteria = (cv2.TERM_CRITERIA_COUNT | cv2.TERM_CRITERIA_EPS,20,0.03))
7+
feature_params = dict(maxCorners=500,qualityLevel=0.01,minDistance=10)
8+
9+
class LKTracker(object):
10+
def __init__(self, imnames):
11+
self.imnames = imnames
12+
self.features = []
13+
self.tracks = []
14+
self.current_frame = 0
15+
self.image = None
16+
self.prev_gray = self.gray = None
17+
18+
def detect_points(self):
19+
self.image = cv2.imread('./OPENCV/'+self.imnames[self.current_frame],1)
20+
self.gray = cv2.cvtColor(self.image,cv2.COLOR_BGR2GRAY)
21+
features = cv2.goodFeaturesToTrack(self.gray,**feature_params)
22+
cv2.cornerSubPix(self.gray,features,**subpix_params)
23+
self.features = features
24+
self.tracks = [ [p] for p in features.reshape((-1,2)) ]
25+
self.prev_gray = self.gray
26+
27+
def track_points(self):
28+
if self.features != []:
29+
self.step()
30+
self.image = cv2.imread('./OPENCV/'+self.imnames[self.current_frame],1)
31+
self.gray = cv2.cvtColor(self.image,cv2.COLOR_BGR2GRAY)
32+
tmp = np.float32(self.features).reshape(-1,1,2)
33+
features,status,tracker_error = cv2.calcOpticalFlowPyrLK(self.prev_gray,self.gray,tmp,None,**lk_params)
34+
self.features = [p for (st,p) in zip(status,features) if st]
35+
features = np.array(features).reshape((-1,2))
36+
for i,f in enumerate(features):
37+
self.tracks[i].append(f)
38+
ndx = [i for (i,st) in enumerate(status) if not st]
39+
ndx.reverse()
40+
for i in ndx:
41+
self.tracks.pop(i)
42+
43+
self.prev_gray = self.gray
44+
45+
def step(self,framenbr = None):
46+
if framenbr is None:
47+
self.current_frame = (self.current_frame + 1) % len(self.imnames)
48+
else:
49+
self.current_frame = framenbr % len(self.imnames)
50+
51+
def draw(self):
52+
for point in self.features:
53+
cv2.circle(self.image, (int(point[0][0]),int(point[0][1])) ,3, (0,255,0),1)
54+
cv2.imshow('LKtrack',self.image)
55+
cv2.waitKey()

OPENCV/lktrack.pyc

3.26 KB
Binary file not shown.

OPENCV/lktracktest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import lktrack
2+
import cv2
3+
4+
imnames = ['test01.jpg','test02.jpg','test03.jpg','test04.jpg']
5+
6+
im = cv2.imread('./OPENCV/test.jpg',1)
7+
for name in imnames:
8+
cv2.imwrite('./OPENCV/'+name,im)
9+
print name
10+
11+
lkt = lktrack.LKTracker(imnames)
12+
13+
lkt.detect_points()
14+
#lkt.draw()
15+
for i in range(len(imnames)-1):
16+
lkt.track_points()
17+
lkt.draw()
18+
cv2.imwrite('./OPENCV/traked'+lkt.imnames[i],lkt.image[i])

0 commit comments

Comments
 (0)