OpenCV: Image Morphological Operations

Original image:

#corrosion operation erode

kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations=5)
#通过更改卷积核大小和迭代次数都会影响腐蚀区域大小
cv2.imshow('erosion',erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(erosion)
plt.show()

Effect:

erosion1 = cv2.erode(img,kernel,iterations=1)
erosion2 = cv2.erode(img,kernel,iterations=20)
erosion3 = cv2.erode(img,kernel,iterations=30)

res = np.hstack((erosion1,erosion2,erosion3))

cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(res)
plt.show()

Effect:

#Expansion operation dilate

kernel = np.ones((3,3),np.uint8)
dilate = cv2.dilate(img_,kernel,iterations=5)
#通过更改卷积核大小和迭代次数都会影响膨胀区域大小
cv2.imshow('dilate',dilate)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(dilate)
plt.show()

Effect:

#Open operation MPRPH_OPEN and close operation MPRPH_CLOSE

#开:先腐蚀,再膨胀 =原始输入
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(img_,cv2.MORPH_OPEN,kernel)
#闭:先膨胀,再腐蚀 
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(img_,cv2.MORPH_CLOSE,kernel)

Original image: open operation: close operation:

# Gradient operation GRADIENT

# 梯度 = 膨胀-腐蚀
kernel = np.ones((3,3),np.uint8)
gradient = cv2.morphologyEx(img_,cv2.MORPH_GRADIENT,kernel)

cv2.imshow('gradient',gradient)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(gradient)
plt.show()

 

#Topper Hat MORPH_TOPHAT and Black Hat MORPH_TOPHAT

# 礼帽:原始输入-开运算结果 MORPH_TOPHAT  只剩刺
img= cv2.imread('dot1.png')
tophat =cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)

cv2.imshow('tphat',tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()

res = np.hstack((img,tophat))
plt.imshow(res)
plt.show()

# 礼帽:原始输入-开运算结果 MORPH_TOPHAT  只剩刺
img= cv2.imread('dot1.png')
tophat =cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)

cv2.imshow('tphat',tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()

res = np.hstack((img,tophat))
plt.imshow(res)
plt.show()

#Image gradient--Sobel operator

img = cv2.imread('cat1.jpg',cv2.IMREAD_GRAYSCALE)
img_=cv2.resize(img,(0,0),fx=0.2,fy=0.2)
cv2.imshow('img',img_)
cv2.waitKey(0)
cv2.destroyAllWindows()

dst = cv2.Sobel(src,ddepth,dx,dy,ksize)

    ddepth: The depth of the image is generally -1
    dx and dy respectively represent the horizontal and vertical directions Gx or Gy
    ksize is the size of the Sobel operator convolution kernel size
    

 White to black is an integer, black to white is a complex number, all negative numbers will be truncated to 0, so take the absolute value

sobely = cv2.Sobel(img_,cv2.CV_64F,0,1,ksize=3)
sobely = cv2.convertScaleAbs(sobely)
#算负数的绝对值

cv_show(sobely,'sobely')
plt.imshow(sobely)
plt.show()

#sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0) 要求是dtype要一致,下面就强制转换了这两个的dtype
sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0, dtype = cv2.CV_32F)
cv_show(sobelxy,'sobelxy')

plt.imshow(sobelxy)
plt.show()

#Scharr operator (second-order operator, larger gradient)

 # The laplacian operator is sensitive to noise

#
scharrx = cv2.Scharr(img,cv2.CV_64F,1,0)
scharry = cv2.Scharr(img,cv2.CV_64F,1,0)
scharrx = cv2.convertScaleAbs(scharrx)
scharry = cv2.convertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx,0.5,scharry,0.5,0)

laplacian =cv2.Laplacian(img,cv2.CV_64F)
laplacian =cv2.convertScaleAbs(laplacian)

res = np.hstack((scharrxy,laplacian))
cv_show(res,'res')

titles = ['sobelxy','scharrxy','laplacian']
images = [sobelxy,scharrxy,laplacian]

for i in range(3):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

Guess you like

Origin blog.csdn.net/Crabfishhhhh/article/details/127693534