-
Notifications
You must be signed in to change notification settings - Fork 53
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
0 parents
commit dd3564e
Showing
12 changed files
with
174 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
venv | ||
.vscode | ||
__pycache__ | ||
.DS_Store |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Cyril Diagne | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,28 @@ | ||
# ScreenPoint | ||
|
||
Finds the (x,y) coordinates of the centroid of an image (eg: a mobile phone camera image) pointing at another image (eg: a computer screen) using [OpenCV SIFT](https://docs.opencv.org/2.4/modules/nonfree/doc/feature_detection.html). | ||
|
||
![debug view](example/match_debug.png) | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install screenpoint | ||
``` | ||
|
||
## Usage | ||
|
||
```python | ||
import screenpoint | ||
import cv2 | ||
|
||
# Load input images. | ||
screen = cv2.imread('screen.png', 0) | ||
view = cv2.imread('view.jpg', 0) | ||
|
||
# Project view centroid to screen space. | ||
# x and y are the coordinate of the `view` centroid in `screen` space. | ||
x, y = screenpoint.project(view, screen) | ||
``` | ||
|
||
See [example.py](example.py) for more information. |
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,12 @@ | ||
import screenpoint | ||
import cv2 | ||
|
||
# Load input images. | ||
screen = cv2.imread('example/screen.png', 0) | ||
view = cv2.imread('example/view.jpg', 0) | ||
|
||
# Project centroid. | ||
x, y, img_debug = screenpoint.project(view, screen, True) | ||
|
||
# Write debug image. | ||
cv2.imwrite('example/match_debug.png', img_debug) |
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 @@ | ||
opencv-contrib-python==3.4.2.17 |
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,3 @@ | ||
from . import screenpoint | ||
|
||
project = screenpoint.project |
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,75 @@ | ||
import numpy as np | ||
import cv2 | ||
import logging | ||
|
||
MIN_MATCH_COUNT = 6 | ||
FLANN_INDEX_KDTREE = 0 | ||
|
||
sift = cv2.xfeatures2d.SIFT_create() | ||
|
||
|
||
def project(view, screen, debug): | ||
kp_screen, des_screen = sift.detectAndCompute(screen, None) | ||
kp_view, des_view = sift.detectAndCompute(view, None) | ||
|
||
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) | ||
search_params = dict(checks=50) | ||
flann = cv2.FlannBasedMatcher(index_params, search_params) | ||
matches = flann.knnMatch(des_screen, des_view, k=2) | ||
|
||
# Store all good matches as per Lowe's ration test | ||
good = [] | ||
for m, n in matches: | ||
if m.distance < 0.7 * n.distance: | ||
good.append(m) | ||
|
||
if len(good) < MIN_MATCH_COUNT: | ||
logging.debug("ScreenPoint: Not enough matches.") | ||
return -1, -1 | ||
|
||
screen_pts = np.float32([kp_screen[m.queryIdx].pt | ||
for m in good]).reshape(-1, 1, 2) | ||
view_pts = np.float32([kp_view[m.trainIdx].pt | ||
for m in good]).reshape(-1, 1, 2) | ||
|
||
h, w = view.shape | ||
M, mask = cv2.findHomography(view_pts, screen_pts, cv2.RANSAC, 5.0) | ||
|
||
pts = np.float32([[(w - 1) * 0.5, (h - 1) * 0.5]]).reshape(-1, 1, 2) | ||
dst = cv2.perspectiveTransform(pts, M) | ||
x, y = np.int32(dst[0][0]) | ||
|
||
if debug: | ||
img_debug = draw_debug_(x, y, view, screen, M, mask, kp_screen, | ||
kp_view, good) | ||
return x, y, img_debug | ||
else: | ||
return x, y | ||
|
||
|
||
def draw_debug_(x, y, view, screen, M, mask, kp_screen, kp_view, good): | ||
|
||
matchesMask = mask.ravel().tolist() | ||
draw_params = dict( | ||
matchColor=(0, 255, 0), # draw matches in green color | ||
singlePointColor=None, | ||
matchesMask=matchesMask, # draw only inliers | ||
flags=2) | ||
img_debug = cv2.drawMatches(screen, kp_screen, view, kp_view, good, None, | ||
**draw_params) | ||
|
||
# Get view centroid coordinates in img_debug space. | ||
cx = int(view.shape[1] * 0.5 + screen.shape[1]) | ||
cy = int(view.shape[0] * 0.5) | ||
# Draw view outline. | ||
cv2.rectangle(img_debug, (screen.shape[1], 0), | ||
(img_debug.shape[1] - 2, img_debug.shape[0] - 2), | ||
(0, 0, 255), 2) | ||
# draw connecting line. | ||
cv2.polylines(img_debug, [np.int32([(x, y), (cx, cy)])], True, | ||
(100, 100, 255), 1, cv2.LINE_AA) | ||
# Draw query/match markers. | ||
cv2.drawMarker(img_debug, (cx, cy), (0, 0, 255), cv2.MARKER_CROSS, 30, 2) | ||
cv2.circle(img_debug, (x, y), 10, (0, 0, 255), -1) | ||
|
||
return img_debug |
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,2 @@ | ||
[metadata] | ||
description-file = README.md |
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,28 @@ | ||
from distutils.core import setup | ||
|
||
setup( | ||
name='screenpoint', | ||
packages=['screenpoint'], | ||
version='0.1.0', | ||
license='MIT', | ||
description='Project an image centroid to another image using OpenCV', | ||
author='Cyril Diagne', | ||
author_email='diagne.cyril@gmail.com', | ||
url='https://github.com/cyrildiagne/screenpoint', | ||
download_url= | ||
'https://github.com/cyrildiagne/screenpoint/archive/v0.1.0.tar.gz', | ||
keywords=['opencv', 'sift', 'projection'], | ||
install_requires=[ | ||
'opencv-contrib-python==3.4.2.17', | ||
], | ||
classifiers=[ | ||
'Development Status :: 3 - Alpha', | ||
'Intended Audience :: Developers', | ||
'Topic :: Software Development :: Build Tools', | ||
'License :: OSI Approved :: MIT License', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.4', | ||
'Programming Language :: Python :: 3.5', | ||
'Programming Language :: Python :: 3.6', | ||
], | ||
) |