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


Python cv2.MORPH_DILATE屬性代碼示例

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


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

示例1: skeletonize

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_DILATE [as 別名]
def skeletonize(img):
    """ OpenCV function to return a skeletonized version of img, a Mat object"""

    #  hat tip to http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/

    img = img.copy() # don't clobber original
    skel = img.copy()

    skel[:,:] = 0
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))

    while True:
        eroded = cv2.morphologyEx(img, cv2.MORPH_ERODE, kernel)
        temp = cv2.morphologyEx(eroded, cv2.MORPH_DILATE, kernel)
        temp  = cv2.subtract(img, temp)
        skel = cv2.bitwise_or(skel, temp)
        img[:,:] = eroded[:,:]
        if cv2.countNonZero(img) == 0:
            break

    return skel 
開發者ID:ver228,項目名稱:tierpsy-tracker,代碼行數:23,代碼來源:getFoodContourMorph.py

示例2: morphological_transformations

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_DILATE [as 別名]
def morphological_transformations(im_thresh, morpho_type="CLOSE", kernel_shape=(5, 5), kernel_type="ELLIPSE"):
    """
    Take black & white image and apply morphological transformation
    :param im_thresh: Black & White Image
    :param morpho_type: CLOSE/OPEN/ERODE/DILATE => See on OpenCV/Google if you do not know these words
    :param kernel_shape: tuple corresponding to the size of the kernel
    :param kernel_type: RECT/ELLIPSE/CROSS => see on OpenCV
    :return: image after processing
    """
    if kernel_type == "CROSS":
        kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, kernel_shape)
    elif kernel_type == "RECT":
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernel_shape)
    else:
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, kernel_shape)

    if morpho_type == "OPEN":
        morph_type = cv2.MORPH_OPEN
    elif morpho_type == "DILATE":
        morph_type = cv2.MORPH_DILATE
    elif morpho_type == "ERODE":
        morph_type = cv2.MORPH_ERODE
    else:
        morph_type = cv2.MORPH_CLOSE

    return cv2.morphologyEx(im_thresh, morph_type, kernel)


# Contours 
開發者ID:NiryoRobotics,項目名稱:niryo_one_ros,代碼行數:31,代碼來源:image_functions.py

示例3: trapped_ball_fill_single

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_DILATE [as 別名]
def trapped_ball_fill_single(image, seed_point, radius):
    """Perform a single trapped ball fill operation.

    # Arguments
        image: an image. the image should consist of white background, black lines and black fills.
               the white area is unfilled area, and the black area is filled area.
        seed_point: seed point for trapped-ball fill, a tuple (integer, integer).
        radius: radius of ball shape.
    # Returns
        an image after filling.
    """
    ball = get_ball_structuring_element(radius)

    pass1 = np.full(image.shape, 255, np.uint8)
    pass2 = np.full(image.shape, 255, np.uint8)

    im_inv = cv2.bitwise_not(image)

    # Floodfill the image
    mask1 = cv2.copyMakeBorder(im_inv, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0)
    _, pass1, _, _ = cv2.floodFill(pass1, mask1, seed_point, 0, 0, 0, 4)

    # Perform dilation on image. The fill areas between gaps became disconnected.
    pass1 = cv2.morphologyEx(pass1, cv2.MORPH_DILATE, ball, anchor=(-1, -1), iterations=1)
    mask2 = cv2.copyMakeBorder(pass1, 1, 1, 1, 1, cv2.BORDER_CONSTANT, 0)

    # Floodfill with seed point again to select one fill area.
    _, pass2, _, rect = cv2.floodFill(pass2, mask2, seed_point, 0, 0, 0, 4)
    # Perform erosion on the fill result leaking-proof fill.
    pass2 = cv2.morphologyEx(pass2, cv2.MORPH_ERODE, ball, anchor=(-1, -1), iterations=1)

    return pass2 
開發者ID:hepesu,項目名稱:LineFiller,代碼行數:34,代碼來源:trappedball_fill.py

