OpenCV algorithm analysis (1)

OpenCV algorithm analysis

  • 1 Image Enhancement
    • 1.1 Meaning
    • 1.2 Method
      • 1.2.1 Histogram equalization
      • 1.2.2 gamma transformation
  • 2 Noise Cancellation
    • 2.1 Meaning
    • 2.2 Method
      • 2.2.1 Gaussian filter
      • 2.2.2 Mean filtering
      • 2.2.3 Median filtering
  • 3 Edge detection
    • 3.1 canny
  • 4 HOG Feature Extraction
    • 4.1 Meaning
    • 4.2 Process
    • 4.3 Case
  • 6 two games
    • 6.1 Integration of three functions
    • 6.2 Object Detection
    • 6.3 Detailed explanation of yolov5 code

1 image enhancement

1.1 Meaning

Highlight image details and make images clearer.

1.2 Method

Histogram equalization

1.2.1 Histogram equalization

Histogram equalization

import cv2
import numpy as np
import matplotlib.pyplot as plt


def histequal(img_gray):
   # 1 Calculate the frequency, cumulative frequency and normalization of each pixel
   h,w = img_gray.shape
   cum_freq = np.array([(img_gray==i).sum() for i in range(255)]).cumsum()
   cum_freq = (cum_freq/cum_freq[-1]*255).astype(np.uint8)
   tmp = img_gray. copy()
   for i in range(255):
       tmp[img_gray == i] = cum_freq[i]
   return tmp


img = cv2.imread('opencv/features/cat0.jpg', 0)
out = histequal(img)
plt. subplot(121)
plt. title('org')
plt.imshow(img,cmap='gray')
plt. subplot(122)
plt. title('histequal')
plt.imshow(out,cmap='gray')
plt. show()

1.2.2 gamma transformation

import cv2
import numpy as np
import matplotlib.pyplot as plt

def gamma(img,c=2,r=2):
    return np.array(c*img**r,dtype=np.uint8).clip(0,255)


img = cv2.imread('/Users/liushuang/Desktop/LearnGit/Project combat/fry detection/yolov5-master/opencv/features/cat.jpg')
out = gamma(img)
plt. subplot(121)
plt. title('org')
plt.imshow(img,cmap='gray')
plt. subplot(122)
plt. title('gamma')
plt.imshow(out,cmap='gray')
plt. show()

2 Denoising

2.1 Meaning

remove noise from image

2.2 Method

Spatial filtering (mean filtering, Gaussian filtering, median filtering, bilateral filtering), transform filtering (Fourier, wavelet transform), morphology, etc.

2.2.1 Gaussian filter

Gaussian filter
Gaussian filtering is suitable for dealing with normally distributed noise. The characteristic of the filter is that the weights are normally distributed. The larger the variance, the more obvious the filtering effect; otherwise, the worse the effect.

import cv2
import numpy as np
import matplotlib.pyplot as plt


def guassian_filter(img, k_size=3, sigma=2):
    # get channel
    if len(img.shape) == 3:
        h, w, c = img. shape
    else:
        img = np. expand_dims(img, axis=-1)
        h, w, c = img. shape

    #img padiing
    pad = (k_size - 1) // 2
    out_put = np.zeros((h + pad * 2, w + pad * 2,3), dtype=np.float32)
    out_put[pad:pad + h, pad:pad + w] = img.copy().astype(np.float32)

    # generate kernel
    k = np.zeros((k_size, k_size), dtype = np.float32)

    for i in range(-pad, k_size - pad):
        for j in range(-pad, k_size - pad):
            k[j + pad, i + pad] = np. exp(-(i ** 2 + j ** 2) / (2 * sigma ** 2))
    k /= (2 * np.pi * sigma ** 2)
    k /= k.sum()
    tmp = out_put. copy()

    # Gaussian filter
    for i in range(h):
        for j in range(w):
            for cc in range(c):
                out_put[pad + i, pad + j, cc] = np. sum(k * tmp[i:i + k_size, j:j + k_size, cc])
    out_put = np. clip(out_put, 0, 255)

    return out_put[pad:pad + h, pad:pad + w].astype(np.uint8)


#
img = cv2.imread('opencv/features/dogGauss.jpeg')
out = guassian_filter(img, k_size=3, sigma=2)
plt. subplot(121)
plt. title('org')
plt.imshow( img[...,::-1])
plt. subplot(122)
plt. title('out')
plt.imshow( out[...,::-1])
plt. show()

    
# sigma = 1, Gaussian kernel
'''
array([[0.07511362, 0.12384141, 0.07511362],
       [0.12384141, 0.20417996, 0.12384141],
       [0.07511362, 0.12384141, 0.07511362]], dtype=float32)

# sigma = 3
array([[0.10699731, 0.11310981, 0.10699731],
       [0.11310981, 0.11957153, 0.11310981],
       [0.10699731, 0.11310981, 0.10699731]], dtype=float32)
'''

2.2.2 Mean filter

### 2.2.2 mean + filter
import cv2
import numpy as np
import matplotlib.pyplot as plt

