[PYTHON] Watershed makes undercoating more efficient

Have you ever encountered the following situations while trying to paint a picture?

  1. When I tried to relax with the fill tool (bucket), the lines were not connected.
  2. I don't know what color to paint.
  3. It is troublesome to paint in the first place.

I have Besides, it's always. Especially, there are so many cases of 1, can you somehow make the work of painting more efficient? It is a story that I thought and tried it.

Caution ・ If your main purpose is to draw a picture, you should find a paint tool that suits you and learn useful functions.

How to solve the problem

Each of the three issues listed above

  1. When I tried to relax with the fill tool (bucket), the lines were not connected.
  1. I don't know what color to paint.
  1. It is troublesome to paint in the first place.

It seems that it can be solved by such a method.

It is a little impossible to solve everything (especially 3), but here I decided to create a painting tool for simplification to some extent.

Attention ・ The topic has changed from drawing a picture to creating a program.

Solution

In conclusion, 1 and 2 can be solved (to some extent) by using Watershed. Specifically, it looks like this.

image Description
Line art you want to paint
ideal
reality
Watershed

Where you are actually painting

Is it better to apply it normally? That's the end of the story ...

About Watershed

Watershed is one of the image area division techniques used to extract and judge areas when recognizing objects. It is an algorithm to do. The name comes from a technique that treats the brightness of pixels as a gradient and allows water to flow into it.

For Qiita, the algorithm is explained in the article "Details of the object segmentation algorithm" watershed "" posted by ysdyt.

When processing a color photograph like the one above, it may be difficult to apply it without preprocessing such as brightness correction, but in the case of line art in an illustration, it is originally monochrome or grayscale, so it works relatively well. To go.

The front pretending is long, that is, I want to paint something like this.

About the implementation of Watershed

Speaking of image processing, first of all, using OpenCV ..., but OpenCV itself comes with a sample. So, for this work, I just made the GUI look like that.

The development language is Python 3.6.1. I used the following libraries.

Operation check

Although it is a fill with Watershed, it behaves differently from a general paint app. (That's a different purpose)

First, OpenCV's Watershed implementation requires at least two colors (three colors including unpainted areas). If you place only one color, the entire area will be filled regardless of the target image.

The fill starts where you put the pin, but how the fill stops where the line isn't closed depends on how far the pin is and how far it is from the other pins. If it does not fill as expected, you may need to move the pin location or increase the number of pins.

Try applying it to line art

watershed_image_1.jpg

If you don't like the way you fill, you can remove the pin and retype or move it.

watershed_image_2.jpg

Maybe it feels good ...

watershed_image_3.jpg

When the coloring is completed, it will be output as a divided image without antialiasing by executing Export Picture.

watershed_image_4.jpg

Even after the fill is completed, you can change the color by changing the palette in the left tree.

It worked pretty well.

About the program

The program itself has been uploaded to Github, so you can get it from the following location.

--I also created binaries for Windows and Mac with cx_Freeze.

how to use

It's a tool for me, but how to use it.

Loading image files

Currently, the only supported images are PNG and JPEG formats. Press the Import Picture button to bring up the file selection dialog or drop the file into the window to import it.

Export image file

When you press the Export Picture button, the file save dialog will be displayed. Save it with a name. The file format is saved according to the extension.

Read pin file

You can specify the filled area and save the current palette.

Export pin file

You can specify the filled area and save the current palette. It will be saved in YAML with a simple structure. (The image is not saved.)

Other operations

--Scale with mouse wheel --Move the display area by dragging the left mouse button while holding down the space key. --Delete the pin specified by the DEL (BS) key

Recommended Posts

Watershed makes undercoating more efficient
[Python] Debugging is more efficient!