-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9331b0
commit 3297689
Showing
31 changed files
with
403 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
Oops, something went wrong.