1、腐蚀运算
import cv2
import numpy as np
from scipy import signal
import math
I=cv2.imread("/home/xiaomingming/profile/xmm.jpg",cv2.IMREAD_GRAYSCALE)
s=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
r=cv2.erode(I,s,iterations=30)
e=I-r
cv2.imshow("I",I)
cv2.imshow("erode",r)
cv2.imshow("edge",e)
cv2.waitKey(0)
cv2.destroyAllWindows()
2、膨胀运算
import cv2
import numpy as np
from scipy import signal
import math
I=cv2.imread("/home/xiaomingming/profile/ss1.jpg",cv2.IMREAD_GRAYSCALE)
#cv2.imshow("I",I)
r=1
MAX_R=20
cv2.namedWindow("dilate",1)
def nothing(*arg):
pass
cv2.createTrackbar("r","dilate",r,MAX_R,nothing)
while True:
r=cv2.getTrackbarPos('r','dilate')
s=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(2*r+1,2*r+1))
d=cv2.dilate(I,s)
cv2.imshow("dilate",d)
ch=cv2.waitKey(5)
if ch==27:
break
cv2.destroyAllWindows()
3、开运算和闭运算
import cv2
import numpy as np
from scipy import signal
import math
I=cv2.imread("/home/xiaomingming/profile/ss1.jpg",cv2.IMREAD_GRAYSCALE)
#cv2.imshow("I",I)
r,i=1,1
MAX_R,MAX_I=20,20
cv2.namedWindow("morphology",1)
def nothing(*arg):
pass
cv2.createTrackbar("r","morphology",r,MAX_R,nothing)
cv2.createTrackbar("i","morphology",i,MAX_I,nothing)
while True:
r=cv2.getTrackbarPos('r','morphology')
i=cv2.getTrackbarPos('i','morphology')
s=cv2.getStructuringElement(cv2.MORPH_RECT,(2*r+1,2*r+1))
d=cv2.morphologyEx(I,cv2.MORPH_CLOSE,s,iterations=i)#cv2.MORPH_OPEN为开运算,cv2.MORPH_CLOSE为闭运算
cv2.imshow("morphology",d)
ch=cv2.waitKey(5)
if ch==27:
break
cv2.destroyAllWindows()