Study reference
The foregoing link
- 【python】OpenCV—RGB, Rectangle, Circle, SS(1)
- 【python】OpenCV—Blur, Threshold, Gradient, Morphology(2)
- 【python】OpenCV—Edge, Corner, Face Detection(3)
Article Directory
0 import
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
1 Edge Detection(Canny)
Dramatic change in the image is detected or discontinuous nature detecting edge pixels. These pixel is the line segment connecting the edges. In fact, [python] OpenCV-Blur, Threshold, Gradient, Morphology (2) we have described a basic edge detection techniques: the use of Sobel operator and Laplacian filtering gradient. By calculating image pixel values in a given direction derivative, i.e. gradient filter can be drawn so that the edge of the image edge detection.
Another detection algorithm is a Canny edge detection technique. And is one of the most popular edge detection technology, is divided into the following four steps to achieve:
- Noise Reduction : Gaussian blur
- Analyzing gradient direction and the gradient : sobel
- Non-maximal suppression : obtained using a gradient detecting pixels wherein each pixel and the surrounding pixels is not confirmed that the local maximum points in the local pixel. If it is not a local maximum value, the pixel value is set to zero point (complete deletion, black)
- Hysteresis threshold process : results of the third step then given two different thresholds, we can obtain three thresholding interval. Thus, if the pixel value of the point is greater than the "threshold value larger" two thresholds were determined as the edge point. In contrast, if it is less than the "threshold value smaller" two threshold parameters set in were identified as non-edge points, i.e. are discarded. Further, if the pixel value of a point located between the two threshold parameter is whether the data with which to decide whether to discard the connection between the "edge point confirmation", following the connection is not dropped principles.
This drawing is wrong feeling, A, B, C three points, the maximum value of B if the gradient (Edges), would be 255, and the AC suppressed to 0, there is the second diagram B assigned to the 0! (Do not know has always been my understanding is wrong)
coding it, the core function cv2.Canny
img = cv2.imread('C://Users/13663//Desktop/4.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Canny detection without blurring
edges = cv2.Canny(image=img, threshold1=127, threshold2=127)
plt.figure(figsize = (20, 20))
plt.subplot(1, 2, 1)
plt.title("original image",fontsize=24,color='white')
plt.imshow(img)
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title("canny",fontsize=24,color='white')
plt.imshow(edges,cmap='gray')
plt.axis('off')
Here only one set threshold value
Try to set two thresholds results
# Set the lower and upper threshold
med_val = np.median(img)
lower = int(max(0, .7*med_val))
upper = int(min(255, 1.3*med_val))
print(med_val,lower,upper)
output
97.0 67 126
Then call it cv2.Canny
, with a mean of three different filter Gaussian filter kernel before! Large threshold, and without distinction +100 100! A total of six combinations!
# Blurring with ksize = 3
img_k3 = cv2.blur(img, ksize = (3, 3))
# Canny detection with different thresholds
edges_k3 = cv2.Canny(img_k3, threshold1 = lower, threshold2 = upper)
edges_k3_2 = cv2.Canny(img_k3, lower, upper+100)
# Blurring with ksize = 5
img_k5 = cv2.blur(img, ksize = (5, 5))
# Canny detection with different thresholds
edges_k5 = cv2.Canny(img_k5, lower, upper)
edges_k5_2 = cv2.Canny(img_k5, lower, upper+100)
# Blurring with ksize = 7
img_k7 = cv2.blur(img, ksize = (7, 7))
# Canny detection with different thresholds
edges_k7 = cv2.Canny(img_k7, threshold1 = lower, threshold2 = upper)
edges_k7_2 = cv2.Canny(img_k7, lower, upper+100)
# Plot the images
names = ['k3, l, u', 'k5, l, u', 'k7, l, u', 'k3, l, u+100', 'k5, l, u+100', 'k7, l, u+100']
images = [edges_k3, edges_k5, edges_k7, edges_k3_2, edges_k5_2, edges_k7_2]
plt.figure(figsize = (20, 14))
for i in range(6):
plt.subplot(2, 3, i+1)
plt.title(names[i],fontsize=24,color='w')
plt.imshow(images[i],cmap='gray')
plt.axis('off')
plt.show()
2 Corner Detection
Harris
Corner detection and Shi&Tomasi
corner detection
Both algorithm works as follows. First, the intensity of the detected pixel values are the respective directions of the points vary widely. Then constructs a matrix, to extract feature values. To determine whether it is a corner score these characteristic values. As shown in the following mathematical expression.
Kernel
cv2.cornerHarris()
cv2.goodFeaturesToTrack()
2.1 Harris Corner Detection
The core function cv2.cornerHarris()
parameters are as follows:
- img - float32 data type of the input image.
- blockSize - corner detection field magnitude to be considered.
- ksize - Sobel find use in guiding the window size
- k - Harris corner detection of free parameters in the equation, the value for the parameter [0,04,0.06] k Harris detector free parameter..
#img = cv2.imread('C://Users/13663//Desktop/1.jpg')
img = cv2.imread('C://Users/13663//Desktop/7.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Apply Harris corner detection
dst = cv2.cornerHarris(img_gray, blockSize = 8, ksize = 3, k = .04)
# Spot the detected corners
img_2 = img.copy()
img_2[dst>0.01*dst.max()]=[255,0,0]
# Plot the image
plt.figure(figsize = (20, 20))
plt.subplot(1, 2, 1); plt.imshow(img)
plt.axis('off')
plt.subplot(1, 2, 2); plt.imshow(img_2)
plt.axis('off')
2.2 Shi&Tomasi Corner Detection
Kernel cv2.goodFeaturesToTrack()
explained with reference parameters may Good Features to track the feature point detection principle opencv (python) implemented
# Apply Shi-Tomasi corner detection
corners = cv2.goodFeaturesToTrack(img_gray, maxCorners = 100,
qualityLevel = 0.01,
minDistance = 10)
corners = np.int0(corners)
# Spot the detected corners
img_2 = img.copy()
for i in corners:
x,y = i.ravel()
cv2.circle(img_2, center = (x, y),
radius = 5, color = 255, thickness = -1)
# Plot the image
plt.figure(figsize = (20, 20))
plt.subplot(1, 2, 1); plt.imshow(img)
plt.axis('off')
plt.subplot(1, 2, 2); plt.imshow(img_2)
plt.axis('off')
3 Face Detection
Based Haar
feature classifier cascade is one of face detection OpenCV model commonly used. It has been pre-trained on thousands of sub-images. Understand four key points of the algorithm are:
1) Haar feature extraction
in the detection process, confirmed by the convolution operations on a sliding window filter and wherein these characteristics are not we need. Party as follows:
So, we specifically, how to determine whether a given region contains features need it? As shown in the image above. Using a convolution kernel specific (upper half region is dark lower half area is bright) to give the average of pixel values in each region, and subtracting the difference between the two. If the result is above the threshold (for example 0.5), the result can be obtained, which is what we are detected features. This process is repeated for each core, while sliding on the image window.
2) integral image
Although this calculation is not complicated, but if you repeat this process in a positive image is still very large amount of calculation. This is the main problem to be solved by the integral image. Integral image is an image representation, which is to increase the speed and efficiency features derived by estimation .
Each point is the original point to the integral image in all pixels of the upper left corner of the rectangle and enclosed!
Orange area of the region is calculated, it calculates two times of subtraction and one addition, the reference integral image (Integral Image) integral with the histogram (Integral Histogram)
- FIG Points: 264--139--126 + 73 = 72
- Original: 10 + 20 + 17 + 25 = 72
3) Adaboost cascade classifier and
so integral image can help us solve the problem of excessive computing to some extent. But not enough, there is still room for optimization computation. When the detection window is positioned on a white background or no target human face detection is performed it will be unnecessary cost calculation. Then you can use and the cascade Adaboost classifier, the amount of calculation in order to achieve further optimization.
The figure shows the various stages of a progressive cascade classifier configured, and class haar feature (feature class haar (Haar-like features) is a digital image feature for target detection) sort. The basic features are identified at an early stage, the latter only identify complex features it hopes to become the target feature. At each stage, Adaboost models will be integrated train weak classifiers. If the child window or sub-components in the previous stage is classified as "not face area", it will be denied access to the next step . By the above operation, taking into account only the screening stage out of the features, in order to achieve higher speeds.
Let's look at combat
Original
first interception of about roi
cap_mavl = cv2.imread('C://Users/13663//Desktop/12.png')
# Find the region of interest
roi = cap_mavl[0:235,265:475]
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
plt.imshow(roi)
plt.axis("off")
# Create the face detecting function
def detect_face(img):
img_2 = img.copy()
# Load Cascade filter
face_cascade = cv2.CascadeClassifier('D://software/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
face_rects = face_cascade.detectMultiScale(img_2,
scaleFactor = 1.1,
minNeighbors = 10)
for (x, y, w, h) in face_rects:
cv2.rectangle(img_2, (x, y), (x+w, y+h), (255, 0, 0), 3)
return img_2
# Detect the face
roi_detected = detect_face(roi)
plt.imshow(roi_detected)
plt.axis('off')
detectMultiScale
The parameters are as follows:
- image: image to be detected, typically to accelerate the speed detecting grayscale image;
- scaleFactor: represents the two successive scans back and forth, the scale factor of the search window. The default is 1.1, that is, each search window in order to expand 10%;
- minNeighbors: represents the minimum number of adjacent rectangles constituting the detection target (the default is 3). If the number of small rectangles composition and less than the detection target min_neighbors - 1 will be excluded. If min_neighbors is 0, the function returns no operation subject all candidate rectangle, this set value is generally used in combination with a program on the user-defined test results;
Note that cv2.CascadeClassifier
the path of the file where opencv own folder, as follows:
You can try another detection, such as detection of the eye, modify the above code
face_cascade = cv2.CascadeClassifier('D://software/opencv/sources/data/haarcascades/haarcascade_eye.xml')
as follows:
Try following detection of the entire map, not roi
# Create the face detecting function
def detect_face(img):
img_2 = img.copy()
# Load Cascade filter
face_cascade = cv2.CascadeClassifier('D://software/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
face_rects = face_cascade.detectMultiScale(img_2,
scaleFactor = 1.1,
minNeighbors = 3)
for (x, y, w, h) in face_rects:
cv2.rectangle(img_2, (x, y), (x+w, y+h), (255, 0, 0), 3)
return img_2
# Load the image file and convert the color mode
avengers = cv2.imread('C://Users/13663//Desktop/12.png')
avengers = cv2.cvtColor(avengers, cv2.COLOR_BGR2RGB)
# Detect the face and plot the result
detected_avengers = detect_face(avengers)
plt.imshow(detected_avengers)
plt.axis('off')