I came across an image filter with a clearly Japanese name called Kuwahara filter, and when I tried it, it was amazing, so I will introduce it.

Introduction

** What is Kuwahara filter ** The Kuwahara filter is a kind of smoothing filter devised by a university professor named Michiyoshi Kuwahara (Wikipedia says). If you want to see, take a look at the bottom of the article) Kuwahara filter -Wikipedia Data processing for SPECT (original paper?)

Contents of Kuwahara filter

image.png https://upload.wikimedia.org/wikipedia/commons/4/49/Kuwahara.jpg

To briefly explain the Kuwahara filter, This filter uses the color of each pixel as the average color of the area with the smallest sum of variances in the upper left, upper right, lower left, and lower right square areas of any width around it.

In the image above, to determine the color of the center pixel,

  1. Calculate the variance of the ** a ** area, the ** b ** area, the ** c ** area, and the ** d ** area for each RGB color, and for each area. Add all RGB to.
  2. Compare the variances obtained in 1 and calculate the average of the colors in the smallest area for each RGB.
  3. Set the average color obtained in 2 as the color of the center pixel.

Go through the steps above or the process to get equivalent results. Also, in the image, one side of the square area is 3 pixels, but it can be any width.

Implementation

** Definition **

import numpy as np
import cv2

 def kuwahara (pic, r = 5, resize = False, rate = 0.5): #Original image, square area width -1, Resize or resize ratio
    h,w,_=pic.shape
    if resize:pic=cv2.resize(pic,(int(w*rate),int(h*rate)));h,w,_=pic.shape
    filtered_pic=np.empty_like(pic)
    pic=np.pad(pic,((r,r),(r,r),(0,0)),"edge")
    ave,var=cv2.integral2(pic)
 ave = (ave [: -r-1,: -r-1] + ave [r + 1 :, r + 1:]-ave [r + 1 :,: -r-1] -ave [: -r -1, r + 1 :]) / (r + 1) ** 2 #Batch derivation of average color of area
 var = ((var [: -r-1,: -r-1] + var [r + 1 :, r + 1:]-var [r + 1 :,: -r-1]-var [:- r-1, r + 1:]) / (r + 1) ** 2-ave ** 2) .sum (axis = 2) #Batch derivation of regional variance

    for i in range(h):
        for j in range(w):
 filtered_pic [i, j] = np.array ([ave [i, j], ave [i + r, j], ave [i, j + r], ave [i + r, j + r]]) [ np.array ([var [i, j], var [i + r, j], var [i, j + r], var [i + r, j + r]]). argmin ()] #color Decision

    return filtered_pic

** Run **

import matplotlib.pyplot as plt

 pic = np.array (plt.imread ("input_picture.png ")) Change # input_picture.png to the path of the image you want to filter
filtered_pic=kuwahara(pic,7,True,0.2)
plt.imshow(filtered_pic)

result

Here are some of the photos I took when I went on a trip to France last year.

The original image IMG_20191102_111359.jpg

** After applying the filter ** コメント 2020-06-10 193044.jpg ** A smaller square area ver. ** eee.png

People, people, people, people > ** Serious painting ** <  ̄Y^Y^Y^Y^Y^Y^ ̄

Summary

I introduced it because I was impressed with the texture like drawing on canvas. It's easy so give it a try. I couldn't think of a method that doesn't use the for statement this time, so if the resizing setting is off for a large image, it will take some time. Don't forget to set the third argument to True.

Recommended Posts

I came across an image filter with a clearly Japanese name called Kuwahara filter, and when I tried it, it was amazing, so I will introduce it.
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
I made a segment tree with python, so I will introduce it
I tried to make a calculator with Tkinter so I will write it
Image processing with Python (I tried binarizing it into a mosaic art of 0 and 1)
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
When I tried the AtCoder Beginner Contest, it was a terrible result, so I look back
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
When accessing a URL containing Japanese (Japanese URL) with python3, it will be encoded in html without permission and an error will occur, so make a note of the workaround.
A memorandum when I tried to get it automatically with selenium
I came across a lambda expression when I was worried about functionalization
I made an app for foreign visitors to Japan with a hackathon and won a prize, but when I thought about it carefully, it was useless.
When I made a treemap (area graph) with python, it was subtle, so when I used flourish, it felt pretty good.
When I tried to make a VPC with AWS CDK but couldn't make it
Somehow the code I wrote worked and I was impressed, so I will post it
When I tried to create a virtual environment with Python, it didn't work
I want to write an element to a file with numpy and check it.
I was able to mock AWS-Batch with python, moto, so I will leave it
I made an image classification model and tried to move it on mobile
I tried to extract a line art from an image with Deep Learning
I tried to make a simple image recognition API with Fast API and Tensorflow
I set up TensowFlow and was addicted to it, so make a note