编程语言
首页 > 编程语言> > OpenCV__Python 腐蚀和膨胀_教程25

OpenCV__Python 腐蚀和膨胀_教程25

作者:互联网

1、腐蚀,kenerl的参数注意自己要选择下

 

#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys


#erode
def erode_test(img):
    #(1)灰度化,阈值分割,黑色里面找白色!!!
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)  
    print("threshold value: %s"%(ret)) #分割的阈值
    cv.namedWindow("binary_image",cv.WINDOW_NORMAL)
    cv.imshow("binary_image",binary)

    #(2)选择腐蚀的核
    #kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
    kernel = cv.getStructuringElement(cv.MORPH_RECT,(11,11))
    #kernel = cv.getStructuringElement(cv.MORPH_CROSS,(3,3))
    #(3)腐蚀操作
    dst = cv.erode(binary,kernel)                     
    cv.namedWindow("erode_test",cv.WINDOW_NORMAL)   
    cv.imshow("erode_test",dst)
    

def img_test():
    img = cv.imread('E:/chenopencvblogimg/erode.jpg')
    #判断是否读取成功
    if img is None:
        print("Could not read the image,may be path error")
        return
    cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
    cv.imshow("origin Pic",img)
    erode_test(img)
    
    #让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
    cv.waitKey(0)
    #销毁窗口
    cv.destroyAllWindows()

if __name__ == '__main__':
    sys.exit(img_test() or 0)

2、膨胀,kenerl的参数注意自己要选择下 

#引入opencv模块
import cv2 as cv
#引入numpy模块
import numpy as np
#引入sys模块
import sys


#erode
def erode_test(img):
    #(1)灰度化,阈值分割,黑色里面找白色!!!
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)  
    print("threshold value: %s"%(ret)) #分割的阈值
    cv.namedWindow("binary_image",cv.WINDOW_NORMAL)
    cv.imshow("binary_image",binary)

    #(2)选择腐蚀的核
    #kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
    kernel = cv.getStructuringElement(cv.MORPH_RECT,(11,11))
    #kernel = cv.getStructuringElement(cv.MORPH_CROSS,(3,3))
    #(3)腐蚀操作
    dst = cv.erode(binary,kernel)                     
    cv.namedWindow("erode_test",cv.WINDOW_NORMAL)   
    cv.imshow("erode_test",dst)


def dilate_test(img): 
    #(1)灰度化,阈值分割,黑色里面找白色!!!
    gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
    ret,binary = cv.threshold(gray,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)  
    print("threshold value: %s"%(ret)) #分割的阈值
    cv.namedWindow("binary_image",cv.WINDOW_NORMAL)
    cv.imshow("binary_image",binary)

    #(2)选择腐蚀的核
    #kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
    kernel = cv.getStructuringElement(cv.MORPH_RECT,(11,11))
    #kernel = cv.getStructuringElement(cv.MORPH_CROSS,(3,3))
    #(3)腐蚀操作
    dst = cv.dilate(binary,kernel)                     
    cv.namedWindow("dilate_test",cv.WINDOW_NORMAL)   
    cv.imshow("dilate_test",dst)


def img_test():
    img = cv.imread('E:/chenopencvblogimg/dilate.jpg')
    #img = cv.imread('E:/chenopencvblogimg/erode.jpg')
    #判断是否读取成功
    if img is None:
        print("Could not read the image,may be path error")
        return
    cv.namedWindow("origin Pic",cv.WINDOW_NORMAL)
    cv.imshow("origin Pic",img)
    #erode_test(img)
    dilate_test(img)
    
    #让显示等待键盘输入维持在那里,否则程序跑完就闪退啦!
    cv.waitKey(0)
    #销毁窗口
    cv.destroyAllWindows()

if __name__ == '__main__':
    sys.exit(img_test() or 0)

标签:__,25,erode,img,binary,Python,test,kernel,cv
来源: https://blog.csdn.net/chencaw/article/details/110529988