Draw a watercolor illusion with edge detection in Python3 and openCV3

environment

windows8.1  python3.4.4  opencv3.1.0

Overview

Load a color image and create a watercolor illusion.

The watercolor illusion is to put color only on the outline of a painting or photograph. Phenomenon that the area that is not actually colored looks slightly colored ... And, talking about vision will be long, so here are some links. Examples of watercolor illusions are also posted.

Illusion of light and darkness and color caused by edges-Hitoshi Arai's homepage Illusion Forum CVRM

image

The image used this time is from the well-known "Irasutoya"

"Specialty goods" test1.png

Narutake I searched for various images with my blood

Body

edge.py


import numpy as np
import cv2

"""
Environment: python3.4.4 openCV
Original image: image_name
Image to save: out_name
image_Convert name to watercolor illusion and output
"""

image_name = 'test1.png'
out_name = 'out1.png'


#Loading images
Img0 = cv2.imread(image_name)


#The image is black and white inverted for convenience with the later mask
negImg = 255 - Img0


#Get the size of the image and specify the thickness of the outline
ImgHeight, ImgWidth = Img0.shape[:2]
#The longer one/200 + 1
if ImgWidth > ImgHeight:
	lineWeight = ImgHeight // 200 + 1
else:
	lineWeight = ImgWidth // 200 + 1


#Get the edge(Two-step threshold 100 200)
#I will match two contours with different thicknesses later, so prepare two images
edgeImg1 = cv2.Canny(Img0,100,200,4)
edgeImg2 = cv2.Canny(Img0,100,200,4)


#Object from black background(White)Detect the contour of
image1, contours, hierarchy = cv2.findContours(edgeImg1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
image2, contours, hierarchy = cv2.findContours(edgeImg2,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)


#Black contour
edge = cv2.drawContours(image1, contours, -1, (255,255,255), 1)
#Color around the black outline(Watercolor element)
mask1 = cv2.drawContours(image2, contours, -1, (255,255,255), lineWeight)


#Since edge is white on a black background, it is displayed in reverse
negEdge = 255- edge


#Cut the inverted image with a mask
negColor = cv2.bitwise_and(negImg, negImg, mask=mask1)


#Invert the cropped inverted image again
Color = 255-negColor


#Image to output
output = cv2.bitwise_and(Color, Color, mask=negEdge)
cv2.imshow('image', output)

#Press the esc key to finish
#Press s to save
k = cv2.waitKey(0) & 0xFF
if k == 27:
    cv2.destroyAllWindows()
elif k == ord('s'):
    cv2.imwrite('out01.png',output)

#python edge.Run with py

output

How about. Does it look colored? out01.png

Various

Edge detection is easy because it provides a function.

However, since edge detection by Canny () is based on only two thresholds, it is not possible to make a delicate boundary judgment like the human eye. That's why the leaves of carrots have disappeared, and the rice has become one and looks like crushed beans that I don't understand.

Another thing, repeating inversions is not very smart. However, when I was trying to leave the black edges or whiten the non-edges, the result was like this. It would be a little easier if the background could be whitened ...

reference

Irasutoya OpenCV-Python Tutorials 1 documentation The process of learning Python Pinna, B., Brelstaff, G., and Spillmann, L. (2001) Surface color from boundaries: a new ‘watercolor’ illusion. Vision Research, 41, 2669-2676.

Recommended Posts

Draw a watercolor illusion with edge detection in Python3 and openCV3
[Python] Using OpenCV with Python (Edge Detection)
Create a striped illusion with gamma correction for Python3 and openCV3
Hello World and face detection with OpenCV 4.3 + Python
Try edge detection with OpenCV
Real-time edge detection with OpenCV
Face detection with Python + OpenCV
Draw a heart in Python
Shining life with Python and OpenCV
Neural network with OpenCV 3 and Python 3
Draw a scatterplot matrix in python
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Draw a CNN diagram in Python
Draw shapes with OpenCV and PIL
Draw arrows (vectors) with opencv / python
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
Face detection with Python + OpenCV (rotation invariant)
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Draw a heart in Ruby with PyCall
Draw Nozomi Sasaki in Excel with python
Capturing images with Pupil, python and OpenCV
A memo with Python2.7 and Python3 on CentOS
Draw a heart in Python Part 2 (SymPy)
Dealing with "years and months" in Python
Draw a tree in Python 3 using graphviz
Note installing modules such as pytorch and opencv with pip in Blender python (2.82a or later)
Put OpenCV in OS X with Homebrew and input / output video with python
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
Draw a graph of a quadratic function in Python
[Python] Get the files in a folder with Python
Draw a graph with Japanese labels in Jupyter
Building a python environment with virtualenv and direnv
Edge extraction with python + OpenCV (Sobel filter, Laplacian filter)
Try to draw a life curve with python
I wrote a class in Python3 and Java
Create a virtual environment with conda in Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Launch a web server with Python and Flask
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
[Python] How to draw a histogram in Matplotlib
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
Put Docker in Windows Home and run a simple web server with Python
How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV
You can do it in 5 minutes !? Create a face detection API with FastAPI and OpenCV and publish it on Heroku
[Python] Draw a Qiita tag relationship diagram with NetworkX
Automate background removal for the latest portraits in a directory with Python and API
How to convert / restore a string with [] in python
Calculate and draw bounded (closed) Voronoi diagrams in Python
Binarization with OpenCV / Python
Build a python virtual environment with virtualenv and virtualenvwrapper
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
I made a program to convert images into ASCII art with Python and OpenCV
Playing with a user-local artificial intelligence API in Python
I tried to draw a route map with Python
Make a simple Slackbot with interactive button in python
Carefully understand the exponential distribution and draw in Python