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
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
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
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
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
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
Updating your own pixel value using surrounding information is called convolution.
Prepare a filter
Perform "(pixel value) x (filter)" around the pixel of interest and add (this is called convolution).
Place on all pixels and convolve
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)
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
--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)
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.)
――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)
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