Skip to content

Commit

Permalink
a new branch of Olaybeauty
Browse files Browse the repository at this point in the history
  • Loading branch information
nodegetout committed Jun 17, 2017
1 parent c9331b0 commit 3297689
Show file tree
Hide file tree
Showing 31 changed files with 403 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CVwithPy/InteractiveInovation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pylab import *

def InputPoints():
im = array(Image.open('test.jpg'))
im = array(Image.open('CVwithpy/test.jpg'))
imshow(im)
print('Please click 3 points')
x = ginput(3)
Expand Down
111 changes: 111 additions & 0 deletions CVwithPy/Olaybeauty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import filters
import os
import cv2
import cv2.cv as cv

def beauty(src,dst,center,radius,strength,m_strike):
#area to calculate
left = 0 if (center[0]-radius<0) else (center[0]-radius)
right = (src.shape[1]-1) if (center[0]+radius>src.shape[1]) else (center[0]+radius)
top = 0 if (center[1]-radius<0) else (center[1]-radius)
bottom = (src.shape[0]-1) if (center[1]+radius>src.shape[0]) else (center[1]+radius)
#radius
powerRadius = radius*radius
print "left,right,top,bottom",left,right,top,bottom
#implement the scale
i = left
j = top
cout = 0
print i,j
for i in range(left,right):
offsetX = i - center[0]
#if (i>=center[0] and i<=right):
#print i
#print "offsetX",offsetX
for j in range(top,bottom):
offsetY = j - center[1]
XY = offsetX*offsetX+offsetY*offsetY
if XY <= powerRadius:
scaleFactor = 1.0 - float(XY)/float(powerRadius)
#print "scaleFactor01",scaleFactor
scaleFactor = 1 - float(strength)/100*scaleFactor
posX = int(offsetX * scaleFactor) + center[0]
posY = int(offsetY * scaleFactor) + center[1]
if posX < 0 :
posX = 0
if posX > src.shape[1]:
posX = src.shape[1]-1
if posY < 0 :
posY = 0
if posY > src.shape[0]:
posY = src.shape[0]-1
#dst[j][i] = src[posY][posX]
if i>center[1]:
dst[j][i] = src[posY][posX]
#dst[j][i] = src[posY][posX]
else:
dst[posY][posX] = src[j][i]
print "cout",cout

def Mopi(src,dst,value1,value2,p):
#
dx = value1*5
fc = value1*12.5
temp1 = cv2.bilateralFilter(dst,dx, fc, fc)
temp2 = (temp1-dst+128)
temp3 = cv2.GaussianBlur(temp2,(2 * value2 - 1, 2 * value2 - 1),0,0)
temp4 = dst + 2 * temp3 - 255;
dst = (dst*(100 - p) + temp4*p) / 100;
#dst.copyTo(image);



