Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

モザイクパターンにつき、パターンごとにスタイルの調査 #1

Closed
huhudev-git opened this issue Nov 25, 2020 · 5 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@huhudev-git
Copy link
Owner

huhudev-git commented Nov 25, 2020

あらすじ

現在モザイククラスは下のように設計している

class AbstructMosaicStyle(abc.ABC):
"""モザイクパターンのスタイルパラメータ
"""
@abc.abstractclassmethod
def from_json(cls, json_data):
return cls()

class Image():
def __init__(self, image_data: bytes):
self.data = image_data
def get_size(self):
pass

class Position():
def __init__(self, startX: int, startY: int, endX: int, endY: int) -> None:
self.startX = startX
self.startY = startY
self.endX = endX
self.endY = endY

class AbstractMosaicFilter(abc.ABC):
# フロントエンドのほうのモザイクパターンの名前
name = None
@abc.abstractmethod
def process(self, image: Image, style: AbstructMosaicStyle, positions: List[Position]) -> bytes:
'''写真を処理し、モザイクをつけた写真を返す
Args:
image (bytes): アップロードした写真
style (Any): モザイクのスタイル
positions (List[Position]): 顔認識の位置
Returns:
bytes: モザイクをつけた写真
'''
pass

そこで、具体的なFilterは三パターンある

  • EyesLineMosaicFilter
  • GaussBlurMosaicFilter
  • PixelBlurMosaicFilter

それぞれのパタ―ンについて、スタイルは異なる、例えば:線の厚さ、ガウスの強さ、ピクセルの大きさなど
Filterごとに必要なスタイルをまとめて、整理する必要がある。 整理したスタイルは、以下のように実装する予定です

class AbstructMosaicStyle(abc.ABC):
"""モザイクパターンのスタイルパラメータ
"""
@abc.abstractclassmethod
def from_json(cls, json_data):
return cls()

@huhudev-git huhudev-git changed the title モザイクパターンにつき、パターンごとにの調査 モザイクパターンにつき、パターンごとにスタイルの調査 Nov 25, 2020
@huhudev-git huhudev-git added the documentation Improvements or additions to documentation label Nov 25, 2020
@k1105
Copy link
Collaborator

k1105 commented Nov 28, 2020

ぼかしフィルター=ガウスぼかしについて。
改めてガウスぼかしについて調べたところ、ガウスぼかしは一般的に、「ノイズ除去」を目的とした処理に使われ、ぼかしの強度を上げてもエッジが残りやすい性質があるみたい(畳み込み演算を行うとき、注目画素付近の重みが大きいため)。
今回の目的は平滑化を通して「顔を隠す」ことにあるため、平均フィルタを用いる方が適当なのかも。

加えて、ここでもし平均フィルタを使うのであれば、

class boxFilterStyle(AbstructMosaicStyle):
    def __init__(self, kernel_size):
        self.kernel_size = kernel_size

のようになるのかな? 実際、cv2の平均フィルタを呼び出す際は

blur = cv2.blur(img,(5,5)) #(img, (kernel_width, kernel_height))

となるので

@huhudev-git
Copy link
Owner Author

@k1105
しかし、サンプルから見ると、平均フィルタあまり効果がないようだが。エッジが残りやすいとは具体的にどのようになるか。確かに、モザイク自身のエッジが強くなるかもしれない、これをある程度の過渡の処理で弱くなれるでしょうか
また、画像処理の授業では、平均フィルタはノイズ除去に使われていると説明しているが。もちろんフィルターを一つに限定しなくてもいい、複数の組み合わせてもいけると思う

@k1105
Copy link
Collaborator

k1105 commented Dec 3, 2020

ガウスぼかしについて:
簡単にガウスぼかしを用いたモザイク処理について実装してみたところ、ガウスぼかしのカーネルサイズを変えただけではカーネルサイズをかなり大きくしないとボケてくれない+カーネルサイズを大きくすると実行に膨大な時間がかかるので、再帰的にガウスぼかしを呼び出して実装しました。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('draemon.png')
h, w, c = img.shape
level = 10

resize = cv2.resize(img, (int(w*100/h), 100))
dst = resize

for i in range(1,level):
    dst = cv2.GaussianBlur(dst,(5,5),10)

また、levelが一緒でも画素数によってぼかしの強さが変わってしまうので、一度リサイズして縦100ピクセルに調整する処理を受け取った画像に対して行っています。

一連の操作のなかで必要になった変数はlevelのみ。反復処理の回数指定に用いるので、整数型。値の範囲は10~100程度。

@k1105
Copy link
Collaborator

k1105 commented Dec 3, 2020

image

@k1105
Copy link
Collaborator

k1105 commented Dec 3, 2020

ピクセルモザイクについて:
実装は以下。一度画像を縮小して、nearest neighborによって補完。例によって一度画像を縦100pxにリサイズしてから戻している。

img = cv2.imread('draemon.png')
h, w, c = img.shape
level = 10

resize = cv2.resize(img, (int(w*100/h) // level, 100 // level), interpolation=cv2.INTER_NEAREST)
dst = cv2.resize(resize, (w, h), interpolation=cv2.INTER_NEAREST)

ここでも必要になったパラメータはlevelのみ。値域は2~10程度。
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants