SerialLain3170 / adeleine

Automatic line art colorization using various types of hint or without hint

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Data Preparation

Ritsu-mio opened this issue · comments

I faced this problem When use the code reference_video. How do I prepare the distance field images of lines arts?

commented

I'm sorry for the inconvenience.

I have forgotten to explain how to get distance field images of line arts. In order to obtain them, I have used snowy like below. Later, I will upload the code and fix the documents.

import numpy as np
import imageio
import snowy


def distance_field(path):
    img = imageio.imread(path, 'JPG-PIL', pilmode='RGBA')
    img = np.clip(np.float64(img) / 255, 0, None)
    mask = np.zeros((img.shape[0], img.shape[1], 4))
    mask[img < 0.95] = 1.0
    img = np.expand_dims(mask[:, :, 0], axis=2)
    sdf = snowy.unitize(snowy.generate_sdf(img != 0.0))

    return sdf

# input_path: line art path
# out_filename: output path

dist_image = distance_field(str(input_path))
snowy.export(dist_image, str(out_filename))

That will help me a lot. Thank you

I am sorry I have the another problem.
Does this apply to get distance field images of grayscale images as well?

commented

The code above applied to .png image file. The shape of array gotten by cv2.imread(png_file_path) was (height, width, 3).

Judging from API Reference of generate_sdf, generate_udf function that is used in generate_sdf takes a grayscale image as the input like below. So you may be able to get distance field images without converting RGBA into grayscale like I did in the code above.

def generate_sdf(image: np.ndarray, wrapx=False, wrapy=False):
    a = generate_udf(image, wrapx, wrapy)
    b = generate_udf(image == 0.0, wrapx, wrapy)
    return a - b

def generate_udf(image: np.ndarray, wrapx=False, wrapy=False):
    assert image.dtype == 'bool', 'Pixel values must be boolean'
    assert len(image.shape) == 3, 'Shape is not rows x cols x channels'
    assert image.shape[2] == 1, 'Image must be grayscale'
    return _generate_edt(image, wrapx, wrapy)

thank you for your reply.