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


Python cv2.RETR_LIST屬性代碼示例

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


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

示例1: _find_size_candidates

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def _find_size_candidates(self, image):
        binary_image = self._filter_image(image)

        _, contours, _ = cv2.findContours(binary_image,
                                          cv2.RETR_LIST,
                                          cv2.CHAIN_APPROX_SIMPLE)

        size_candidates = []
        for contour in contours:
            bounding_rect = cv2.boundingRect(contour)
            contour_area = cv2.contourArea(contour)
            if self._is_valid_contour(contour_area, bounding_rect):
                candidate = (bounding_rect[2] + bounding_rect[3]) / 2
                size_candidates.append(candidate)

        return size_candidates 
開發者ID:nemanja-m,項目名稱:gaps,代碼行數:18,代碼來源:size_detector.py

示例2: find_squares

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def find_squares(img):
    img = cv2.GaussianBlur(img, (5, 5), 0)
    squares = []
    for gray in cv2.split(img):
        for thrs in xrange(0, 255, 26):
            if thrs == 0:
                bin = cv2.Canny(gray, 0, 50, apertureSize=5)
                bin = cv2.dilate(bin, None)
            else:
                retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
            bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                cnt_len = cv2.arcLength(cnt, True)
                cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
                if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
                    cnt = cnt.reshape(-1, 2)
                    max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
                    if max_cos < 0.1:
                        squares.append(cnt)
    return squares 
開發者ID:makelove,項目名稱:OpenCV-Python-Tutorial,代碼行數:22,代碼來源:squares.py

示例3: findPossibleCharsInPlate

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def findPossibleCharsInPlate(imgGrayscale, imgThresh):
    listOfPossibleChars = []                        # this will be the return value
    contours = []
    imgThreshCopy = imgThresh.copy()

            # find all contours in plate
    contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    for contour in contours:                        # for each contour
        possibleChar = PossibleChar.PossibleChar(contour)

        if checkIfPossibleChar(possibleChar):              # if contour is a possible char, note this does not compare to other chars (yet) . . .
            listOfPossibleChars.append(possibleChar)       # add to list of possible chars
        # end if
    # end if

    return listOfPossibleChars
# end function

################################################################################################### 
開發者ID:muchlisinadi,項目名稱:ALPR-Indonesia,代碼行數:22,代碼來源:DetectChars.py

示例4: polygons

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def polygons(self):
        """
        Returns or generates :class:`Polygons` representation of mask.

        :returns: Polygons representation
        :rtype: :class:`Polygons`
        """
        if not self._c_polygons:

            # Generate polygons from mask
            mask = self.array.astype(np.uint8)
            mask = cv2.copyMakeBorder(mask, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
            polygons = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE, offset=(-1, -1))
            polygons = polygons[0] if len(polygons) == 2 else polygons[1]
            polygons = [polygon.flatten() for polygon in polygons]

            self._c_polygons = Polygons(polygons)
            self._c_polygons._c_mask = self

        return self._c_polygons 
開發者ID:jsbroks,項目名稱:imantics,代碼行數:22,代碼來源:annotation.py

