當前位置: 首頁>>代碼示例>>Python>>正文


Python cv2.MORPH_TOPHAT屬性代碼示例

本文整理匯總了Python中cv2.MORPH_TOPHAT屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.MORPH_TOPHAT屬性的具體用法?Python cv2.MORPH_TOPHAT怎麽用?Python cv2.MORPH_TOPHAT使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在cv2的用法示例。


在下文中一共展示了cv2.MORPH_TOPHAT屬性的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: maximizeContrast

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_TOPHAT [as 別名]
def maximizeContrast(imgGrayscale):

    height, width = imgGrayscale.shape

    imgTopHat = np.zeros((height, width, 1), np.uint8)
    imgBlackHat = np.zeros((height, width, 1), np.uint8)

    structuringElement = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

    imgTopHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_TOPHAT, structuringElement)
    imgBlackHat = cv2.morphologyEx(imgGrayscale, cv2.MORPH_BLACKHAT, structuringElement)

    imgGrayscalePlusTopHat = cv2.add(imgGrayscale, imgTopHat)
    imgGrayscalePlusTopHatMinusBlackHat = cv2.subtract(imgGrayscalePlusTopHat, imgBlackHat)

    return imgGrayscalePlusTopHatMinusBlackHat
# end function 
開發者ID:muchlisinadi,項目名稱:ALPR-Indonesia,代碼行數:19,代碼來源:Preprocess.py

示例2: remove_background_perframe

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_TOPHAT [as 別名]
def remove_background_perframe(fm, method, wnd, selem):
    if method == 'uniform':
        return fm - uniform_filter(fm, wnd)
    elif method == 'tophat':
        return cv2.morphologyEx(fm, cv2.MORPH_TOPHAT, selem) 
開發者ID:DeniseCaiLab,項目名稱:minian,代碼行數:7,代碼來源:preprocessing.py

示例3: process

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_TOPHAT [as 別名]
def process(self, cv_before, name):

        k = self.k[0]
        kernel = np.ones((k, k), np.uint8)

        if name == 'Invert':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.bitwise_not(cv_before)
        elif name == 'Histogram Equalization':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
            cv_after = clahe.apply(cv_before)
        elif name == 'Threshold':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            ret, cv_after = cv2.threshold(
                cv_before, k, 255, cv2.THRESH_BINARY)
        elif name == 'Gaussian Threshold':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.adaptiveThreshold(cv_before, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                             cv2.THRESH_BINARY, k, 2)
        elif name == 'HSV':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2HSV)
            lower_color = np.array([k - 35, 0, 0])
            upper_color = np.array([k + 35, 255, 255])
            cv_after = cv2.inRange(cv_before, lower_color, upper_color)
        elif name == 'LAB':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2LAB)
            L, a, b = cv2.split(cv_before)
            ret, cv_after = cv2.threshold(L, k, 255, cv2.THRESH_BINARY)
        elif name == 'Erosion':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.erode(cv_before, kernel, iterations=1)
        elif name == 'Dilation':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.dilate(cv_before, kernel, iterations=1)
        elif name == 'Opening':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.morphologyEx(
                cv_before, cv2.MORPH_OPEN, kernel)
        elif name == 'Closing':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.morphologyEx(
                cv_before, cv2.MORPH_CLOSE, kernel)
        elif name == 'Top Hat':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.morphologyEx(
                cv_before, cv2.MORPH_TOPHAT, kernel)
        elif name == 'Black Hat':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.morphologyEx(
                cv_before, cv2.MORPH_BLACKHAT, kernel)
        elif name == 'Canny':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.Canny(cv_before, 100, k)
        elif name == 'Laplacian':
            cv_before = cv2.cvtColor(cv_before, cv2.COLOR_RGB2GRAY)
            cv_after = cv2.Laplacian(cv_before, cv2.CV_64F)
            cv_after = np.absolute(cv_after)
            cv_after = np.uint8(cv_after)

        return cv_after 
