Introduction to OpenCV (python)-(2)

This is a continuation of the following article (I want to make something because there are only input articles ...)

https://qiita.com/tom_S/items/b85aee14f34dcc856e54

histogram

It is possible to express the distribution of pixel values such as whether there are many reds or many blues in the image.

The program that displays the RGB value histogram as a graph is shown below.

import cv2
import matplotlib.pyplot as plt #Library for image plots
%matplotlib inline #Library for image plots

img = cv2.imread("Image file path here")

color_list = ["blue", "green","red"]
for i,j in enumerate(color_list):
    hist = cv2.calcHist([img],[i],None,[256],[0,256]) # blue, green,Calculate in the order of red
    plt.plot(hist,color= j)

Further, by making the histogram uniform, the bright part can be made brighter and the dark part can be made darker. Uniformize the histogram by calling the equalizeHist function as follows:

import cv2

img = cv2.imread("Image file path here")
img_eq = cv2.equalizeHist(img) # img_An image with a uniform histogram is included in eq

Gamma conversion

Roughly speaking, the method of changing the brightness of an image is called gamma conversion. The conversion is performed using a gamma table (lookup table) such as the following formula. When the value of r is larger than 1, it becomes brighter, and when it is less than 1, it becomes darker.

y = 255* (x/255)^(1/r)

The source code is shown below. (The explanation of the lookup table is omitted)

import cv2
import numpy as np

gamma = 1.5 #R in the above equation
img = cv2.imread("Image file path here")
gamma_cvt = np.zeros((256,1), dtype= np.uint8)
for i in range(256):
    gamma_cvt[i][0] = 255 * (float(i)/255) ** (1.0/gamma)
    
img_gamma = cv2.LUT(img, gamma_cvt) #img_The image after gamma conversion is entered in gamma

I think it's hard to understand what kind of effect it has, so I'll put a link to a site that seems to be helpful below. http://peaceandhilightandpython.hatenablog.com/entry/2016/02/05/004445

Afine conversion

The motion of moving an image by combining translation and rotation and the transformation (shear deformation) that transforms a rectangle into a parallelogram are collectively called affine transformation. Affination conversion is performed by performing matrix calculation.

Since translation requires summing the matrices, homogeneous coordinates are added to the matrix to shrink the linear transformation, and a transformation matrix is created to create an expression between singular terms. Shows the source code for both translation and rotation

Translation

import cv2
import numpy as np

img = cv2.imread("Image file path here")
h,w = img.shape[:2]
dx,dy = 30,30

afn_mat = np.float32([[1,0,dx],[0,1,dy]]) #Translation only. Shift by 30 vertically and horizontally
img_afn = cv2.warpAffine(img, afn_mat,(w,h)) # img_Image after translation is entered in afn

rotation

import cv2
import numpy as np

img = cv2.imread("Image file path here")
h,w = img.shape[:2]

rot_mat = cv2.getRotationMatrix2D((w/2,h/2),40,1) # (Coordinates of the center of rotation,rotation angle,Image scale(1 is the same size))give
img_afn2 = cv2.warpAffine(img, rot_mat,(w,h)) # img_The image after rotation is included in afn

Convolution

Updating your own pixel value using surrounding information is called convolution.

How to fold

  1. Prepare a filter

  2. Perform "(pixel value) x (filter)" around the pixel of interest and add (this is called convolution).

  3. Place on all pixels and convolve

Filters that use convolution

Smoothing filter

The pixel value is calculated from the surrounding information and noise is removed. The source code is shown below

import cv2 
import numpy as np

kernel = np.ones((3,3)) /9.0 #Filter used for convolution

img = cv2.imread("Image file path here", 0)
img_kel = cv2.filter2D(img, -1, kernel) #The argument "-1 "is the original image(img)Means to return.Perform convolution (smoothing)

sobel filter

Vertical edges can be detected by differentiating. An example source code is shown below.

import cv2 
import numpy as np

kernel2 = np.zeros((3,3))
kernel2[0,0] = 1
kernel2[1,0] = 2
kernel2[2,0] = 1
kernel2[0,2] = -1
kernel2[1,2] = -2
kernel2[2,2] = -1

img = cv2.imread("Image file path here", 0)
img_ke2 = cv2.filter2D(img, -1,kernel2) #sobel filter

Smoothing filter

--Linear transformation

A filter that evenly smoothes and blurs the entire surface. The source code is shown below.

import cv2 

img = cv2.imread("Image file path here")

img_blur = cv2.blur(img, (3,3)) #Smoothing filter
img_ga = cv2.GaussianBlur(img,(9,9), 2) #2 is the variance value. The larger this value, the more blurred(Gaushan)
img_me = cv2.medianBlur(img, 5) #Rewrite with mode(Blurred)Median filter

