1. 程式人生 > >opencv(10)-開閉操作+其他形態學操作

opencv(10)-開閉操作+其他形態學操作

開閉操作

開操作-線的提取:對結果元素cv2.getStructuringElement的ksize進行操作,就可以提取

                  1.ksize = (15,1)

                 2.ksize=(1,15)

                3.ksize=(3,3),開操作

開操作:

程式碼:

import cv2
import numpy as np

# 開操作
def open_demo(image):
    # 轉換二值操作影象
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    ret,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    cv2.imshow('binary-image',binary)
    # 結構元素
    kernel = cv2.getStructuringElement(binary,cv2.MORPH_RECT,(5,5))
    # 形態學操作:morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
    binary = cv2.morphologyEx(binary,cv2.MORPH_OPEN,kernel) # 開操作
    cv2.imshow('open-image',binary)
    
img = cv2.imread('dog.jpg',1)
cv2.imshow('src-image',img)
open_demo(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果:

閉操作:

程式碼:

import cv2
import numpy as np

# 閉操作
def close_demo(image):
    # 轉換二值操作影象
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    ret,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    cv2.imshow('binary-image',binary)
    # 結構元素
    kernel = cv2.getStructuringElement(binary,cv2.MORPH_RECT,(5,5)) # 若達不到填充效果,調大
    # 形態學操作:morphologyEx(src, op, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) -> dst
    binary = cv2.morphologyEx(binary,cv2.MORPH_CLOSE,kernel) # 閉操作
    cv2.imshow('close-image',binary)
    
img = cv2.imread('dog.jpg',1)
cv2.imshow('src-image',img)
close_demo(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果:

其他形態學操作

頂帽

程式碼:

import cv2
import numpy as np

# 頂帽
def top_hat_image(image):
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
    dst = cv2.morphologyEx(gray,cv2.MORPH_TOPHAT,kernel)
    # 結果太暗了,提高點亮度
    cimage = np.array(gray.shape,np.uint8)
    cimage = 100
    dst = cv2.add(dst,cimage)
    
    cv2.imshow('top_hat_image',dst)
    
img = cv2.imread('dog.jpg',1)
cv2.imshow('src-image',img)
top_hat_image(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果:

黑帽

程式碼:

import cv2
import numpy as np

# 黑帽
def black_hat_image(image):
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
    dst = cv2.morphologyEx(gray,cv2.MORPH_BLACKHAT,kernel) # 黑帽
    # 結果太暗了,提高點亮度
    cimage = np.array(gray.shape,np.uint8)
    cimage = 100
    dst = cv2.add(dst,cimage)
    
    cv2.imshow('black_hat_image',dst)
    
img = cv2.imread('dog.jpg',1)
cv2.imshow('src-image',img)
black_hat_image(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果:

形態學梯度

基本梯度:

程式碼:

import cv2
import numpy as np

# 基本梯度
def base_gradient(image):
    # 本例中用二值影象來測試基本梯度
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    ret,binary = cv2.threshold(gray,0,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    cv2.imshow('binary-image',binary)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
    dst = cv2.morphologyEx(gray,cv2.MORPH_GRADIENT,kernel) # 基本梯度
    cv2.imshow('base_gradient',dst)
    
img = cv2.imread('numbers.jpg',1)
cv2.imshow('src-image',img)
base_gradient(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果:

內外梯度:

程式碼:

import cv2
import numpy as np

# 內外梯度
def base_gradient(image):
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
    dm = cv2.dilate(image,kernel)
    em = cv2.erode(image,kernel)
    dst1 = cv2.subtract(image,em) # 內梯度
    dst2 = cv2.subtract(dm,image) # 外梯度
    cv2.imshow('internal-image',dst1)
    cv2.imshow('external-image',dst2)
    
img = cv2.imread('numbers.jpg',1)
cv2.imshow('src-image',img)
base_gradient(img)
cv2.waitKey(0)
cv2.destroyAllWindows()

結果: