1.Canny edge detection

canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient ]]])
Necessary parameters ：
First parameter image Is the original image to be processed , The image must be a single channel grayscale image ;
Second parameter threshold1 Is threshold 1;
Third parameter threshold1 Is threshold 2;

The larger threshold 2 Used to detect obvious edges in the image , But generally, the effect of detection is not so perfect , Edge detection is intermittent . So use a smaller threshold at this time 1 Used to connect these discontinuous edges ;
Among the optional parameters apertureSize namely Sobel Size of operator ;

L2gradient Parameter is a Boolean value , If true , Use a more accurate L2 Norm calculation （ That is, the square of the reciprocal of the two directions ）, Otherwise use L1 norm （ Add the absolute values of the two directional derivatives directly ）;
The return value of the function is a binary image （ Black and white image ）, Include detected edges .

2. threshold function

threshold(src, thresh, maxval, type)
First parameter src Is the original image to be processed ( array ), The image must be a single channel grayscale image ;
Second parameter thresh Is threshold ;
Third parameter maxval Is the maximum value to be set ;
Fourth parameter type Is the threshold method ：
THRESH_BINARY： Binary thresholding                   0
THRESH_TOZERO_INV： Inverse binary thresholding       1
THRESH_TRUNC： Truncation thresholding                     2
THRESH_TOZERO： Below threshold is set to 0          3
THRESH_TOZERO_INV： Exceeding the threshold is set to 0     4
Function has two return values , First return value ret Is specified thresh, The second return value is the output result after thresholding , for example threshold(img, 127, 255,
0), Is to put the image img Perform binary threshold processing , Gray value less than thresh Namely 127 Set as 0, greater than 127 Set as maxval Namely 255.

3. Contour detection function

findContours(imgt, mode, method)
First parameter imgt Is the original image to be processed ( array ), The image must be binary ;
Second parameter mode Is the contour detection mode ：
cv2.RETR_EXTERNAL： Detect only outer contour
cv2.RETR_LIST： The detected contour does not establish a hierarchical relationship
cv2.RETR_CCOMP： Create a profile of two levels , The upper layer is the outer boundary , The inner layer is the boundary information of the inner hole . If there is another connected object in the inner hole , The boundary of this object is also at the top
cv2.RETR_TREE： Establish the outline of a hierarchical tree structure
Third parameter method Is the contour approximation method ：
cv2.CHAIN_APPROX_NONE： Store all contour points , The pixel position difference of two adjacent points shall not exceed 1, Namely max（abs（x1-x2）,abs（y2-y1））==1
cv2.CHAIN_APPROX_SIMPLE： Compression horizontal direction , vertical direction , Diagonal elements , Only the end coordinates of this direction are retained , For example, a rectangular outline only needs 4 Points to save contour information
cv2.CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS： use teh-Chinl chain approximation algorithm
This function has two return values , The first is the outline itself contours , There is also an attribute corresponding to each contour hierarchy（ Hierarchical relationship ）.

4. Contour drawing function

drawContours(image, contours,contouridx,scalar&color,thickness)
First parameter image Is the original image to be processed ( array ), Output results directly update the original image image;
Second parameter contours Is the contour itself ;
Third parameter contouridx Specify which profile in the sketch profile , If it is -1, Then draw all the contours in it ;
The fourth parameter is contour color （B,G,R）;
Fifth parameter thickness Indicates the width of the contour line , If it is -1（cv2.FILLED）, Fill mode .

5. The following codes are split screen real-time display ： Original drawing , Grayscale image ,canny Edge detection diagram , Contour detection diagram . When displaying in one window, the image needs to be three channels , So use COLOR_BGR2GRAY Change grayscale image to BGR Color map .
import cv2 import numpy as np # Turn on the camera ( Only one camera is numbered 0, If any 2 The order is 0,1) camera =
cv2.VideoCapture(0) # create a window cv2.namedWindow('Webcam', cv2.WINDOW_NORMAL)
# Continuous interval 1ms Wait for the key , If there is a key, jump out of the cycle while (cv2.waitKey(1)==-1): # Read and process camera data , Note that deep copy is required (deep
copy) success, frame =camera.read() original=frame.copy()# Original drawing
gray=cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)# Convert to grayscale image canny=cv2.Canny(gray,
200, 300) #Canny edge detection ret, imgt= cv2.threshold(gray, 127, 255, 0) # Threshold processing
contours, hierarchy = cv2.findContours(imgt, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE) # Contour detection cont = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# take gray Convert to color image cont cv2.drawContours(cont, contours, -1, (0,0,255))# Draw outline # Split screen display h1
= np.hstack((original, cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)))# first line h2 =
np.hstack((cv2.cvtColor(canny,cv2.COLOR_GRAY2BGR), cont))# Second line vdisp =
np.vstack((h1, h2))# Longitudinal cv2.imshow('Webcam',vdisp) # Disconnect the camera camera.release()
# Release all windows cv2.destroyAllWindows()

Technology
Daily Recommendation