Roberts、Prewitt、Sobel、 Laplacian边缘检测

Roberts、Prewitt、Sobel、 Laplacian边缘检测

import cv2

# Roberts边缘检测
def roberts(grayImage):                                
    kernelx = np.array([[-1,0],[0,1]], dtype=int)      
    kernely = np.array([[0,-1],[1,0]], dtype=int)      
    x = cv2.filter2D(grayImage, cv2.CV_16S, kernelx)   
    y = cv2.filter2D(grayImage, cv2.CV_16S, kernely)                                            
    absX = cv2.convertScaleAbs(x)                      
    absY = cv2.convertScaleAbs(y)                      
    Roberts = cv2.addWeighted(absX,0.5,absY,0.5,0)     
    return Roberts             
                            
# Prewitt边缘检测
def prewitt(grayImage):                                        
    kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int)
    kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int)
    x = cv2.filter2D(grayImage, cv2.CV_16S, kernelx)           
    y = cv2.filter2D(grayImage, cv2.CV_16S, kernely)                                                             
    absX = cv2.convertScaleAbs(x)                              
    absY = cv2.convertScaleAbs(y)                              
    Prewitt = cv2.addWeighted(absX,0.3,absY,0.7,0)             
    return Prewitt  
                                               
# Sobel边缘检测
def sobel(grayImage):
	x = cv2.Sobel(grayImage, cv2.CV_16S, 1, 0) #对x求一阶导
	y = cv2.Sobel(grayImage, cv2.CV_16S, 0, 1) #对y求一阶导
	absX = cv2.convertScaleAbs(x)      
	absY = cv2.convertScaleAbs(y)    
	Sobel = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
	return Sobel
	
#  Laplacian边缘检测
def laplacian(grayImage):
	dst = cv2.Laplacian(grayImage, cv2.CV_16S, ksize = 3)
	Laplacian = cv2.convertScaleAbs(dst) 
	return Laplacian
	
img = cv2.imread('demo.jpg')
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)	

你可能感兴趣的:(python,baseknowledge,python,opencv,计算机视觉)