0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

OpenCVの画像フィルタを使ってみた

Posted at

OpenCVのいくつかのフィルタを使ってみました。

環境

環境はこちらで作成した環境です。

ソースコード

image_filter.py

#-*- coding:utf-8 -*-
import cv2
import numpy as np

def main():
    # 入力画像を読み込み
    img = cv2.imread("input.jpg")

    # グレースケール変換
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cv2.imwrite("gray.jpg", gray )
    
    # medianフィルタ
    median = cv2.medianBlur(gray,9)
    cv2.imwrite("median.jpg", median )
    
    # sobelフィルタ
    gray_x = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=3)
    gray_y = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3)
    sobel = np.sqrt(gray_x ** 2 + gray_y ** 2)
    cv2.imwrite("sobel.jpg", sobel)

    # Gaussianフィルタ処理
    gauss = cv2.GaussianBlur(gray, (11,11), 5.0)
    cv2.imwrite("gauss.jpg", gauss )
    

if __name__ == "__main__":
    main()

実行

input.jpgを自分がいじりたい画像に変えて

$ python image_filter.py

を実行すれば完了です。

入力画像

この画像を入力に使いました。
image.png

出力

gray.jpg
image.png

median.jpg
image.png

sobel.jpg
image.png

gauss.jpg
image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?