one , Image gradient

1.1 Sobel operator

Sobel operator
dst=cv2.Sobel(src,ddepth,ds,dy,ksize)
interpretation ：
ddepth: Depth of image (-1)
dx and dy Horizontal and vertical directions respectively
ksize： yes Sobel Size of operator （3×3...）

def cv_show(img,name): cv2.imshow(name,img) cv2.waitKey(0)
cv2.destroyAllWindows() #cv2.CV_64F： More digits , Can be negative #x=1,y=0 ： Horizontal ( Left right structure ) Right minus left
soblex=cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3) #convertScaleAbs: Calculate the absolute value of a negative number
soblex=cv2.convertScaleAbs(soblex) cv_show(soblex,'test')
#cv2.CV_64F： More digits , Can be negative #x=0,y=1 ： Vertical ( Upper and lower structure ) Down minus up
sobley=cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) #convertScaleAbs: Calculate the absolute value of a negative number
sobley=cv2.convertScaleAbs(sobley) cv_show(sobley,'test') # Image fusion display
soblexy=cv2.convertScaleAbs(soblexy) cv_show(soblexy,'test')
Calculate both horizontal and vertical
Direct calculation is not recommended , Not as good as separate calculation and re fusion
soblexy=cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3)
soblexy=cv2.convertScaleAbs(soblexy) cv_show(soblexy,'test')
1.2 Scharr operator

Overall calculation method and Sobel Operator consistency , It's just that the number in the core gets bigger , Finer edge detection

1.3 laplacian operator

Second derivative （ Rate of change of first derivative ）, More sensitive to change , Sensitive to noise points , But the noise point is not the boundary .

1.4 Comparison of three operator difference pairs
# Difference comparison of three operators scharrx=cv2.Scharr(img,cv2.CV_64F,1,0)
scharry=cv2.Scharr(img,cv2.CV_64F,0,1) scharrx=cv2.convertScaleAbs(scharrx)
scharry=cv2.convertScaleAbs(scharry)
laplacian=cv2.Laplacian(img,cv2.CV_64F)
laplacian=cv2.convertScaleAbs(laplacian)
res=np.hstack((soblexy,scharrxy,laplacian)) cv_show(res,'test')
#Scharr Edge ratio of operator discrimination Soble Operator more ,laplacian Operator detects more noise points
Scharr Edge ratio of operator discrimination Soble Operator more ,laplacian Operator detects more noise points

1.5 Canny edge detection

Use Gaussian filter , Smooth image , Filter out noise
Calculate the gradient intensity and direction of each pixel in the image
Apply non maximum suppression , To eliminate the spurious effect caused by edge detection
Applying double threshold detection to identify real and potential edges （ Filter the boundary candidate values again , Find the real boundary ）
Finally, edge detection is completed by suppressing isolated weak edges （ Discard weak edges , Keep main edge ）

1.5.1  Gaussian filter

1.5.2  Gradient and direction （ Adopted Sobel operator ）

1.5.3  Non maximum suppression

Method 1 ：

Method II ：

1.5.4  Double threshold detection

code implementation ：
#minvalues,maxvalues Two thresholds respectively , Higher threshold , More edges filtered , The less you show canny1=cv2.Canny(img,80,150)
canny2=cv2.Canny(img,50,100) res=np.hstack((canny1,canny2)) cv_show(res,'test')
two , image pyramid

2.1 Gauss pyramid

2.1.1 Down sampling （ narrow ）

2.1.2 Up sampling （ enlarge ）

code implementation ：
# Down sampling down=cv2.pyrDown(img) cv_show(down,'down') # Upsampling up=cv2.pyrUp(img)
cv_show(up,'up') print(up.shape)
2.2  laplacian pyramid

original image -（ original image .Down）.Up

code implementation ：
# laplacian pyramid # original image -（ original image .Down）.Up down=cv2.pyrDown(img) down_up=cv2.pyrUp(down)
lapla=img-down_up lapla_inv=img-lapla cv_show(lapla,'lapla')
three , Image outline

cv2.findContours(img,mode,method)
mode: Contour retrieval mode
RETR_EXTERNAL： Detect only the outermost contour
RETR_LIST： Retrieve all profiles , And save it to a linked list
RETR_CCOMP： Retrieve all profiles , And organize them into two layers , The top layer is the outer boundary of each part , The second floor is the boundary of the cavity
RETR_TREE（ Most commonly used , Best use ）： Retrieve all profiles , And reconstruct the entire hierarchy of nested profiles
method： Contour approximation method
CHAIN_APPROX_NONE： with Freeman Output contour by chain code , All other methods of exporting polygons （ Sequence of vertices ）
CHAIN_APPROX_SIMPLE： Compression level , Vertical and oblique parts , that is , Functions keep only their end points

3.1 Sketch outline
# First convert the image to a grayscale image img=cv2.imread('pic.jpg')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Convert to binary graph # Image threshold ： exceed 127 take 255, Otherwise, take 0
ret,thresh=cv2.threshold(gray,127,255,cv2.THRESH_BINARY) cv_show(thresh,'test')
# The three return values are ： Previous version openCV Return values are img, The latest edition is gone , Only two return values #contours： outline (array)
hierarchy： Relationship between profiles ( list )( Hierarchy )
contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print(contours[0])# Print the coordinates of all points of the first profile , Change here 0, by 0--（ Total number of profiles -1）, The coordinates of all points of the corresponding contour can be printed out
print(hierarchy) # Print out the relationship between the corresponding contours
# Sketch outline （ notes ： Copy the original drawing ,drawContours Save image ）( new edition openCV Modified this BUG) new_thresh=thresh.copy()
# Incoming parameters , image , Contour coordinates , Indexes （-1 Coordinates for all profiles ）, Color mode , Line thickness
res=cv2.drawContours(new_thresh,contours,-1,(0,0,255),5) cv_show(res,'res')
3.1 Contour feature

3.1.1 the measure of area
# Contour feature # the measure of area cnt=contours[0] cv2.contourArea(cnt)
3.1.2 Perimeter
# Perimeter (True Indicates closed ) cv2.arcLength(cnt,True)
3.2 Contour approximation
# Contour approximation img=cv2.imread('pic.jpg') gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt=contours[0] draw_img=img.copy()
res=cv2.drawContours(draw_img,[cnt],-1,(0,0,255),5) cv_show(res,'res')
# Approximate value , Generally one tenth of the perimeter epsilon=0.1*cv2.arcLength(cnt,True)
# New contour coordinates after approximation ,cnt： Original contour coordinates ,epslion： Approximate value approx=cv2.approxPolyDP(cnt,epsilon,True)
draw_img=img.copy() res=cv2.drawContours(draw_img,[approx],-1,(0,0,255),5)
cv_show(res,'res')
3.3 Circumscribed rectangle
# Circumscribed rectangle # Calculate and return a four value through the original contour information ：x,y,w,h x,y,w,h=cv2.boundingRect(cnt)
#cv2.rectangle: adopt x,y,w,h Functions for drawing contours
img=cv2.rectangle(draw_img,(x,y),(x+w,y+h),(0,255,0),2) cv_show(img,'test')
# Proportion of original contour area to circumscribed rectangle area=cv2.contourArea(cnt)# Original contour area new_area=w*h
extent=float(area)/new_area
3.4 Circumscribed circle
# Circumscribed circle # Return circumscribed circle parameters from original contour calculation x,y,radius=cv2.minEnclosingCircle(cnt) # Set center coordinates
center=(int(x),int(y)) # Convert into int type radius=int(radius) # draw