示例5: crop_image

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def crop_image(silhs):
    res = np.asarray(silhs).any(axis=0)
    cnts, hier = cv2.findContours(
        np.uint8(res) * 255, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    """
    checks = []
    for cnt in cnts:
        kk = np.zeros((1000, 1000, 3), dtype=np.uint8)
        hull = cv2.convexHull(cnt)
        cv2.drawContours(kk, [cnt], 0, (255,255,255), -1)
        checks.append(kk)
    """

    max_id = 0
    max_length = len(cnts[0])
    for i in range(1, len(cnts)):
        if len(cnts[i]) > max_length:
            max_id = i
            max_length = len(cnts[i])

    (x, y, w, h) = cv2.boundingRect(cnts[max_id])
    return (x, y, w, h) 
開發者ID:akanazawa,項目名稱:human_dynamics,代碼行數:24,代碼來源:read_human36m.py

示例6: crop_and_clean_mask_to_int

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def crop_and_clean_mask_to_int(mask, x, y, w, h):
    mask = np.uint8(mask[y:y + h, x:x + w]) * 255

    # TODO: put this into a function (it's used above as well)
    cnts, hier = cv2.findContours(mask.copy(), cv2.RETR_LIST,
                                  cv2.CHAIN_APPROX_SIMPLE)

    max_id = 0
    max_length = len(cnts[0])
    for i in range(1, len(cnts)):
        if len(cnts[i]) > max_length:
            max_id = i
            max_length = len(cnts[i])

    tmp_mask = np.dstack((mask, mask, mask))
    for i, cnt in enumerate(cnts):
        if i != max_id:
            cv2.drawContours(tmp_mask, [cnt], 0, (0, 0, 0), -1)
    return cv2.split(tmp_mask)[0] 
開發者ID:akanazawa,項目名稱:human_dynamics,代碼行數:21,代碼來源:read_human36m.py

示例7: detect_edge

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def detect_edge(self, image, enabled_transform = False):
        dst = None
        orig = image.copy()

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (5, 5), 0)
        edged = cv2.Canny(blurred, 0, 20)
        _, contours, _ = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

        contours = sorted(contours, key=cv2.contourArea, reverse=True)

        for cnt in contours:
            epsilon = 0.051 * cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, epsilon, True)

            if len(approx) == 4:
                target = approx
                cv2.drawContours(image, [target], -1, (0, 255, 0), 2)

                if enabled_transform:
                    approx = rect.rectify(target)
                    # pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])
                    # M = cv2.getPerspectiveTransform(approx,pts2)
                    # dst = cv2.warpPerspective(orig,M,(800,800))
                    dst = self.four_point_transform(orig, approx)
                break

        return image, dst 
開發者ID:yushulx,項目名稱:web-document-scanner,代碼行數:30,代碼來源:document.py

示例8: find_marker

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def find_marker(image):
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	gray = cv2.GaussianBlur(gray, (5, 5), 0)
	edged = cv2.Canny(gray, 35, 125)
	(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
	c = max(cnts, key = cv2.contourArea)

	# compute the bounding box of the of the paper region and return it
	return cv2.minAreaRect(c) 
開發者ID:dark-archerx,項目名稱:Traffic-Signs-and-Object-Detection,代碼行數:11,代碼來源:dista.py

示例9: find_bbox

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def find_bbox(img, img_closed):  # 尋找身份證正反麵區域
    """
    根據二值化結果判定並裁剪出身份證正反麵區域
    :param img: 原始RGB圖片
    :param img_closed: 二值化後的圖片
    :return: 身份證正反麵區域
    """
    (contours, _) = cv2.findContours(img_closed.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)  # 求出框的個數
    # 這裏opencv如果版本不對(4.0或以上)會報錯,隻需把(contours, _)改成 (_, contours, _)

    contours = sorted(contours, key=cv2.contourArea, reverse=True)  # 按照麵積大小排序

    countours_res = []
    for i in range(0, len(contours)):
        area = cv2.contourArea(contours[i])  # 計算麵積

        if (area <= 0.4 * img.shape[0] * img.shape[1]) and (area >= 0.05 * img.shape[0] * img.shape[1]):
            # 人為設定,身份證正反麵框的大小不會超過整張圖片大小的0.4,不會小於0.05(這個參數隨便設置的)
            rect = cv2.minAreaRect(contours[i])  # 最小外接矩,返回值有中心點坐標,矩形寬高,傾斜角度三個參數
            box = cv2.boxPoints(rect)
            left_down, right_down, left_up, right_up = point_judge([int(rect[0][0]), int(rect[0][1])], box)
            src = np.float32([left_down, right_down, left_up, right_up])  # 這裏注意必須對應

            dst = np.float32([[0, 0], [int(max(rect[1][0], rect[1][1])), 0], [0, int(min(rect[1][0], rect[1][1]))],
                              [int(max(rect[1][0], rect[1][1])),
                               int(min(rect[1][0], rect[1][1]))]])  # rect中的寬高不清楚是個怎麽機製,但是對於身份證,肯定是寬大於高,因此加個判定
            m = cv2.getPerspectiveTransform(src, dst)  # 得到投影變換矩陣
            result = cv2.warpPerspective(img, m, (int(max(rect[1][0], rect[1][1])), int(min(rect[1][0], rect[1][1]))),
                                         flags=cv2.INTER_CUBIC)  # 投影變換
            countours_res.append(result)
    return countours_res  # 返回身份證區域 
開發者ID:Mingtzge,項目名稱:2019-CCF-BDCI-OCR-MCZJ-OCR-IdentificationIDElement,代碼行數:33,代碼來源:cut_part.py

示例10: find_items

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def find_items(maze_image):
    # Preprocessing to find the contour of the shapes
    h, w = maze_image.shape[0], maze_image.shape[1]
    dim = (h+w)//2
    b_and_w = cv2.cvtColor(maze_image, cv2.COLOR_BGR2GRAY)
    edges = cv2.GaussianBlur(b_and_w, (11, 11), 0)
    edges = cv2.adaptiveThreshold(edges, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 9, 2)

    cv2.rectangle(edges,(0, 0),(w-1,h-1),(255,255,255),16)
    contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    # cv2.imshow('d', edges)

    items = []

    if contours:
        item_mask = np.zeros(edges.shape, np.uint8)
        conts = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=False)

        for cnt in conts:

            if cv2.contourArea(cnt) > 0.35*dim:
                return items, item_mask

            elif cv2.contourArea(cnt) > 0.05*dim:
                d = np.mean(cnt, axis=0)
                d[0][0], d[0][1] = int(round(d[0][0])), int(round(d[0][1]))

                # TODO adjust the size here?
                if cv2.contourArea(cnt) < 0.1*dim:
                    items.append((d, 'smol'))
                    cv2.drawContours(item_mask, [cnt], -1, (255,255,255), -1)
                else:
                    items.append((d, 'big'))
                    cv2.drawContours(item_mask, [cnt], -1, (255,255,255), -1)

    return items, item_mask 
