[PYTHON] Real-time image processing basics with opencv

Introduction

Introducing a program that displays the screen in real time with the camera attached to the personal computer

environment

Language: python3 Library: opencv Editor: jupyter notebook

Source code

import cv2

#Preparing for capture
cap = cv2.VideoCapture(0)

#From startup to screen display
while(1):
    #Creating a capture frame
    _, frame = cap.read()
    cv2.imshow('Original', frame)
    
    #Inversion of original (mirror state)
    original = cv2.flip(frame, 1)
    cv2.imshow('Inversion', original)
    
    #binarization
    gray = cv2.cvtColor(original, cv2.COLOR_RGB2GRAY)
    cv2.imshow('Binarization', gray)
    
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

"Original" is the body captured by the camera. "Inversion" looks like a mirror by flipping the original screen. "Binarization" is a binarization of the Inversion screen, which is black and white.

This time, the process is easy to understand, but depending on the person who writes the code, it can be sorted as follows.

import cv2

#Preparing for capture
cap = cv2.VideoCapture(0)

#From startup to screen display
while(1):
    #Creating a capture frame
    _, frame = cap.read()

    #Inversion of original (mirror state)
    original = cv2.flip(frame, 1)

    #binarization
    gray = cv2.cvtColor(original, cv2.COLOR_RGB2GRAY)

    
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

    cv2.imshow('Original', frame)
    cv2.imshow('Inversion', original)
    cv2.imshow('Binarization', gray)

cv2.destroyAllWindows()
cap.release()

Nothing changes the process itself. However, the following may be easier for programmers to see as a chunk of processing. This is because it looks like they are being processed one by one.

at the end

This time I did the basics in the basics. I will write something that applies this, and I can do various things including other libraries, so I recommend you to try it.

Recommended Posts

Real-time image processing basics with opencv
Basics of binarized image processing with Python
Image processing with Python & OpenCV [Tone Curve]
Light image processing with Python x OpenCV
Image processing with Lambda + OpenCV (gray image creation)
Image processing with MyHDL
Image processing with Python
Image Processing with PIL
XavierNX accelerates OpenCV image processing with GPU (CUDA)
Image processing with Python (Part 2)
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Image processing with Python (Part 1)
Real-time edge detection with OpenCV
Image processing with Python (Part 3)
Get image features with OpenCV
Image recognition with Keras + OpenCV
[Python] Image processing with scikit-image
[Python] Using OpenCV with Python (Image Filtering)
[Python] Using OpenCV with Python (Image transformation)
Image processing with Python 100 knocks # 3 Binarization
Find image similarity with Python + OpenCV
Try blurring the image with opencv2
Image processing with Python 100 knocks # 2 Grayscale
Image processing with Python 100 knock # 10 median filter
Create an image processing viewer with PySimpleGUI
Image processing with Python 100 knocks # 8 Max pooling
Image processing with Python 100 knock # 12 motion filter
Image acquisition from camera with Python + OpenCV
Drawing with Matrix-Reinventor of Python Image Processing-
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
Image processing with Python 100 knocks # 9 Gaussian filter
[Image processing] Posterization
python image processing
Image processing 100 knocks ①
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
Image processing from scratch with python (5) Fourier transform
How to crop an image with Python + OpenCV
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
I tried "binarizing" the image with Python + OpenCV
[Small story] Test image generation with Python / OpenCV
I tried simple image processing with Google Colaboratory.
Former image processing engineer tried to solve Saizeriya's spot the difference with OpenCV
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Processing datasets with pandas (1)
Processing datasets with pandas (2)
Flat Field image processing
Detect stoop with OpenCV
Binarization with OpenCV / Python
Notes on HDR and RAW image processing with Python
Image recognition with keras
First Python image processing
[OpenCV / Python] I tried image analysis of cells with OpenCV
Image capture / OpenCV speed comparison with and without GPU
Read digital image processing
MongoDB Basics: Transaction Processing
Rotate sprites with OpenCV