1. 程式人生 > >第十九天黑帽頂帽

第十九天黑帽頂帽

 

numpy array 和 python list 有什麼區別?標準Python的列表(list)中,元素本質是物件。如:L = [1, 2, 3],需要3個指標和三個整數物件,對於數值運算比較浪費記憶體和CPU。因此,Numpy提供了ndarray(N-dimensional array object)物件:儲存單一資料型別的多維陣列。

 

 

 

import cv2 as cv
import numpy as np

def hat_gray_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))##獲取結構元數
    dst = cv.morphologyEx(gray, cv.MORPH_TOPHAT, kernel)  #頂帽
    cimage = np.array(gray.shape, np.uint8)
    cimage = 120;
    dst = cv.add(dst, cimage)
    cv.imshow("tophat", dst)


def hat_binary_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)##二值影象獲取
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
    dst = cv.morphologyEx(binary, cv.MORPH_BLACKHAT, kernel)
    cv.imshow("tophat", dst)


def hat_binary_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    dst = cv.morphologyEx(binary, cv.MORPH_GRADIENT, kernel)
    cv.imshow("tophat", dst)


def gradient2_demo(image):
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    dm = cv.dilate(image, kernel)  ##腐蝕
    em = cv.erode(image, kernel)  #膨脹
    dst1 = cv.subtract(image, em) # 計算陣列差值internal gradient內部梯度
    dst2 = cv.subtract(dm, image) # external gradient外部梯度
    cv.imshow("internal", dst1)
    cv.imshow("external", dst2)



print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("C:/Users/weiqiangwen/Desktop/sest/data/digits.png")
# cv.namedWindow("input contours",cv.WINDOW_AUTOSIZE)
cv.imshow("contours", src)

cv.waitKey(0)

cv.destroyAllWindows()