--Nonlinear transformation

There is a bilateral filter for non-linear transformation. This is to leave the portion where the brightness change is drastic and smooth the portion where the brightness change is gradual. The source code is shown below.

import cv2 

img = cv2.imread("Image file path here")

img_bi = cv2.bilateralFilter(img,20,30,30)

Morphology operation

An operation consisting of expansion and contraction. It is used to separate images by contracting and to integrate them by expanding. In addition, noise can be eliminated by contracting and expanding. The source code is shown below.

import cv2

img = cv2.imread("Image file path here")
ret, img_th = cv2.threshold(img, 110, 255, cv2.THRESH_BINARY) #Binarization

kernel = np.ones((3,3),dtype = np.uint8) #If there is white in the vicinity of 8 around, it will be white, if there is a lot of black, it will be black
img_d = cv2.dilate(img_th, kernel)
img_e = cv2.erode(img_th,kernel)

img_c = cv2.morphologyEx(img_th,cv2.MORPH_CLOSE, kernel) # img_The image after calculation is entered in c
# MORPH_CLOSE:Performs a process of contracting after expansion
# (MORPH_By specifying OPEN, it is also possible to expand after contraction.)

Feature extraction

――Characteristics are parts with a lot of information. There are the following three features. For feature extraction, you will find an eigenvector with a large eigenvalue in the image.

--Flat: There is no direction in the image. The eigenvectors are not biased and the eigenvalues are small (fewer features)

--Edge: There is one eigenvector with a large eigenvalue in the image (more characteristic than flat)

--Corner: There are two eigenvectors with large eigenvalues in the image (many features)

Feature extractor

Extractor Description
Harris Accuracy drops when the scale changes. Extract features by eigenvalues and eigenvectors
SIFT The feature quantity is 128 dimensions. Not available for commercial use due to patent acquisition
SURF Speed up SIFT. Not available for commercial use due to patent acquisition
ORB The feature amount is binarized. Easy and recommended
KAZE Uses a non-linear filter. Easy and recommended
AKAZE A faster version of KAZE. Easy and recommended

How to show the source code for each extractor.

import cv2
import numpy as np
import copy

img = cv2.imread("Image file path here")
img_g = cv2.imread("Image file path here",0)

# Harris
img_harris = copy.deepcopy(img)
img_dst = cv2.cornerHarris(img_g, 2,3,0.04)
#2 is the block size. How much range should be detected? If you make it big, the amount of extraction will increase

# KAZE
img_kaze = copy.deepcopy(img)
kaze = cv2.KAZE_create() #Create a feature extractor(KAZE)
kp1 = kaze.detect(img,None)
img_kaze = cv2.drawKeypoints(img_kaze, kp1, None) #The image marked in the feature part is img_Enter kaze

# AKAZE
img_kaze = copy.deepcopy(img)
kaze = cv2.AKAZE_create() #Create a feature extractor(AKAZE)
kp1 = kaze.detect(img,None)
img_kaze = cv2.drawKeypoints(img_kaze, kp1, None) #The image marked in the feature part is img_Enter kaze

# ORB
img_orb = copy.deepcopy(img)
orb = cv2.ORB_create() #Create a feature extractor(ORB)
kp2 = orb.detect(img_orb)
img_orb = cv2.drawKeypoints(img_orb, kp2, None) #The image marked in the feature part is img_Enter orb

Recommended Posts

Introduction to OpenCV (python)-(2)
Introduction to image analysis opencv python
Introduction to Python language
Introduction to Python Django (2) Win
Introduction to serial communication [Python]
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
[Introduction to Udemy Python 3 + Application] 58. Lambda
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python] <numpy ndarray> [edit: 2020/02/22]
[Introduction to Udemy Python 3 + Application] 57. Decorator
Introduction to Python Hands On Part 1
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] Let's use pandas
[Introduction to Python] Let's use pandas
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Python] Let's use pandas
An introduction to Python for non-engineers
Introduction to Python Django (2) Mac Edition
[AWS SAM] Introduction to Python version
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Python Tutorial] An Easy Introduction to Python
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Updated to Python 2.7.9
Introduction of Python
Introduction to Tkinter 1: Introduction
Python2.7 + CentOS7 + OpenCV3
Introduction to PyQt
[Linux] Introduction to Linux
Introduction to Scrapy (4)
OpenCV Samples (Python)
Introduction to discord.py (2)
[Note] openCV + python
Introduction to discord.py
"Backport" to python 2
Introduction of Python
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
Introduction to Discrete Event Simulation Using Python # 1
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
A super introduction to Python bit operations
[Introduction to Udemy Python 3 + Application] 10. Numerical values