示例4: get_border_point

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_DILATE [as 別名]
def get_border_point(points, rect, max_height, max_width):
    """Get border points of a fill area

    # Arguments
        points: points of fill .
        rect: bounding rect of fill.
        max_height: image max height.
        max_width: image max width.
    # Returns
        points , convex shape of points
    """
    # Get a local bounding rect.
    border_rect = get_border_bounding_rect(max_height, max_width, rect[:2], rect[2:], 2)

    # Get fill in rect.
    fill = np.zeros((border_rect[3] - border_rect[1], border_rect[2] - border_rect[0]), np.uint8)
    # Move points to the rect.
    fill[(points[0] - border_rect[1], points[1] - border_rect[0])] = 255

    # Get shape.
    _, contours, _ = cv2.findContours(fill, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    approx_shape = cv2.approxPolyDP(contours[0], 0.02 * cv2.arcLength(contours[0], True), True)

    # Get border pixel.
    # Structuring element in cross shape is used instead of box to get 4-connected border.
    cross = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
    border_pixel_mask = cv2.morphologyEx(fill, cv2.MORPH_DILATE, cross, anchor=(-1, -1), iterations=1) - fill
    border_pixel_points = np.where(border_pixel_mask == 255)

    # Transform points back to fillmap.
    border_pixel_points = (border_pixel_points[0] + border_rect[1], border_pixel_points[1] + border_rect[0])

    return border_pixel_points, approx_shape 
開發者ID:hepesu,項目名稱:LineFiller,代碼行數:35,代碼來源:trappedball_fill.py

示例5: invisibility

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_DILATE [as 別名]
def invisibility(image):

    # converting from BGR to HSV color space
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    # cv2.imshow("hsv", hsv[..., 0])

    # Range for lower red
    lower_red = np.array([0, 120, 70])
    upper_red = np.array([10, 255, 255])
    mask1 = cv2.inRange(hsv, lower_red, upper_red)

    # Range for upper range
    lower_red = np.array([170, 120, 70])
    upper_red = np.array([180, 255, 255])
    mask2 = cv2.inRange(hsv, lower_red, upper_red)

    # Generating the final mask to detect red color
    mask1 = mask1 + mask2

    mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
    mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, np.ones((3, 3), np.uint8))

    # creating an inverted mask to segment out the cloth from the frame
    mask2 = cv2.bitwise_not(mask1)
    # Segmenting the cloth out of the frame using bitwise and with the inverted mask
    res1 = cv2.bitwise_and(image, image, mask=mask2)

    # creating image showing static background frame pixels only for the masked region
    res2 = cv2.bitwise_and(background, background, mask=mask1)
    # Generating the final output
    final_output = cv2.addWeighted(res1, 1, res2, 1, 0)
    return final_output 
開發者ID:charlielito,項目名稱:snapchat-filters-opencv,代碼行數:34,代碼來源:invisibility_cloak.py

示例6: __extract_grids__

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import MORPH_DILATE [as 別名]
def __extract_grids__(img,horizontal):
    assert len(img.shape) == 2
    # height,width = img.shape
    grid_lines = []

    if horizontal:
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,1))
        d_image = cv2.Sobel(img,cv2.CV_16S,0,2)

    else:
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(1,5))

        d_image = cv2.Sobel(img,cv2.CV_16S,2,0)

    d_image = cv2.convertScaleAbs(d_image)
    cv2.normalize(d_image,d_image,0,255,cv2.NORM_MINMAX)
    ret,close = cv2.threshold(d_image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

    # _,th1 = cv2.threshold(d_image,127,255,cv2.THRESH_BINARY)

    close = cv2.morphologyEx(close,cv2.MORPH_DILATE,kernel)

    _,contour, hier = cv2.findContours(close.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    for cnt in contour:
        x,y,w,h = cv2.boundingRect(cnt)
        perimeter = cv2.arcLength(cnt,True)
        if min(h,w) > 1 and (perimeter > 500):



            s = cnt.shape
            f = np.reshape(cnt,(s[0],s[2]))

            if horizontal and (w/h > 5):
                grid_lines.append(f)
                template = np.zeros(img.shape,np.uint8)
                template.fill(255)
                cv2.drawContours(template, [cnt], 0, 0, -1)
                plt.imshow(template)
                plt.show()

            elif not horizontal and (h/w > 5):
                grid_lines.append(f)


    return grid_lines 
開發者ID:zooniverse,項目名稱:aggregation,代碼行數:48,代碼來源:paper_quad.py


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