開發者ID:guille0,項目名稱:hazymaze,代碼行數:39,代碼來源:extract_lines.py

示例11: findPossibleCharsInScene

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def findPossibleCharsInScene(imgThresh):
    listOfPossibleChars = []                # this will be the return value

    intCountOfPossibleChars = 0

    imgThreshCopy = imgThresh.copy()

    contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)   # find all contours
    height, width = imgThresh.shape
    imgContours = np.zeros((height, width, 3), np.uint8)

    for i in range(0, len(contours)):                       # for each contour

        possibleChar = PossibleChar.PossibleChar(contours[i])

        if DetectChars.checkIfPossibleChar(possibleChar):                   # if contour is a possible char, note this does not compare to other chars (yet) . . .
            intCountOfPossibleChars = intCountOfPossibleChars + 1           # increment count of possible chars
            listOfPossibleChars.append(possibleChar)                        # and add to list of possible chars
        # end if
    # end for

    return listOfPossibleChars
# end function


################################################################################################### 
開發者ID:muchlisinadi,項目名稱:ALPR-Indonesia,代碼行數:28,代碼來源:DetectPlates.py

示例12: findEllipses

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def findEllipses(edges):
    contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    ellipseMask = np.zeros(edges.shape, dtype=np.uint8)
    contourMask = np.zeros(edges.shape, dtype=np.uint8)

    pi_4 = np.pi * 4

    for i, contour in enumerate(contours):
        if len(contour) < 5:
            continue

        area = cv2.contourArea(contour)
        if area <= 100:  # skip ellipses smaller then 10x10
            continue

        arclen = cv2.arcLength(contour, True)
        circularity = (pi_4 * area) / (arclen * arclen)
        ellipse = cv2.fitEllipse(contour)
        poly = cv2.ellipse2Poly((int(ellipse[0][0]), int(ellipse[0][1])), (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)), int(ellipse[2]), 0, 360, 5)

        # if contour is circular enough
        if circularity > 0.6:
            cv2.fillPoly(ellipseMask, [poly], 255)
            continue

        # if contour has enough similarity to an ellipse
        similarity = cv2.matchShapes(poly.reshape((poly.shape[0], 1, poly.shape[1])), contour, cv2.cv.CV_CONTOURS_MATCH_I2, 0)
        if similarity <= 0.2:
            cv2.fillPoly(contourMask, [poly], 255)

    return ellipseMask, contourMask 
