Skip to content

Commit

Permalink
move subclasses of stitcher back to stitcher module.
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmirgut committed Apr 10, 2017
1 parent 3109a89 commit 75b1978
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 158 deletions.
157 changes: 0 additions & 157 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
import os

import cv2
import numpy as np
import pytest
from numpy import testing as npt

import bb_stitcher.picking.picker
import bb_stitcher.helpers as helpers
import bb_stitcher.stitcher as stitcher


def draw_marks(img, pts, color=(0, 0, 255), marker_types=cv2.MARKER_CROSS):
img_m = np.copy(img)
if len(img_m.shape) == 2:
img_m = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
pts = pts.astype(int)
for pt in pts:
cv2.drawMarker(img_m, tuple(pt), color, markerType=marker_types,
markerSize=40, thickness=5)
return img_m


@pytest.fixture
Expand All @@ -27,142 +9,3 @@ def outdir(main_outdir):
if not os.path.exists(out_path):
os.makedirs(out_path)
return out_path


@pytest.fixture
def fb_stitcher(config):
fbs = stitcher.FeatureBasedStitcher(config)
return fbs


def test_get_default_config():
config = helpers.get_default_config()

config_set = {
'Rectificator',
'FeatureBasedStitcher',
'SURF',
'FeatureMatcher'}
assert set(config.sections()) == config_set


def test_get_default_debug_config():
deb_config = helpers.get_default_debug_config()
assert 'loggers' in deb_config


"""
Test for feature based Stitcher.
"""


def test_calc_feature_mask():
target_mask_left = np.array(
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 255, 255],
[0, 255, 255],
[0, 255, 255],
[0, 255, 255],
[0, 0, 0],
], np.uint8)
target_mask_right = np.array(
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
[0, 0, 0],
], np.uint8)
mask_left, mask_right = stitcher.FeatureBasedStitcher._calc_feature_mask(
(3, 8), (3, 8), 2, 3, 1)
npt.assert_equal(mask_left, target_mask_left)
npt.assert_equal(mask_right, target_mask_right)


def test_fb_estimate_transform(fb_stitcher, left_img, right_img, not_to_bee):
# provoke no finding of transformation
fb_stitcher.estimate_transform(left_img['img'], not_to_bee)
assert fb_stitcher.homo_left is None and fb_stitcher.homo_right is None

# find transformation
fb_stitcher.estimate_transform(left_img['img'], right_img['img'], 90, -90)
assert fb_stitcher.homo_left is not None and fb_stitcher.homo_right is not None


@pytest.mark.slow
def test_overall_fb_stitching(fb_stitcher, left_img, right_img, outdir):
fb_stitcher.estimate_transform(left_img['img'], right_img['img'], 90, -90)
assert fb_stitcher.homo_left is not None and fb_stitcher.homo_right is not None
pano = fb_stitcher.compose_panorama(
left_img['img_w_detections'], right_img['img_w_detections'])
detections_left_mapped = fb_stitcher.map_left_points(left_img['detections'])
detections_right_mapped = fb_stitcher.map_right_points(right_img['detections'])
pano = draw_marks(pano, detections_left_mapped)
pano = draw_marks(pano, detections_right_mapped)

out = os.path.join(outdir, 'panorama_fb_w_detections.jpg')
cv2.imwrite(out, pano)


"""
Test for rectangle Stitcher.
"""


def test_rect_stitcher_estimate_transform(left_img, right_img, outdir, config,
monkeypatch):
def mockreturn(myself, image_list, all):
left_points = np.array([
[88.91666412, 3632.6015625],
[2760.26855469, 3636.70849609],
[2726.26708984, 363.4861145],
[93.88884735, 371.98330688]], dtype=np.float32)
right_points = np.array([
[181.49372864, 3687.71582031],
[2903.44042969, 3723.99926758],
[2921.27368164, 458.77352905],
[255.66642761, 431.24780273]], dtype=np.float32)
return left_points, right_points
monkeypatch.setattr(bb_stitcher.picking.picker.PointPicker, 'pick', mockreturn)
# print(left_points)
rt_stitcher = stitcher.RectangleStitcher(config)
rt_stitcher.estimate_transform(
left_img['img'], right_img['img'], 90, -90)
assert rt_stitcher.homo_left is not None
assert rt_stitcher.homo_right is not None
assert rt_stitcher.pano_size is not None


@pytest.mark.slow
def test_overall_rt_stitching(left_img, right_img, outdir, config, monkeypatch):
def mockreturn(myself, image_list, all):
left_points = np.array([
[88.91666412, 3632.6015625],
[2760.26855469, 3636.70849609],
[2726.26708984, 363.4861145],
[93.88884735, 371.98330688]], dtype=np.float32)
right_points = np.array([
[181.49372864, 3687.71582031],
[2903.44042969, 3723.99926758],
[2921.27368164, 458.77352905],
[255.66642761, 431.24780273]], dtype=np.float32)
return left_points, right_points
monkeypatch.setattr(bb_stitcher.picking.picker.PointPicker, 'pick', mockreturn)
rt_stitcher = stitcher.RectangleStitcher(config)
rt_stitcher.estimate_transform(left_img['img'], right_img['img'], 90, -90)
assert rt_stitcher.homo_left is not None
assert rt_stitcher.homo_right is not None
assert rt_stitcher.pano_size is not None
pano = rt_stitcher.compose_panorama(
left_img['img_w_detections'], right_img['img_w_detections'])
detections_left_mapped = rt_stitcher.map_left_points(left_img['detections'])
detections_right_mapped = rt_stitcher.map_right_points(right_img['detections'])
pano = draw_marks(pano, detections_left_mapped)
pano = draw_marks(pano, detections_right_mapped)

