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
First, I will post the execution result of NPR. Input is here Snow Sakura
NPR conversion result
detailEnhance
pencilSketch
stylization
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()
python NPR.py -i picture_name -o output_dir
python NPR.py --is_webcam
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.
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