Opencv 形态学操作 -python代码
Opencv 形态学操作 -python代码
import cv2
import numpy as np
import matplotlib as pl
def display(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Vshow = cv2.imread("img/OIP-C.jpg")
gray = cv2.cvtColor(Vshow,cv2.COLOR_BGR2GRAY)
aussian = cv2.GaussianBlur(gray,(5,5),1)
thresh = cv2.adaptiveThreshold(aussian,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,15,3)
kernel = np.ones((3,3),np.uint8)
erosion = cv2.erode(thresh,kernel,iterations=2)
dige_dilate=cv2.dilate(erosion,kernel,iterations=2)
opening= cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel)
closeing= cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel)
gradient = cv2.morphologyEx(thresh,cv2.MORPH_GRADIENT,kernel)
gradient = cv2.morphologyEx(thresh,cv2.MORPH_TOPHAT,kernel)
gradient = cv2.morphologyEx(thresh,cv2.MORPH_BLACKHAT,kernel)
dstx = cv2.Sobel(opening,cv2.CV_64F,1,0,ksize=3)
dsty = cv2.Sobel(opening,cv2.CV_64F,0,1,ksize=3)
dstx = cv2.convertScaleAbs(dstx)
dsty = cv2.convertScaleAbs(dsty)
dstxy = cv2.addWeighted(dstx,0.5,dsty,0.5,0)
cv2.imshow("sobel",dstxy)
display("name",Vshow)