I tried non-photorealistic rendering with Python + opencv

Introduction

I tried non-photorealistic rendering (non-photorealistic rendering: NPR) provided in the opencv library. NPR aims for non-photorealistic expressions such as handwritten paintings on input images and videos. Please imagine the actual conversion result below as a reference.

I implemented it with reference to this site. OpenCV Non-Photorealistic Rendering Learn OpenCV

result

First, I will post the execution result of NPR. Input is here Snow Sakura

httpswww.pakutaso.com20200336089post-26662.html.jpg

NPR conversion result

detailEnhance detailEnhance.png

pencilSketch pencilSketch.png

stylization stylization.png

Implementation

Below is the Python code.

NPR.py


import cv2
import argparse
from PIL import Image

def NPR(src):
    epf = cv2.edgePreservingFilter(src, flags=1, sigma_s=60, sigma_r=0.4)
    de = cv2.detailEnhance(src, sigma_s=10, sigma_r=0.15)
    pen_gray, pen_col = cv2.pencilSketch(src, sigma_s=60, sigma_r=0.1, shade_factor=0.03)
    style = cv2.stylization(src, sigma_s=60, sigma_r=0.07)
    return epf, de, pen_col, style

def webcam_or_pic2npr(out,is_webcam,pic):
    if is_webcam:
        cap = cv2.VideoCapture(0)

        while True:
            ret, frame = cap.read()
            e,d,p,s = NPR(frame)
            cv2.imshow('raw_input', frame)
            cv2.imshow('edgePreservingFilter',e)
            cv2.imshow('detailEnhance',d)
            cv2.imshow('pencilSketch',p)
            cv2.imshow('stylization',s)

            key = cv2.waitKey(1) & 0xFF
            if key == ord('q'):
                break
            elif key == ord('s'):
                cv2.imwrite(out,frame)
                cv2.imwrite(out,p)
        cap.release()
        cv2.destroyAllWindows()
    else:
        img = cv2.imread(pic, cv2.IMREAD_COLOR)
        e,d,p,s = NPR(img)
        cv2.imwrite(str(out)+'edgePreservingFilter.png',e)
        cv2.imwrite(str(out)+'detailEnhance.png',d)
        cv2.imwrite(str(out)+'pencilSketch.png',p)
        cv2.imwrite(str(out)+'stylization.png',s)

def main():
    parser = argparse.ArgumentParser(description='python+opencv_npr')
    parser.add_argument('--in_pic','-i',default='sample.png',help='input_picture_name')
    parser.add_argument('--out','-o',default='./',help='output_dir')
    parser.add_argument('--is_webcam',action='store_true',help='use webwebcam_or_pic2npr')
    args = parser.parse_args()
    webcam_or_pic2npr(args.out, args.is_webcam, args.in_pic)

if __name__ == "__main__":
    main()

Execution command (input = image)

python NPR.py -i picture_name -o output_dir 

Execution command (input = webcam)

python NPR.py --is_webcam

Impressions

It can be easily implemented with python and opencv. Details of the logic of the four types of libraries will be added if there is time. If you try various things, you can see that the image is sharp or blurred.

Summary

I tried 4 types of non-photorealistic rendering with python + opencv. Please try it with the image at hand. Thank you for reading. We would appreciate it if you could point out any injustice in the source code. LGTM is also welcome!

Recommended Posts

I tried non-photorealistic rendering with Python + opencv
I tried fp-growth with python
I tried scraping with Python
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
I tried gRPC with Python
I tried scraping with python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried "morphology conversion" of images with Python + OpenCV
I tried trimming efficiently with OpenCV
I tried web scraping with python.
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried face recognition with OpenCV
I tried "gamma correction" of the image with Python + OpenCV
I tried scraping Yahoo News with Python
I tried sending an email with python.
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
#I tried something like Vlookup with Python # 2
I tried to make an image similarity function with Python + OpenCV
Binarization with OpenCV / Python
I tried Python> autopep8
I tried Python> decorator
I tried hundreds of millions of SQLite with python
I tried object detection using Python and OpenCV
I tried to detect motion quickly with OpenCV
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
I tried function synthesis and curry with python
I tried to output LLVM IR with Python
I tried running faiss with python, Go, Rust
I tried to automate sushi making with python
I tried playing mahjong with Python (single mahjong edition)
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
"Apple processing" with OpenCV3 + Python3
I tried to extract features with SIFT of OpenCV
Image editing with python OpenCV
I tried Learning-to-Rank with Elasticsearch!
Camera capture with Python + OpenCV
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I made blackjack with python!
I tried to draw a route map with Python
[Python] Using OpenCV with Python (Basic)
I tried clustering with PyCaret
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
Face detection with Python + OpenCV
I tried Python C extension
[Python] I tried using OpenPose
Mayungo's Python Learning Episode 1: I tried printing with print
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I made blackjack with Python.
I made wordcloud with Python.