Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the bug with rasterize landmarks with matplotlib backend. #703

Merged
merged 6 commits into from Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
*~
__pycache__
.coverage
cover/*
*.pyd
Expand Down
3 changes: 2 additions & 1 deletion menpo/image/rasterize.py
Expand Up @@ -80,7 +80,8 @@ def _rasterize_matplotlib(image, pclouds, render_lines=True, line_style='-',
# Get the pixels directly from the canvas buffer which is fast
c_buffer, shape = f.canvas.print_to_buffer()
# Turn buffer into numpy array and reshape to image
pixels_buffer = np.array(c_buffer).reshape(shape[::-1] + (-1,))
pixels_buffer = np.fromstring(c_buffer,
dtype=np.uint8).reshape(shape[::-1] + (-1,))
# Prevent matplotlib from rendering
plt.close(f)
# Ignore the Alpha channel
Expand Down
68 changes: 68 additions & 0 deletions menpo/image/test/rasterize_test.py
@@ -0,0 +1,68 @@
import os
import numpy as np
import unittest
from numpy.testing import assert_allclose
from menpo.shape import PointCloud, PointUndirectedGraph
from menpo.image import Image
from menpo.image.rasterize import rasterize_landmarks_2d


centre = PointCloud([[4.5, 4.5]])
line = PointUndirectedGraph(np.array([[2, 4.5], [8, 4.5]]),
adjacency_matrix=np.array([[0, 1], [1, 0]]))


@unittest.skipIf(os.environ.get('TRAVIS'), 'Skipping due to lack of DISPLAY on Travis')
def test_rasterize_matplotlib_basic():
im = Image.init_blank([11, 11], fill=0, n_channels=1)
im.landmarks['test'] = centre
new_im = rasterize_landmarks_2d(im, group='test', render_lines=False,
marker_style='.', marker_face_colour='r',
marker_size=2, marker_edge_width=0,
backend='matplotlib')
assert new_im.n_channels == 3
assert new_im.shape == (11, 11)
assert_allclose(new_im.pixels[:, 5, 5], [255, 0, 0])


@unittest.skipIf(os.environ.get('TRAVIS'), 'Skipping due to lack of DISPLAY on Travis')
def test_rasterize_matplotlib_basic_line():
im = Image.init_blank([11, 11], fill=0, n_channels=1)
im.landmarks['test'] = line
new_im = rasterize_landmarks_2d(im, group='test', render_lines=True,
marker_style='.', marker_face_colour='r',
marker_size=2, marker_edge_width=0,
backend='matplotlib')
assert new_im.n_channels == 3
assert new_im.shape == (11, 11)
assert_allclose(new_im.pixels[0, 3, 5], [255])
assert_allclose(new_im.pixels[0, 9, 5], [255])
assert_allclose(new_im.pixels[2, 5:8, 5], [255, 255, 255])


@unittest.skipIf(os.environ.get('TRAVIS'), 'Skipping due to lack of DISPLAY on Travis')
def test_rasterize_pillow_basic():
im = Image.init_blank([11, 11], fill=0, n_channels=3)
im.landmarks['test'] = centre
new_im = rasterize_landmarks_2d(im, group='test', render_lines=False,
marker_style='s', marker_face_colour='r',
marker_size=1, marker_edge_width=0,
backend='pillow')
assert new_im.n_channels == 3
assert new_im.shape == (11, 11)
assert_allclose(new_im.pixels[0, 3:6, 3:6], 255)


@unittest.skipIf(os.environ.get('TRAVIS'), 'Skipping due to lack of DISPLAY on Travis')
def test_rasterize_pillow_basic_line():
im = Image.init_blank([11, 11], fill=0, n_channels=3)
im.landmarks['test'] = line
new_im = rasterize_landmarks_2d(im, group='test', render_lines=True,
line_width=1, line_colour='b',
marker_style='s', marker_face_colour='r',
marker_size=1, marker_edge_width=0,
backend='pillow')
assert new_im.n_channels == 3
assert_allclose(new_im.pixels[0, 1:4, 3:6], 255)
assert_allclose(new_im.pixels[0, 7:-1, 3:6], 255)
assert_allclose(new_im.pixels[2, 4:7, 4], 255)