def generate_pattern(self, reference, size):
"""Extracts a pattern from the reference image.
Firstly, the image is transformed to grayscale. A random square from image
is picked. A pattern is extracted using the edge detection (Sobel's filter).
:param reference: Reference image.
:param int size: Size of a pattern (length of its edge).
:returns:
A pattern extracted from the image.
"""
# Import image from reference and convert it to grayscale.
img = mpimg.imread(reference)
gray = np.mean(img, -1)
# Normalization of data to decimal (0.0 - 1.0) representation
if gray.max() > 1.0:
gray /= 255.0
# Extract a random slice with the pixel size given as parameter
slice_center_x = random.randint(0, len(img[0]) - size - 1)
slice_center_y = random.randint(0, len(img) - size - 1)
slice = gray[slice_center_y: slice_center_y + size, slice_center_x: slice_center_x + size]
# # Detects border to generate the pattern of the brush
dx = ndimage.sobel(slice, 0) # horizontal derivative
dy = ndimage.sobel(slice, 1) # vertical derivative
pattern = np.hypot(dx, dy) # grayscale slice with border detection
# Normalize pattern
if pattern.max() > 1.0:
return pattern / pattern.max()
return pattern
# Test
评论列表
文章目录