def mean_filter(img, k_size=3):
    #shape
    if len(img.shape) != 3:
        img = img[..., None]
    h, w, c = img. shape
    # 1 kernel
    k = np.ones((k_size, k_size), np.float32) / (k_size * k_size)
    # 2 out_Put
    pad = (k_size - 1) // 2
    out_put = np.zeros((h + 2*pad, w + 2*pad, c), np.float32)
    out_put[pad:pad + h, pad:pad + w] = img.copy().astype(np.float32)
    # 3 filter
    tmp = out_put. copy()
    for i in range(h):
        for j in range(w):
            for cc in range(c):
                out_put[pad + i:, pad + j, cc] = (tmp[i:i + k_size, j:j + k_size, cc] * k).sum()

    out_put = np. clip(out_put, 0, 255)

    return out_put[pad:pad + h, pad:pad + w].astype(np.uint8)


# filter

img = cv2.imread('opencv/features/dogGauss.jpeg')
out = mean_filter(img, k_size=3)
plt. subplot(121)
plt. title('org')
plt.imshow(img[..., ::-1])
plt. subplot(122)
plt. title('out_mean_filter')
plt.imshow(out[..., ::-1])
plt. show()

2.2.3 Median filter

Take the median of the window as the value of the center point. If the effect is not good, you can increase the size of the kernel, or cycle several times.

import cv2
import numpy as np
import matplotlib.pyplot as plt


def median_filter(img, k_size=10):
    #shape
    if len(img.shape) != 3:
        img = img[..., None]
    h, w, c = img. shape

    # 2 out_Put
    pad = (k_size - 1) // 2
    out_put = np. ones((h + 2 * pad, w + 2 * pad, c), np. float32) * (-1)
    out_put[pad:pad + h, pad:pad + w] = img.copy().astype(np.float32)
    # 3 filter
    tmp = out_put. copy()
    for i in range(h):
        for j in range(w):
            for cc in range(c):
                t = tmp[i:i + k_size, j:j + k_size, cc]
                out_put[pad + i:, pad + j, cc] = np.median(t[t > 0])

    return out_put[pad:pad + h, pad:pad + w].astype(np.uint8)


# filter

img = cv2.imread('opencv/features/letteropen.png')
out = median_filter(img, k_size=20)
plt. subplot(121)
plt. title('org')
plt.imshow(img[..., ::-1])
plt. subplot(122)
plt. title('median_filter')
plt.imshow(out[..., ::-1])
plt. show()

3 Edge detection

3.1 canny

canny edge detection process

  1. smoothing
  2. gradient detection
  3. non-maximum suppression
  4. Hysteresis Thresholding
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def canny(img):
    # 1. Smooth
    img_blur = cv.GaussianBlur(img, (5, 5), 2)

    # 2. Gradient
    gradx = cv.Sobel(img_blur, cv.CV_64F, 1, 0) # x
    grady = cv. Sobel(img_blur, cv. CV_64F, 0, 1) # y
    R = np.abs(gradx) + np.abs(grady)
    T = np.arctan(grady / (gradx + 1e-3))

    # 3. Non-maximum suppression, refine the edge
    h, w = R.shape
    img_thin = np.zeros_like(R)

    for i in range(1, h - 1):
        for j in range(1, w - 1):
            theta = T[i, j]
            if -np.pi / 8 <= theta < np.pi / 8: # (-22,22)
                if R[i, j] == max([R[i, j], R[i, j - 1], R[i, j + 1]]): # up and down
                    img_thin[i, j] = R[i, j]
            elif -3 * np.pi / 8 <= theta < -np.pi / 8: # (-66,-22)
                if R[i, j] == max([R[i, j], R[i - 1, j + 1], R[i + 1, j - 1]]): # slash
                    img_thin[i, j] = R[i, j]
            elif np.pi / 8 <= theta < 3 * np.pi / 8: # (22,66)
                if R[i, j] == max([R[i, j], R[i - 1, j - 1], R[i + 1, j + 1]]): # backslash
                    img_thin[i, j] = R[i, j]
            else: # (66,110)
                if R[i, j] == max([R[i, j], R[i - 1, j], R[i + 1, j]]): # around
                    img_thin[i, j] = R[i, j]

    # 4 Dual Threshold Suppression
    th1 = 5
    th2 = 30
    maxv = 255
    img_edge = np.zeros_like(img_thin)
    h, w = img_thin.shape
    for i in range(1, h - 1):
        for j in range(1, w - 1):
            if img_thin[i, j] >= th2:
                img_edge[i, j] = maxv
            elif img_thin[i, j] > th1:
                around = img_thin[i - 1:i + 2, j - 1:j + 2]
                if around.max() >= th2:
                    img_edge[i, j] = maxv
    return img_edge

img = cv2.imread('opencv/features/letteropen.png', 0)
out = canny(img)
plt. subplot(121)
plt. title('org')
plt.imshow(img,cmap='gray')
plt. subplot(122)
plt. title('canny')
plt.imshow(out,cmap='gray')
plt. show()

4 HOG feature extraction

4.1 Meaning

By extracting the features of the gradient histogram in the local area of the image, the features of the entire image are obtained. HOG remains invariant to both geometric and optical deformations. Through local normalization, the change of details is ignored, and the common feature is extracted.

4.2 Process

img –> cells,blocks –> Calculate the gradient histogram of each cell

4.3 Case

6 two games

6.1 Integration of three functions

6.2 Target detection

6.3 Detailed explanation of yolov5 code