[PYTHON] Play with demons because it's setsubun

Introduction

I'm afraid of demons, so I will repel them with a script.

What i did

I will neutralize the scary demon (image below).

setsubun_oni_kowai.png (Source: Irasutoya "https://www.irasutoya.com/2020/01/blog-post_891.html")

It's a scary demon.

grayscale.png

First, grayscale it. Maybe I'm even more scared. otsu.png

Use the polarization of Otsu. At this point the demon seems weak.

oni.png

Fine lines with Zhang-Suen's algorithm. I'm happy to be able to write in one line. Maybe it's okay to be attacked.

Below is the code I used.

oni-taizi.rb


from skimage.morphology import skeletonize
import cv2
import numpy as np

#Read
img = cv2.imread('setsubun_oni_kowai.png')

#Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

#Polarization of Otsu
ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)

#Image 0,Convert to 1
th[th>0]=1    

#Thinning
skeleton = skeletonize(th)

#Image 0,Convert to 255
output = np.where(skeleton, 0, 255)

#Save
cv2.imwrite("oni.png ", output)

in conclusion

Whether or not you were able to get rid of it depends on your personal subjectivity. Thank you for visiting.

Recommended Posts

Play with demons because it's setsubun
Play with Prophet
Play with PyTorch
Play with 2016-Python
Play with CentOS 8
Play with Pyramid
Play with Fathom
Play with Othello (Reversi)
Let's play with 4D 4th
Play with reinforcement learning with MuZero
Play with push notifications with imap4lib
Play around with Linux partitions
Let's play with Amedas data-Part 4
Play with Jupyter Notebook (IPython Notebook)
[Python] Play with Discord's Webhook.
Play RocketChat with API / Python
Let's play with Amedas data-Part 3
Let's play with Amedas data-Part 2
Play with A3RT (Text Suggest)