Create a striped illusion with gamma correction for Python3 and openCV3

environment

windows8.1 python3.4.4 opencv3.1.0

Overview

First of all, please see the actual striped illusion.

out03.jpg

Can't you see this kind of squirrel? (The image is from "Free Stock Photo Material Pakutaso") out1.jpg

I will try it with illustrations. As usual, this is an illustration of a Mexican borrowed from "Irasutoya". mexico.jpg

Let's make this a striped illusion.

procedure

It is roughly divided into three stages.

  1. Convert the image to monochrome
  2. Brighten with gamma correction
  3. Combine with stripes

Body

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

output

out01.jpg

Can you see it? I'm a healthy Mexican.

Details

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

reference

BlankTar FROM UMENTU IMPORT STUPID opencv v2.1 documentation array operations Free Stock Photos Irasutoya

Recommended Posts

Create a striped illusion with gamma correction for Python3 and openCV3
Draw a watercolor illusion with edge detection in Python3 and openCV3
Create a LINE BOT with Minette for Python
Create a Layer for AWS Lambda Python with Docker
Create a directory with python
Create a High Dynamic Range Image (HDR) with OpenCV and Python (Mertens, Robertson, Debevec)
I tried "gamma correction" of the image with Python + OpenCV
Create a child account for connect with Stripe in Python
[Python] Create a date and time list for a specified period
Let's create a PRML diagram with Python, Numpy and matplotlib.
Installation procedure for Python and Ansible with a specific version
Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a simple video analysis tool with python wxpython + openCV
Library for specifying a name server and dig with python
[Python] Create a screen for HTTP status code 403/404/500 with Django
Shining life with Python and OpenCV
Create a virtual environment with Python!
Create a Python execution environment for Windows with VScode + Remote WSL
Create a color picker for the color wheel with Python + Qt (PySide)
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
Create a CGH for branching a laser in Python (laser and SLM required)
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Create a USB boot Ubuntu with a Python environment for data analysis
Create and return a CP932 CSV file for Excel with Chalice
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Capturing images with Pupil, python and OpenCV
A memo with Python2.7 and Python3 on CentOS
Create and decrypt Caesar cipher with python
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create a word frequency counter with Python 3.4
[Python] Read images with OpenCV (for beginners)
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Create your own virtual camera with Python + OpenCV and apply original effects
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
[Python] How to create a dictionary type list, add / change / delete elements, and extract with a for statement
Hello World and face detection with OpenCV 4.3 + Python
Create a frame with transparent background with tkinter [Python]
Causal reasoning and causal search with Python (for beginners)
Get a ticket for a theme park with python
Building a python environment with virtualenv and direnv
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Create a dashboard for Network devices with Django!
Create a web map using Python and GDAL
Create a color bar with Python + Qt (PySide)
Steps to create a Twitter bot with python
Launch a web server with Python and Flask