windows8.1 python3.4.4 opencv3.1.0
First of all, please see the actual striped illusion.
Can't you see this kind of squirrel? (The image is from "Free Stock Photo Material Pakutaso")
I will try it with illustrations. As usual, this is an illustration of a Mexican borrowed from "Irasutoya".
Let's make this a striped illusion.
It is roughly divided into three stages.
stripe.py
import numpy as np
import cv2
"""
Environment: python3.4.4 openCV
Original image: image_name
Image to save: out_name
Output the image as a stripe illusion
(At first glance it's just a stripe, but when you look at it while shaking your head, an optical illusion emerges.)
"""
image_name = 'mexico.jpg'
out_name1 = 'out01.jpg'
out_name2 = 'out02.jpg'
out_name3 = 'out03.jpg'
#Loading images
Img0 = cv2.imread(image_name,-1)
#Convert to monochrome
Img0 = cv2.cvtColor(Img0, cv2.COLOR_RGB2GRAY)
#Get the size of the image
Height, Width = Img0.shape[:2]
#Matrix size
rows = Height
cols = Width
#Create a pure white background
whiteImg = np.zeros((rows, cols), np.uint8)
whiteImg.fill(255)
#Synthesized with the original Img0
whiteImg += Img0
#Display on the way
cv2.imshow("white back image", whiteImg)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite(out_name1,whiteImg)
#Brightness adjustment(Gamma correction)
#Gamma value(Much larger than usual)
gamma = 19.0
#Array for conversion look_up_Make a table
look_up_table = np.zeros((256, 1), np.uint8)
for i in range(256):
look_up_table[i][0] = 255 * pow(float(i) / 255, 1.0 / gamma)
#Convert the previous image with LookUpTable
newImg = cv2.LUT(whiteImg, look_up_table)
#Display on the way
cv2.imshow("higher brightness image", newImg)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite(out_name2,newImg)
#Make an array for stripes.
stripe = np.zeros((rows, cols), np.uint8)
#The width of the stripe is width/160(Rule of thumb)
if Width >= 640:
w = Width/160
else:
w = 4
w = int(w)
#0~640 in white, 4 each
for i in range(0, w):
stripe[0:rows, i:cols:w*2] = 0
#0~64 in black by 4
for i in range(0, w):
stripe[0:rows, i:cols:w*2] = 1
#When treating an image as a matrix, not the logical product
#It is treated as a multiplication of numbers, so change it from 0 to 1.
#Overlay stripes
newImg *= stripe
#Image to output
cv2.imshow('final stripe image', newImg)
#Press any key to finish
#Press s to save
k = cv2.waitKey(0) & 0xFF
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite(out_name3,newImg)
#python stripe.Run with py
Can you see it? I'm a healthy Mexican.
If there is a transparent part such as png, the transparent part will be black, so it is necessary to make the background white. This time, by adding 255 to all the pixel values, the part with brightness 0 is set to 255 (white), and the other parts are values obtained by subtracting 1 from the original pixel value (for example, 1 if the pixel value is 1). I took the method of setting +255 = 256, that is, 0).
The gamma correction formula is [here](https://www.blog.umentu.work/python-opencv3%E3%81%A7%E3%82%AC%E3%83%B3%E3%83%9E%E5 Please check with% A4% 89% E6% 8F% 9Bgamma-conversion-2 /). Since the strength of the optical signal is not proportional to the electrical signal, we do not raise the value uniformly, but make such a correction.
In addition, we will introduce the following three matrix calculations that are convenient for image processing.
-Generate a matrix with all elements 0
Img = np.zeros((rows, cols), np.uint8)
-Fill the matrix with a certain value
Img.fill(255)
-Substitute values from (row1, col1) to (row2, col2) in the matrix at w1 intervals in the column direction and w2 intervals in the row direction.
Img[row1:row2:w1, col1:col2:w2] = 0
BlankTar FROM UMENTU IMPORT STUPID opencv v2.1 documentation array operations Free Stock Photos Irasutoya
Recommended Posts