brush.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:ccmas-imgen 作者: ccmas-imgen 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号