Skip to content
Snippets Groups Projects
Commit 589ba585 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Implemented an extractor of patches

parent 0865f4a1
Branches
Tags
1 merge request!123Drop bob.db.atnt and bob.ip.base
Pipeline #60628 failed
import numpy
A_org = numpy.array(range(1, 17), "float64").reshape((4, 4))
A_ans_0_3D = numpy.array(
[[[1, 2], [5, 6]], [[3, 4], [7, 8]], [[9, 10], [13, 14]], [[11, 12], [15, 16]]],
"float64",
)
A_ans_0_4D = numpy.array(
[[[[1, 2], [5, 6]], [[3, 4], [7, 8]]], [[[9, 10], [13, 14]], [[11, 12], [15, 16]]]],
"float64",
)
from bob.pad.face.utils.load_utils import block
def test_block():
B = block(A_org, (2, 2), (0, 0))
assert (B == A_ans_0_4D).all()
B = block(A_org, (2, 2), (0, 0), flat=True)
assert (B == A_ans_0_3D).all()
......@@ -11,6 +11,46 @@ from imageio import get_reader
from PIL import Image
def block(X, block_size, block_overlap, flat=False):
"""
Parameters
----------
X : numpy.ndarray
The image to be split into blocks.
block_size : tuple
The size of the block.
block_overlap : tuple
The overlap of the block.
Returns
-------
numpy.ndarray
The image split into blocks.
"""
assert len(block_size) == 2
assert len(block_overlap) == 2
size_ov_h = int(block_size[0] - block_overlap[0])
size_ov_w = int(block_size[1] - block_overlap[1])
n_blocks_h = int((X.shape[0] - block_overlap[0]) / size_ov_h)
n_blocks_w = int((X.shape[1] - block_overlap[1]) / size_ov_w)
blocks = numpy.zeros(shape=(n_blocks_h, n_blocks_w, size_ov_h, size_ov_w))
for h in range(n_blocks_h):
for w in range(n_blocks_w):
blocks[h, w, :, :] = X[
h * size_ov_h : h * size_ov_h + block_size[0],
w * size_ov_w : w * size_ov_w + block_size[1],
]
if flat:
return blocks.reshape(n_blocks_h * n_blocks_w, blocks.shape[2], blocks.shape[3])
return blocks
def scale(image, scaling_factor):
"""
Scales and image using PIL
......@@ -199,19 +239,21 @@ def blocks(data, block_size, block_overlap=(0, 0)):
ValueError
If data dimension is not between 2 and 4 (inclusive).
"""
data = numpy.asarray(data)
# if a gray scale image:
if data.ndim == 2:
output = block(data, block_size, block_overlap, flat=True)
# if a color image:
elif data.ndim == 3:
out_shape = list(data.shape[0:1]) + list(
block_output_shape(data[0], block_size, block_overlap, flat=True)
)
# out_shape = list(data.shape[0:1]) + list(
# block_output_shape(data[0], block_size, block_overlap, flat=True)
# )
output = numpy.empty(out_shape, dtype=data.dtype)
# output = numpy.empty(out_shape, dtype=data.dtype)
output = []
for i, img2d in enumerate(data):
block(img2d, block_size, block_overlap, output[i], flat=True)
output.append(block(img2d, block_size, block_overlap, flat=True))
output = numpy.moveaxis(output, 0, 1)
# if a color video:
elif data.ndim == 4:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment