windows8.1 python3.4.4 opencv3.1.0
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
The image used this time is from the well-known "Irasutoya"
"Specialty goods"
Narutake I searched for various images with my blood
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
How about. Does it look colored?
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 ...
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