如何移除最大长度块周围的小块?

2024-06-06 13:57:07 发布

您现在位置:Python中文网/ 问答频道 /正文

result on the right

the example image

在上图中,如何获得一个干净的线块,移除上下围绕中长条宽带的线块?我试过投影,但失败了

def hProject(binary):
    h, w = binary.shape

    hprojection = np.zeros(binary.shape, dtype=np.uint8)
[red is the result][2]
    h_h = [0]*h
    for j in range(h):
        for i in range(w):
            if binary[j,i] == 255:
                h_h[j] += 1

    return h_h

Tags: infordefnpzerosrange宽带投影
1条回答
网友
1楼 · 发布于 2024-06-06 13:57:07
def creat_T_rectangle(h, w, mode='x_up'):
    assert mode in ['x_up', 'x_down', 'y_left', 'y_right']
    if mode == 'x_up':
        up_t = np.ones((h*2, w*3), np.uint8)
        up_t[:h, :w] = 0
        up_t[:h, 2*w:] = 0

        return up_t, (0, h, w, 2*w)   # y1, y2, x1, x2
    elif mode == 'y_left':
        left_t = np.ones((h*3, w*2), np.uint8)
        left_t[:h, :w] = 0
        left_t[2*h:, :w] = 0

        return left_t, (h, 2*h, 0, w)
    elif mode == 'x_down':
        down_t = np.ones((h*2, w*3), np.uint8)
        down_t[h:2*h, :w] = 0
        down_t[h:2*h, 2*w:] = 0

        return down_t, (h, 2*h, w, 2*w)
    elif mode == 'y_right':
        right_t = np.ones((h*3, w*2), np.uint8)
        right_t[:h, w:2*w] = 0
        right_t[2*h:, w:] = 0

        return right_t, (h, 2*h, w, 2*w)
    else:
        raise NotImplementedError

def remove_around_rectangle(markers, bh, bw):
    '''
    markers:binary image, bh, bw = 5, 5 ...
    '''
    
    up_t, up_rect = creat_T_rectangle(bh, bw, mode='x_up')
    down_t, down_rect = creat_T_rectangle(bh, bw, mode='x_down')
    one_nums = up_t.sum()
    i = bh
    j = bw
    while i < markers.shape[0]-bh:
        while j < markers.shape[1]-2*bw:
            block = markers[i-bh:i+bh, j-bw:j+2*bw]  
            inner_up = block * up_t
            inner_down = block * down_t
            if inner_down.sum()//255 == inner_up.sum()//255 == one_nums:
                markers[down_rect[0]+i-bh:down_rect[1]+i-bh, down_rect[2]+j-bw:down_rect[3]+j-bw] = 0
            else:
                if inner_up.sum()//255 == one_nums:
                    markers[up_rect[0]+i-bh:up_rect[1]+i-bh, up_rect[2]+j-bw:up_rect[3]+j-bw] = 0
                if inner_down.sum()//255 == one_nums:
                    markers[down_rect[0]+i-bh:down_rect[1]+i-bh, down_rect[2]+j-bw:down_rect[3]+j-bw] = 0
            
            j += bw
        i += 1
        j = bw
    
    
    left_t, left_rect = creat_T_rectangle(bh, bw, mode='y_left')
    one_nums = left_t.sum()
    
    right_t, right_rect = creat_T_rectangle(bh, bw, mode='y_right')
    i = bh
    j = bw
    while i < markers.shape[0] - 2*bh:
        while j < markers.shape[1] - bw:
            block = markers[i-bh:i+2*bh, j-bw:j+bw]   
            inner_left = block * left_t
            inner_right = block * right_t
            if inner_left.sum()//255 == one_nums == inner_right.sum()//255 :
                markers[left_rect[0]+i-bh:left_rect[1]+i-bh, left_rect[2]+j-bw:left_rect[3]+j-bw] = 0
            else:
                if inner_right.sum()//255 == one_nums:
                    markers[right_rect[0]+i-bh:right_rect[1]+i-bh, right_rect[2]+j-bw:right_rect[3]+j-bw] = 0
                if inner_left.sum()//255 == one_nums :
                    markers[left_rect[0]+i-bh:left_rect[1]+i-bh, left_rect[2]+j-bw:left_rect[3]+j-bw] = 0
            j += bw
        i += 1
        j = bw
    
    return markers

上面是我的代码,但速度太慢了

相关问题 更多 >