#im = np.array(Image.open('CVwithPy/test.jpg').convert('L'))
im = cv2.imread("CVwithPy/testImage05.jpg")
origin = cv2.imread("CVwithPy/testImage05.jpg")
#im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY);
#im2 = filters.gaussian_filter(im,5)
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
origin = cv2.cvtColor(origin,cv2.COLOR_BGR2RGB)
src = im.copy()
dst = im.copy()
print "im",im.shape
print "src",src.shape
print "dst",dst.shape
#center(x,y)
#center1 = (340,480)
center1 = (260,430)
#center = (415,368)
print "center",center1
#beauty(src,dst,center,radius,strength,m_strike)
beauty(src,dst,center1,80,-20,5)
center2=(460,460)
src = dst.copy()
beauty(dst,src,center2,80,-20,5)
#smooth image
#src = dst.copy()
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(src,-1,kernel)
#Mopi(src,dst,value1,value2,p)
Mopi(dst,dst,3,4,50)
dst = cv2.bilateralFilter(dst,7,0, 70, 10)
#src = cv2.cv.CreateImage(dst,dst,dst.channels())
#cv2.cv.Smooth(src,dst,cv2.cv.CV_GAUSSIAN_5x5)
#kernel_erode = np.ones((5,5),np.uint8)
#dst = cv2.erode(dst,kernel,1)
#Mopi(dst,dst,3,4,50)
center2 = (400,240)
plt.subplot(121)
#origin[center1[1]+50][center1[0]-50] = (255,255,255)
plt.imshow(origin)
plt.subplot(122)
plt.imshow(dst)
plt.show()
#image write into disk
im = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
dst = cv2.cvtColor(dst,cv2.COLOR_BGR2RGB)
cv2.imwrite("./before.png", im, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
cv2.imwrite("./after.png", dst, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
Binary file added CVwithPy/PCA.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion CVwithPy/pca_new_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pca_new import pca
from PCA import pca_book

imX = np.array(Image.open('test.jpg').convert('L'))
imX = np.array(Image.open('CVwithPy/test.jpg').convert('L'))
n,m = imX.shape[0:2]
points = []
for i in range(n):
Expand Down
Binary file added CVwithPy/testImage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CVwithPy/testImage01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CVwithPy/testImage02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CVwithPy/testImage03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CVwithPy/testImage04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CVwithPy/testImage05.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions FeatureTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from SimpleCV import Image
import cv2

img = Image("./OPENCV/test.jpg")
nutBolts = img.binarize().findBlobs()
nutBolts.image = img
nutBolts.show()

"""
while True:
nutBolts.show()
if cv2.waitKey(10) == 27:
break
"""
42 changes: 42 additions & 0 deletions OPENCV/CalcOpticalFlow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cv2
import numpy as np

"""draw the flow"""
def draw_flow(im,flow,step=16):
""""""
h,w = im.shape[:2]
y,x = np.mgrid[step/2:h:step,step/2:w:step].reshape(2,-1)
fx,fy = flow[y,x].T

lines = np.vstack([x,y,x+fx,y+fy]).T.reshape(-1,2,2)
lines = np.int32(lines)

vis = cv2.cvtColor(im,cv2.COLOR_GRAY2BGR)
for (x1,y1),(x2,y2) in lines:
cv2.line(vis,(x1,y1),(x2,y2),(0,255,0),1)
cv2.circle(vis,(x1,y1),1,(0,255,0),-1)

return vis


#the main video capture segment
cap = cv2.VideoCapture(0)
cap.set(CV_CAP_PROP_FRAME_WIDTH,640)
cap.set(CV_CAP_PROP_FRAME_HEIGHT,360)
ret,im = cap.read()
prev_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

while True :
ret,im = cap.read()
cap.set(CV_CAP_PROP_FRAME_WIDTH,640)
cap.set(CV_CAP_PROP_FRAME_HEIGHT,360)
print ret
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

#flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)
flow = cv2.calcOpticalFlowFarneback(prev_gray,gray,0.5,3,15,3,5,1.2,0)
prev_gray = gray

cv2.imshow('Optical Flow',draw_flow(gray,flow))
if cv2.waitKey(10) == 27:
break
19 changes: 4 additions & 15 deletions OPENCV/Cartoonize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@
edge = cv2.Laplacian(median_blur,cv2.CV_8U,5)
#threshold mask : return [retval,dst]
mask = cv2.threshold(edge,8,255,cv2.THRESH_BINARY_INV)
#mask = cv2.threshold(mask[1],8,255,cv2.THRESH_BINARY_INV)
#mask = cv2.threshold(mask[1],8,255,cv2.THRESH_BINARY_INV)
#print mask
#blur = cv2.GaussianBlur(im,(0,0),0)

#colorize and cartoonify
#smallSize = (im.shape[0],im.shape[1])
#smallImg = cv2.Mat(smallSize,cv2.CV_8UC3)
#smallImg = M(smallSize,cv2.CV_8UC3)

#resize the video : notice that the shape is [height,width]
smallSize = im.shape[1]/2,im.shape[0]/2,im.shape[2]
smallImg = cv2.resize(mask[1],smallSize[:2],cv2.INTER_LINEAR)
cv2.imshow('video capture test',smallImg)
#show the image
cv2.imshow('video capture test',mask[1])
#char keypress = cv2.waitKey(20)
if cv2.waitKey(10) == 27:
break
break
46 changes: 46 additions & 0 deletions OPENCV/SURF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import cv2
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

im = cv2.imread("./OPENCV/bookexampleimg.png",1)
plt_originim = Image.open("./OPENCV/bookexampleimg.png")
#get the image width and height
h,w = im.shape[:2]
#down sampler
im_lower = cv2.pyrDown(im)
#gray image
gray = cv2.cvtColor(im_lower,cv2.COLOR_RGB2GRAY)
#detection the feature point
#s = cv2.SURF(400)
surf = cv2._getRawKeypoints()
mask = np.uint8(np.ones(gray.shape))
keypoints = surf.detect(gray,mask)
#show the key points
vis = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)

for k in keypoints[::10]:
cv2.Circle(vis,(int(k.pt[0]),int(k.pt[1]),2,(0,255,0),-1))
cv2.Circle(vis,(int(k.pt[0]),int(k.pt[1]),int(k.size),(0,255,0),2))

"""使用pyplot显示对比图像"""

plt_im = cv2.cvtColor(vis,cv2.COLOR_BGR2RGB)
plt_originim = cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
plt.subplot(121)
plt.imshow(plt_im)
plt.subplot(122)
plt.imshow(plt_originim)
plt.show()


#write the result into a jpg image file
cv2.imwrite("./OPENCV/floodfill result.jpg",vis)

#cv2 image show
while True:
cv2.imshow("the flood fill image",vis)
if cv2.waitKey(10) == 27:
break


17 changes: 17 additions & 0 deletions OPENCV/VideoFrames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

frames = []
while True:
ret, im = cap.read()
cv2.imshow('video capture',im)
frames.append(im)
if cv2.waitKey(10) == 27:
break

frames = np.array(frames)
print im.shape
print frames.shape

55 changes: 55 additions & 0 deletions OPENCV/lktrack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import cv2
import numpy as np

#useful default parameters
lk_params = dict(winSize=(15,15),maxLevel=2,criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,0.03))
subpix_params = dict(zeroZone=(-1,-1),winSize=(10,10),criteria = (cv2.TERM_CRITERIA_COUNT | cv2.TERM_CRITERIA_EPS,20,0.03))
feature_params = dict(maxCorners=500,qualityLevel=0.01,minDistance=10)

