标签:imp sub import 膨胀 demo dem ips gray src
import cv2 as cv import numpy as np def top_hat_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) cv.imshow("binary",binary) #这里的二值化图像就可以看作是原图像(注意:基于腐蚀膨胀是可以直接对彩色图像操作的)) kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE,(5,5)) dst = cv.morphologyEx(binary,cv.MORPH_OPEN,kernel) #查看开操作图像 cv.imshow("open_demo",dst) dst = cv.morphologyEx(binary,cv.MORPH_TOPHAT,kernel) #查看顶帽图像 cv.imshow("top_hat_demo",dst) src = cv.imread("./cir.png") #读取图片 cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) #创建GUI窗口,形式为自适应 cv.imshow("input image",src) #通过名字将图像和窗口联系 top_hat_demo(src) cv.waitKey(0) #等待用户操作,里面等待参数是毫秒,我们填写0,代表是永远,等待用户操作 cv.destroyAllWindows() #销毁所有窗口
def black_hat_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) cv.imshow("binary",binary) kernel = cv.getStructuringElement(cv.MORPH_RECT,(15,15)) dst = cv.morphologyEx(binary,cv.MORPH_CLOSE,kernel) cv.imshow("close_demo",dst) dst = cv.morphologyEx(binary,cv.MORPH_BLACKHAT,kernel) cv.imshow("black_hat_demo",dst)
def graditent_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) cv.imshow("binary",binary) kernel = cv.getStructuringElement(cv.MORPH_RECT,(15,15)) dst = cv.dilate(binary, kernel) cv.imshow("dilate_demo", dst) dst = cv.erode(binary, kernel) cv.imshow("erode_demo", dst) dst = cv.morphologyEx(binary,cv.MORPH_GRADIENT,kernel) cv.imshow("graditent_demo",dst)
def graditent_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) cv.imshow("binary",binary) kernel = cv.getStructuringElement(cv.MORPH_RECT,(15,15)) erode_dst = cv.erode(binary, kernel) cv.imshow("erode_demo", erode_dst) dst2 = cv.subtract(dilate_dst,binary) cv.imshow("inner graditent",dst1)
def graditent_demo(image): gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU) cv.imshow("binary",binary) kernel = cv.getStructuringElement(cv.MORPH_RECT,(15,15)) dilate_dst = cv.dilate(binary, kernel) cv.imshow("dilate_demo", dilate_dst) dst2 = cv.subtract(dilate_dst,binary) cv.imshow("outer graditent",dst2)
标签:imp sub import 膨胀 demo dem ips gray src
原文地址:https://www.cnblogs.com/ssyfj/p/9277891.html