out = os.path.join(outdir, 'panorama_rt_w_detections.jpg')
cv2.imwrite(out, pano)
16 changes: 16 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,19 @@ def test_get_ratio_px_to_mm():
distance_mm = 25
px_to_mm = helpers.get_ratio_px_to_mm(start_point, end_point, distance_mm)
assert px_to_mm == 0.5


def test_get_default_config():
config = helpers.get_default_config()

config_set = {
'Rectificator',
'FeatureBasedStitcher',
'SURF',
'FeatureMatcher'}
assert set(config.sections()) == config_set


def test_get_default_debug_config():
deb_config = helpers.get_default_debug_config()
assert 'loggers' in deb_config
109 changes: 108 additions & 1 deletion tests/test_stitcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import cv2
import numpy as np
import numpy.testing as npt
import pytest
import numpy.testing as npt

import bb_stitcher.helpers as helpers
import bb_stitcher.picking.picker
Expand Down Expand Up @@ -199,3 +199,110 @@ def mockreturn(myself, image_list, all):
monkeypatch.setattr(bb_stitcher.picking.picker.PointPicker, 'pick', mockreturn)
monkeypatch.setitem(__builtins__, 'input', lambda x: "348")
super_stitcher._calc_image_to_world_mat(panorama)


def test_calc_feature_mask():
target_mask_left = np.array(
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 255, 255],
[0, 255, 255],
[0, 255, 255],
[0, 255, 255],
[0, 0, 0],
], np.uint8)
target_mask_right = np.array(
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
[255, 255, 0],
[0, 0, 0],
], np.uint8)
mask_left, mask_right = stitcher.FeatureBasedStitcher._calc_feature_mask(
(3, 8), (3, 8), 2, 3, 1)
npt.assert_equal(mask_left, target_mask_left)
npt.assert_equal(mask_right, target_mask_right)


def test_fb_estimate_transform(fb_stitcher, left_img, right_img, not_to_bee):
# provoke no finding of transformation
fb_stitcher.estimate_transform(left_img['img'], not_to_bee)
assert fb_stitcher.homo_left is None and fb_stitcher.homo_right is None

# find transformation
fb_stitcher.estimate_transform(left_img['img'], right_img['img'], 90, -90)
assert fb_stitcher.homo_left is not None and fb_stitcher.homo_right is not None


@pytest.mark.slow
def test_overall_fb_stitching(fb_stitcher, left_img, right_img, outdir):
fb_stitcher.estimate_transform(left_img['img'], right_img['img'], 90, -90)
assert fb_stitcher.homo_left is not None and fb_stitcher.homo_right is not None
pano = fb_stitcher.compose_panorama(
left_img['img_w_detections'], right_img['img_w_detections'])
detections_left_mapped = fb_stitcher.map_left_points(left_img['detections'])
detections_right_mapped = fb_stitcher.map_right_points(right_img['detections'])
pano = draw_marks(pano, detections_left_mapped)
pano = draw_marks(pano, detections_right_mapped)

out = os.path.join(outdir, 'panorama_fb_w_detections.jpg')
cv2.imwrite(out, pano)


def test_rect_stitcher_estimate_transform(left_img, right_img, outdir, config,
monkeypatch):
def mockreturn(myself, image_list, all):
left_points = np.array([
[88.91666412, 3632.6015625],
[2760.26855469, 3636.70849609],
[2726.26708984, 363.4861145],
[93.88884735, 371.98330688]], dtype=np.float32)
right_points = np.array([
[181.49372864, 3687.71582031],
[2903.44042969, 3723.99926758],
[2921.27368164, 458.77352905],
[255.66642761, 431.24780273]], dtype=np.float32)
return left_points, right_points
monkeypatch.setattr(bb_stitcher.picking.picker.PointPicker, 'pick', mockreturn)
# print(left_points)
rt_stitcher = stitcher.RectangleStitcher(config)
rt_stitcher.estimate_transform(
left_img['img'], right_img['img'], 90, -90)
assert rt_stitcher.homo_left is not None
assert rt_stitcher.homo_right is not None
assert rt_stitcher.pano_size is not None


@pytest.mark.slow
def test_overall_rt_stitching(left_img, right_img, outdir, config, monkeypatch):
def mockreturn(myself, image_list, all):
left_points = np.array([
[88.91666412, 3632.6015625],
[2760.26855469, 3636.70849609],
[2726.26708984, 363.4861145],
[93.88884735, 371.98330688]], dtype=np.float32)
right_points = np.array([
[181.49372864, 3687.71582031],
[2903.44042969, 3723.99926758],
[2921.27368164, 458.77352905],
[255.66642761, 431.24780273]], dtype=np.float32)
return left_points, right_points
monkeypatch.setattr(bb_stitcher.picking.picker.PointPicker, 'pick', mockreturn)
rt_stitcher = stitcher.RectangleStitcher(config)
rt_stitcher.estimate_transform(left_img['img'], right_img['img'], 90, -90)
assert rt_stitcher.homo_left is not None
assert rt_stitcher.homo_right is not None
assert rt_stitcher.pano_size is not None
pano = rt_stitcher.compose_panorama(
left_img['img_w_detections'], right_img['img_w_detections'])
detections_left_mapped = rt_stitcher.map_left_points(left_img['detections'])
detections_right_mapped = rt_stitcher.map_right_points(right_img['detections'])
pano = draw_marks(pano, detections_left_mapped)
pano = draw_marks(pano, detections_right_mapped)

out = os.path.join(outdir, 'panorama_rt_w_detections.jpg')
cv2.imwrite(out, pano)

0 comments on commit 75b1978

Please sign in to comment.