開發者ID:anonymouslycn,項目名稱:bjtu_BinocularCameraRecord,代碼行數:63,代碼來源:FilterCvQtContainer.py

示例4: filter_lane_points

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_TOPHAT [as 別名]
def filter_lane_points(self,
                           img,
                           filter_type='bilateral',
                           ksize_r=25,
                           C_r=8,
                           ksize_b=35,
                           C_b=5,
                           mask_noise=False,
                           ksize_noise=65,
                           C_noise=10,
                           noise_thresh=135):
        '''
        Filter an image to isolate lane lines and return a binary version.

        All image color space conversion, thresholding, filtering and morphing
        happens inside this method. It takes an RGB color image as input and
        returns a binary filtered version.
        '''

        # Define structuring elements for cv2 functions
        strel_lab_b = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(55,55))
        strel_rgb_r = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(29,29))
        strel_open = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(5,5))
        # Extract RGB R-channel and LAB B-channel
        rgb_r_channel = img[:,:,0]
        lab_b_channel = (cv2.cvtColor(img, cv2.COLOR_RGB2LAB))[:,:,2]
        # Apply tophat morphology
        rgb_r_tophat = cv2.morphologyEx(rgb_r_channel, cv2.MORPH_TOPHAT, strel_rgb_r, iterations=1)
        lab_b_tophat = cv2.morphologyEx(lab_b_channel, cv2.MORPH_TOPHAT, strel_lab_b, iterations=1)
        if filter_type == 'bilateral':
            # Apply bilateral adaptive color thresholding
            rgb_r_thresh = bilateral_adaptive_threshold(rgb_r_tophat, ksize=ksize_r, C=C_r)
            lab_b_thresh = bilateral_adaptive_threshold(lab_b_tophat, ksize=ksize_b, C=C_b)
        elif filter_type == 'neighborhood':
            rgb_r_thresh = cv2.adaptiveThreshold(rgb_r_channel, 255, adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C, thresholdType=cv2.THRESH_BINARY, blockSize=ksize_r, C=-C_r)
            lab_b_thresh = cv2.adaptiveThreshold(lab_b_channel, 255, adaptiveMethod=cv2.ADAPTIVE_THRESH_MEAN_C, thresholdType=cv2.THRESH_BINARY, blockSize=ksize_b, C=-C_b)
        else:
            raise ValueError("Unexpected filter mode. Expected modes are 'bilateral' or 'neighborhood'.")
        if mask_noise: # Merge both color channels and the noise mask
            # Create a mask to filter out noise such as trees and other greenery based on the LAB B-channel
            noise_mask_part1 = cv2.inRange(lab_b_channel, noise_thresh, 255) # This catches the noise, but unfortunately also the yellow line, therefore...
            noise_mask_part2 = bilateral_adaptive_threshold(lab_b_channel, ksize=ksize_noise, C=C_noise) # ...this brings the yellow line back...
            noise_bool = np.logical_or(np.logical_not(noise_mask_part1), noise_mask_part2) # ...once we combine the two.
            noise_mask = np.zeros_like(rgb_r_channel, dtype=np.uint8)
            noise_mask[noise_bool] = 255

            merged_bool = np.logical_and(np.logical_or(rgb_r_thresh, lab_b_thresh), noise_mask)
            merged = np.zeros_like(rgb_r_channel, dtype=np.uint8)
            merged[merged_bool] = 255
        else: # Only merge the two color channels
            merged_bool = np.logical_or(rgb_r_thresh, lab_b_thresh)
            merged = np.zeros_like(rgb_r_channel, dtype=np.uint8)
            merged[merged_bool] = 255

        # Apply open morphology
        opened = cv2.morphologyEx(merged, cv2.MORPH_OPEN, strel_open, iterations=1)

        return opened 
開發者ID:pierluigiferrari,項目名稱:lane_tracker,代碼行數:60,代碼來源:lane_tracker.py


注:本文中的cv2.MORPH_TOPHAT屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。