Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
add sift and hough
  • Loading branch information
TakashiIjiri committed Apr 9, 2017
1 parent ac668c4 commit cb26325
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 0 deletions.
76 changes: 76 additions & 0 deletions SIFT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# coding: utf-8

import cv2
import scipy as sp
import numpy as np
import random




def visKeyPoints(img1, points1) :
tmp = np.zeros(img1.shape)
tmp = img1
vis = cv2.merge((tmp, tmp, tmp))

for p in points1:
if( p.size < 5 ) : continue
#temp = (p.pt, p.size, p.angle, p.response, p.octave, p.class_id)
color = (random.uniform(1,255), random.uniform(1,255), random.uniform(1,255))
cv2.circle(vis, (int(p.pt[0]),int(p.pt[1])), int(p.size), color, 2)
return vis



def visMatching(img1, img2, points1, points2) :
H1, W1 = img1.shape[:2]
H2, W2 = img2.shape[:2]

print(img1.shape)
print(img2.shape)

gray = sp.zeros( (max(H1, H2), (W1 + W2 + 30)), sp.uint8)
gray[ :H1, :W1] = img1
gray[ :H2, W1+ 30:] = img2
visImg = cv2.merge((gray, gray, gray))

for i in range(len(points1)):
if( i % 2 != 0) : continue
color = (random.uniform(1,255), random.uniform(1,255), random.uniform(1,255))
p1 = ( int(points1[i][0] ), int(points1[i][1] ) )
p2 = ( int(points2[i][0] + W1+30), int(points2[i][1] ) )
cv2.line(visImg, p1, p2, color)

return visImg





img1 = cv2.imread("imgs/book1.jpg", 0)
img2 = cv2.imread("imgs/book3.jpg", 0)

#compute SIFT
sift = cv2.xfeatures2d.SIFT_create()
key1, des1 = sift.detectAndCompute (img1, None )
key2, des2 = sift.detectAndCompute (img2, None )

#matching and triming
matches = cv2.BFMatcher( cv2.NORM_L2 ).match(des1, des2)
threshold = np.array([m.distance for m in matches]).mean() * 1.2
matches_trim = [m for m in matches if m.distance < threshold]


point1 = np.array( [ np.array( key1[m.queryIdx].pt) for m in matches_trim ])
point2 = np.array( [ np.array( key2[m.trainIdx].pt) for m in matches_trim ])
imgMatch= visMatching(img1, img2, point1, point2)

imgVis1 = visKeyPoints( img1, key1 )
imgVis2 = visKeyPoints( img2, key2 )


# wait key input
cv2.imshow("key points1",imgVis1 )
cv2.imshow("key points2",imgVis2 )
cv2.imshow("key match" ,imgMatch )
cv2.waitKey(0)
41 changes: 41 additions & 0 deletions hough.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# coding: utf-8

import cv2
import numpy as np



img_orig = cv2.imread ("imgs/book1.jpg")
img_gray = cv2.cvtColor(img_orig, cv2.COLOR_BGR2GRAY)
img_canny = cv2.Canny (img_gray, 50,150,apertureSize = 3)
img_vis = cv2.merge ((img_gray,img_gray,img_gray))


ACCURACY_RHO = 1
ACCURACY_THETA = np.pi/180.0
THRESH_VOTE = 150

hough_lines = cv2.HoughLines(img_canny, ACCURACY_RHO, ACCURACY_THETA, THRESH_VOTE)

for line in hough_lines :
for r,t in line :
#r = x cos t + y sin t
a = np.cos(t)
b = np.sin(t)
x0 = a*r
y0 = b*r
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*( a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*( a))

cv2.line(img_vis, (x1,y1), (x2,y2), (0,0,255),1)




cv2.imshow("c", img_canny)
cv2.imshow("a", img_vis )


cv2.waitKey(0)
Binary file added houghlines3.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 imgs/book1.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 imgs/book2.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 imgs/book3.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 imgs/tea1.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 imgs/tea2.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 imgs/tea3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cb26325

Please sign in to comment.