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) #高斯滤波 ## 膨胀腐蚀 滤波 去除噪声 # ret ,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # 二值化 thresh = cv2.adaptiveThreshold(aussian,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,15,3) #腐蚀 对的是二值化之后的数据 kernel = np.ones((3,3),np.uint8) # 3x3 的一个核 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) # 图像梯度 sobel 算子 分开计算要比合一起计算要好 #dstxy = cv2.Sobel(opening,cv2.CV_64F,1,1,ksize=3) 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) # scharr 算子 laplacian 算子 cv2.imshow("sobel",dstxy) display("name",Vshow)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)