[PYTHON] OpenCV aiming for a decent line art

</ i> Overview

Take a line art from a color image. A line art is a monochrome image, not binarized (probably) When I think about using OpenCV, I read it in grayscale → I came up with binarization with ʻadaptiveThreshold`, but it's not very good. So, I will verify how the comments of the topic are done.

</ i> Reference

</ i> code


# -*- coding: utf-8 -*-
"""
Created on Fri Jan 27 11:30:12 2017

@author: khsk
"""

import numpy as np
import cv2 as c
import glob
import os

#8 Definition of neighborhood
neiborhood8 = np.array([[1, 1, 1],
                            [1, 1, 1],
                            [1, 1, 1]],
                            np.uint8)

for path in glob.glob('./images/eupho/*'):
    if (os.path.basename(path) == 'Thumbs.db'):
        continue
    img = c.imread(path, 0) #Color without 0
    img_dilate = c.dilate(img, neiborhood8, iterations=1)
    img_diff = c.absdiff(img, img_dilate)
    img_diff_not = c.bitwise_not(img_diff)
    #gray = c.cvtColor(img_diff_not, c.COLOR_RGB2GRAY)
    
    #at = c.adaptiveThreshold(img_diff_not, 255, c.ADAPTIVE_THRESH_GAUSSIAN_C, c.THRESH_BINARY, 7, 8) #Adjust int nicely
    c.imwrite(os.path.dirname(path) + '_clean_senga_color_gray/' + os.path.basename(path), img_diff_not)
    
c.imshow('test',img)
c.imshow('test2',img_dilate)
c.imshow('test3',img_diff)
c.imshow('test4',img_diff_not)
c.waitKey(10000)

c.destroyAllWindows()

Basic code. Comment out, change variables, and mess with int values. There seems to be an expansion of around 4, but if you want to make it clearer, I thought it might be around 8 more. However, unless otherwise stated, reading is in grayscale.

</ i> Original image

still_01.jpg

I remember seeing names such as euphonium in machine learning research, but I forgot the details. Is it ideal for machine learning? Are you making it using it? It has nothing to do with line art. Probably.

</ i> Binarization

ʻAdaptive Threshold` only

still_01.jpg

The lines are rattling and this is fine, but sometimes there are black spots like dust.

</ i> 1 expansion

still_01.jpg

I was surprised because it was too beautiful. Speaking of points of concern, there are shades and faint lines often appear.

hibikeyufoirasuto01002.jpg

</ i> Expansion 3.5 times

still_01.jpg

still_01.jpg

I increased the number of expansions so that if the difference becomes thicker, even a thin line will appear clearly, The feeling of strangeness spreads just by making the dark lines thicker. It may still be good if it is twice.

</ i> Color difference → Grayscale

still_01.jpg

still_01.jpg

Since the reference tweet was grayscale at the end, I tried to process the first reading in color. (1 expansion) I think it's about an error. The color line art has a taste.

</ i> After one expansion, binarize

still_01.jpg

It's a beautiful line art, but I thought it wasn't binarized, so I binarized it at the end. It feels like the amount of information has been reduced from binarization.

</ i> 1 expansion after binarization

still_01.jpg

Up to this point, I noticed that the expansion seems to be applied to the binarized image, so follow the procedure of reading → binarization → expansion → difference. The difference can no longer be expressed in words.

</ i> End

I think it's been decent. I don't know what a "good line art" is in the first place.

I was thinking about creating a dataset, but I feel like creating data for machine learning with machine learning. Rough line art has already been studied, so ambiguous lines can be complemented.

So, I haven't stepped into machine learning, but when I was wondering if the data preparation had taken a step forward [because the trained model was made into a web service instead of being released], it is definitely better to play here. pleasant.

QFW455OLKMO3EC3E1OSRTD4T8ID75DNV_0.jpg

The hair color matches great.

[Pixel expansion processing (dilation) and contraction processing (erosion) with Python OpenCV3 (with a little explanation) --from umentu import stupid]: https://www.blog.umentu.work/python-opencv3%E3%81%A7 % E7% 94% BB% E7% B4% A0% E3% 81% AE% E8% 86% A8% E5% BC% B5% E5% 87% A6% E7% 90% 86dilation% E3% 81% A8% E5 % 8F% 8E% E7% B8% AE% E5% 87% A6% E7% 90% 86erosion-% E3% 81% A1% E3% 82% 87% E3% 81% A3% E3% 81% A8% E8% A7% A3 /