Skip to content

Commit bdd6f18

Browse files
author
antonio
committed
extractor and display
1 parent 18e9573 commit bdd6f18

File tree

8 files changed

+518
-0
lines changed

8 files changed

+518
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Pipfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
numpy = "*"
8+
opencv-python = "*"
9+
pygame = "*"
10+
matplotlib = "*"
11+
scikit-image = "*"
12+
13+
[dev-packages]
14+
15+
[requires]
16+
python_version = "3.9"

Pipfile.lock

+380
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

corner_detector.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
import cv2 as cv
3+
from matplotlib import pyplot as plt
4+
5+
img = cv.imread('cubes.png')
6+
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
7+
8+
corners = cv.goodFeaturesToTrack(gray,300,0.01,3)
9+
corners = np.int0(corners)
10+
11+
for i in corners:
12+
x,y = i.ravel()
13+
cv.circle(img,(x,y),3,255,-1)
14+
15+
plt.imshow(img),plt.show()

cubes.png

222 KB
Loading

display.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pygame
2+
from pygame.locals import DOUBLEBUF
3+
4+
class Display(object):
5+
def __init__(self, W, H):
6+
pygame.init()
7+
self.screen = pygame.display.set_mode((W, H), DOUBLEBUF)
8+
self.surface = pygame.Surface(self.screen.get_size()).convert()
9+
10+
def paint(self, img):
11+
# junk
12+
for event in pygame.event.get():
13+
pass
14+
15+
# draw
16+
#pygame.surfarray.blit_array(self.surface, img.swapaxes(0,1)[:, :, [2,1,0]])
17+
18+
# RGB, not BGR (might have to switch in twitchslam)
19+
pygame.surfarray.blit_array(self.surface, img.swapaxes(0,1)[:, :, [0,1,2]])
20+
self.screen.blit(self.surface, (0,0))
21+
22+
# blit
23+
pygame.display.flip()

extractor.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import cv2
2+
import numpy as np
3+
from skimage.measure import ransac
4+
from skimage.transform import FundamentalMatrixTransform
5+
6+
class Extractor(object):
7+
def __init__(self):
8+
self.orb = cv2.ORB_create()
9+
self.bf = cv2.BFMatcher.create(cv2.NORM_HAMMING)
10+
self.last = None
11+
12+
def extract(self, img):
13+
# preprocessing
14+
# temporaly disabled because of np.float32() required for the followirng steps
15+
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
16+
# detection
17+
feats = cv2.goodFeaturesToTrack(np.mean(img, axis=2).astype(np.uint8), maxCorners=300, qualityLevel=0.01, minDistance=3)
18+
19+
# extraction
20+
kps = [cv2.KeyPoint(x=f[0][0], y=f[0][1], _size=20) for f in feats]
21+
kps, des = self.orb.compute(img, kps)
22+
23+
# matching
24+
ret = []
25+
if self.last is not None:
26+
matches = self.bf.knnMatch(des, self.last['des'], k=2)
27+
for m,n in matches:
28+
if m.distance < 0.75*n.distance:
29+
kp1 = kps[m.queryIdx].pt
30+
kp2 = self.last['kps'][m.trainIdx].pt
31+
ret.append((kp1, kp2))
32+
33+
# filter
34+
if len(ret) > 0:
35+
ret = np.array(ret)
36+
37+
model, inliers = ransac((ret[:, 0], ret[:, 1]),
38+
FundamentalMatrixTransform,
39+
min_samples=8,
40+
residual_threshold=1,
41+
max_trials=100)
42+
ret = ret[inliers]
43+
44+
# return
45+
self.last = {'kps': kps, 'des': des}
46+
return ret

slam.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cv2
2+
import pygame
3+
from display import Display
4+
from extractor import Extractor
5+
import numpy as np
6+
7+
#cv2.namedWindow('image', cv2.WINDOW_NORMAL)
8+
W = 1920//2
9+
H = 1080//2
10+
11+
disp = Display(W, H)
12+
fe = Extractor()
13+
14+
def process_frame(img):
15+
img = cv2.resize(img, (W, H))
16+
matches = fe.extract(img)
17+
18+
print("%d matches" % len(matches))
19+
20+
for pt1, pt2 in matches:
21+
u1,v1 = map(lambda x: int(round(x)), pt1 )
22+
u2,v2 = map(lambda x: int(round(x)), pt2 )
23+
24+
cv2.circle(img, (u1,v1), color=(0,255,0), radius=3)
25+
cv2.line(img, (u1,v1), (u2,v2), color=(255,0,0))
26+
27+
disp.paint(img)
28+
29+
30+
if __name__ == "__main__":
31+
cap = cv2.VideoCapture("test.mp4")
32+
33+
while cap.isOpened():
34+
ret, frame = cap.read()
35+
if ret == True:
36+
process_frame(frame)
37+
else:
38+
break

0 commit comments

Comments
 (0)