開發者ID:AVGInnovationLabs,項目名稱:DoNotSnap,代碼行數:33,代碼來源:RegionOfInterest.py

示例13: find_markers_from_img_thresh

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def find_markers_from_img_thresh(img_thresh, max_dist_between_centers=3, min_radius_circle=4,
                                 max_radius_circle=35, min_radius_marker=7):
    contours = cv2.findContours(img_thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
    list_potential_markers = []
    for cnt in contours:
        (x, y), radius = cv2.minEnclosingCircle(cnt)
        if not min_radius_circle < radius < max_radius_circle:
            continue
        center = (int(round(x)), int(round(y)))
        radius = int(radius)
        list_potential_markers.append(PotentialMarker(center, radius, cnt))

    list_potential_markers = sorted(list_potential_markers, key=lambda m: m.x)
    list_good_candidates = []

    for i, potential_marker in enumerate(list_potential_markers):
        if potential_marker.is_merged:
            continue
        marker1 = Marker(potential_marker)
        center_marker = marker1.get_center()

        for potential_marker2 in list_potential_markers[i + 1:]:
            if potential_marker.is_merged:
                continue
            center_potential = potential_marker2.get_center()
            if center_potential[0] - center_marker[0] > max_dist_between_centers:
                break
            dist = euclidean_dist_2_pts(center_marker, center_potential)
            if dist <= max_dist_between_centers:
                marker1.add_circle(potential_marker2)
                center_marker = marker1.get_center()

        if marker1.nb_circles() > 2 and marker1.radius >= min_radius_marker:
            list_good_candidates.append(marker1)
            marker1.get_id_from_slice(img_thresh)

    return list_good_candidates 
開發者ID:NiryoRobotics,項目名稱:niryo_one_ros,代碼行數:39,代碼來源:markers_detection.py

示例14: biggest_contours_finder

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def biggest_contours_finder(img, nb_contours_max=3):
    """
    Fonction to find the biggest contour in an binary image
    :param img: Binary Image
    :param nb_contours_max: maximal number of contours which will be returned
    :return: biggest contours found
    """
    contours = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[-2]
    if not contours:
        return None
    contours_area = list()
    for cnt in contours:
        contours_area.append(cv2.contourArea(cnt))
    biggest_contours = []
    le = len(contours_area)
    if nb_contours_max > le:
        nb_contours = le
        id_contours_sorted_init = list(range(nb_contours))
    else:
        nb_contours = nb_contours_max
        id_contours_sorted_init = np.argpartition(contours_area, -nb_contours)[-nb_contours:]
    id_contours_sorted = [x for x in sorted(id_contours_sorted_init, key=lambda idi: -contours_area[idi])]

    for i in range(nb_contours):
        id_used = id_contours_sorted[i]

        if contours_area[id_used] < 400:
            break

        biggest_contours.append(contours[id_used])
    return biggest_contours 
開發者ID:NiryoRobotics,項目名稱:niryo_one_ros,代碼行數:33,代碼來源:image_functions.py

示例15: detect_biggest_polygon

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_LIST [as 別名]
def detect_biggest_polygon(binary_img):
    binary_img_to_process = binary_img.copy()  # contour detection will mess up the image
    contours, _ = cv2.findContours(binary_img_to_process, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    biggest_contour = None
    if contours != []:
        biggest_contour_index = 0
        for i in range(1, len(contours)):
            if cv2.contourArea(contours[i]) > cv2.contourArea(contours[biggest_contour_index]):
                biggest_contour_index = i
        
        biggest_contour = contours[biggest_contour_index]
    return biggest_contour 
開發者ID:dronekit,項目名稱:visual-followme,代碼行數:14,代碼來源:red_blob_detection.py


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