[PYTHON] I tried using the image filter of OpenCV

I tried using some filters of OpenCV.

environment

The environment is the environment created by here.

Source code

image_filter.py



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

def main():
    #Read the input image
    img = cv2.imread("input.jpg ")

    #Grayscale conversion
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cv2.imwrite("gray.jpg ", gray )
    
    #median filter
    median = cv2.medianBlur(gray,9)
    cv2.imwrite("median.jpg ", median )
    
    #sobel filter
    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 filtering
    gauss = cv2.GaussianBlur(gray, (11,11), 5.0)
    cv2.imwrite("gauss.jpg ", gauss )
    

if __name__ == "__main__":
    main()

Run

Change ʻinput.jpg` to the image you want to play with

$ python image_filter.py

Execute and you're done.

Input image

I used this image for input. image.png

output

gray.jpg image.png

median.jpg image.png

sobel.jpg image.png

gauss.jpg image.png

Recommended Posts

I tried using the image filter of OpenCV
I tried using GrabCut of OpenCV
I tried "gamma correction" of the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried to correct the keystone of the image
I tried "binarizing" the image with Python + OpenCV
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried using the API of the salmon data project
I tried to compress the image using machine learning
Judgment of backlit image using OpenCV
I tried using the checkio API
I tried to find the entropy of the image with python
I tried refactoring the CNN model of TensorFlow using TF-Slim
I tried face recognition of the laughter problem using Keras.
[Python] I tried collecting data using the API of wikipedia
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried the asynchronous server of Django 3.0
I checked the options of copyMakeBorder of OpenCV
I tried playing with the image with Pillow
I tried using the BigQuery Storage API
I tried to get the index of the list using the enumerate function
I tried to process the image in "sketch style" with OpenCV
I looked at the meta information of BigQuery & tried using it
I tried to digitize the stamp stamped on paper using OpenCV
I just erased the object using image repair (inpaint) (OpenCV: Python)
I tried to process the image in "pencil style" with OpenCV
I tried using parameterized
I tried using argparse
I tried using scrapy for the first time
I tried the pivot table function of pandas
I tried using mimesis
I tried cluster analysis of the weather map
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried using anytree
vprof --I tried using the profiler for Python
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried using aiomysql
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried object detection using Python and OpenCV
I tried using Summpy
I tried using PyCaret at the fastest speed
I tried using coturn
I tried using Pipenv
I tried using the Google Cloud Vision API
I tried to touch the API of ebay
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using the trained model VGG16 of the deep learning library Keras
I tried using the Datetime module by Python
I tried to extract and illustrate the stage of the story using COTOHA
I tried using ngrok
I tried using Jupyter
I tried using PyCaret
I tried using Heapq