【问题标题】:How to get only the center object in an image using OpenCV?如何使用 OpenCV 仅获取图像中的中心对象?
【发布时间】:2021-04-04 00:55:09
【问题描述】:

我试图通过使用连接的 cv2.connectedComponentsWithStats 仅获取此图像中的中心 retangle。但是我不知道如何只获取中心图像。

我的尝试是这样的:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

img = cv2.threshold(bw, 127, 255, cv2.THRESH_BINARY)[1]  # ensure binary
def undesired_objects(image):
    image = image.astype('uint8')
    nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)
    sizes = stats[:, -1]

    max_label = 1
    max_size = sizes[1]
    for i in range(2, nb_components):
        if sizes[i] > max_size:
            max_label = i
            max_size = sizes[i]

    img2 = np.zeros(output.shape)
    img2[output == max_label] = 255
    cv2.imshow("Biggest component", img2)
    cv2.waitKey()

undesired_objects(img)

【问题讨论】:

    标签: python image opencv object-detection


    【解决方案1】:

    我认为您应该在使用 connectedComponentsWithStats 之前更好地处理您的图像

    import cv2
    import numpy as np
    
    
    def threshold_gray_const(image_, rang: tuple):
        return cv2.inRange(image_, rang[0], rang[1])
    
    
    def reject_borders(image_):
        out_image = image_.copy()
        h, w = image_.shape[:2]
        for row in range(h):
            if out_image[row, 0] == 255:
                cv2.floodFill(out_image, None, (0, row), 0)
            if out_image[row, w - 1] == 255:
                cv2.floodFill(out_image, None, (w - 1, row), 0)
        for col in range(w):
            if out_image[0, col] == 255:
                cv2.floodFill(out_image, None, (col, 0), 0)
            if out_image[h - 1, col] == 255:
                cv2.floodFill(out_image, None, (col, h - 1), 0)
        return out_image
    
    
    img = cv2.imread("D:\\Downloads\\ZXo3i.png")
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # ret, bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    #
    # img = cv2.threshold(bw, 127, 255, cv2.THRESH_BINARY)[1]  # ensure binary
    img = threshold_gray_const(gray, (240, 255))
    img = reject_borders(img)
    
    
    def undesired_objects(image):
        image = image.astype('uint8')
    
        nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)
        sizes = stats[:, -1]
    
        max_label = 1
        max_size = sizes[1]
        for i in range(2, nb_components):
            if sizes[i] > max_size:
                max_label = i
                max_size = sizes[i]
    
        img2 = np.zeros(output.shape)
        img2[output == max_label] = 255
        cv2.imshow("Biggest component", img2)
        cv2.waitKey()
    
    
    undesired_objects(img)
    

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2019-01-02
    • 2021-11-20
    • 2019-10-16
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多