class LKTracker(object):
def __init__(self, imnames):
self.imnames = imnames
self.features = []
self.tracks = []
self.current_frame = 0
self.image = None
self.prev_gray = self.gray = None

def detect_points(self):
self.image = cv2.imread('./OPENCV/'+self.imnames[self.current_frame],1)
self.gray = cv2.cvtColor(self.image,cv2.COLOR_BGR2GRAY)
features = cv2.goodFeaturesToTrack(self.gray,**feature_params)
cv2.cornerSubPix(self.gray,features,**subpix_params)
self.features = features
self.tracks = [ [p] for p in features.reshape((-1,2)) ]
self.prev_gray = self.gray

def track_points(self):
if self.features != []:
self.step()
self.image = cv2.imread('./OPENCV/'+self.imnames[self.current_frame],1)
self.gray = cv2.cvtColor(self.image,cv2.COLOR_BGR2GRAY)
tmp = np.float32(self.features).reshape(-1,1,2)
features,status,tracker_error = cv2.calcOpticalFlowPyrLK(self.prev_gray,self.gray,tmp,None,**lk_params)
self.features = [p for (st,p) in zip(status,features) if st]
features = np.array(features).reshape((-1,2))
for i,f in enumerate(features):
self.tracks[i].append(f)
ndx = [i for (i,st) in enumerate(status) if not st]
ndx.reverse()
for i in ndx:
self.tracks.pop(i)

self.prev_gray = self.gray

def step(self,framenbr = None):
if framenbr is None:
self.current_frame = (self.current_frame + 1) % len(self.imnames)
else:
self.current_frame = framenbr % len(self.imnames)

def draw(self):
for point in self.features:
cv2.circle(self.image, (int(point[0][0]),int(point[0][1])) ,3, (0,255,0),1)
cv2.imshow('LKtrack',self.image)
cv2.waitKey()
Binary file added OPENCV/lktrack.pyc
Binary file not shown.
18 changes: 18 additions & 0 deletions OPENCV/lktracktest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lktrack
import cv2

imnames = ['test01.jpg','test02.jpg','test03.jpg','test04.jpg']

im = cv2.imread('./OPENCV/test.jpg',1)
for name in imnames:
cv2.imwrite('./OPENCV/'+name,im)
print name

lkt = lktrack.LKTracker(imnames)

lkt.detect_points()
#lkt.draw()
for i in range(len(imnames)-1):
lkt.track_points()
lkt.draw()
cv2.imwrite('./OPENCV/traked'+lkt.imnames[i],lkt.image[i])
Loading

0 comments on commit 3297689

Please sign in to comment.