从1D信号中提取步进脉冲以进行对象分割

时间:2018-01-02 11:10:48

标签: python vector image-segmentation

我有1D向量,我想得到矩形信号的开始和结束位置(在下面的图像中突出显示)。我正在使用python,这个信号是y轴上二进制图像内的白色像素直方图。我试图在没有周围噪音的情况下获得物体的ROI。

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

这是一个天真的解决方案,但它的工作原理。从中间点开始,搜索左右两侧的急剧下降。

def get_start_end(projection):
    middle_indx = int(len(projection)/2)
    middle_value = projection[middle_indx]
    print "middle index is = ", middle_indx, " it's value is ", middle_value
    #-- search for sharp dropping right (end)
    for i, v in enumerate(projection[middle_indx+1:]):
        diff = int(middle_value) - v
        if(diff > 0.5*middle_value):
            end = i + middle_indx+1
            break

    #-- search for sharp dropping left (start)
    for i, v in enumerate(projection[:middle_indx]):
        diff = int(middle_value) - v
        if(diff > 0.5*middle_value):
            start = i
    return start, end

- 编辑

如果我们没有发现高于ratio*middle_value的跌幅,请找到最大跌幅。

def get_plate_y_coordinates(projection_vector):
    ratio = 0.5
    middle_indx = int(len(projection_vector)/2)
    middle_value = projection_vector[middle_indx]
    start = 0
    end = len(projection_vector)
    print "middle index is = ", middle_indx, " it's value is ", middle_value

    #-- search for sharp dropping right (end)
    saved_diff = []
    for i, v in enumerate(projection_vector[middle_indx+1:]):
        diff = int(middle_value) - v
        if(diff > ratio*middle_value):
            end = i + middle_indx+1
            break
        else:
            saved_diff.append((diff, i + middle_indx+1))

    if (end == len(projection_vector)) and (len(saved_diff)>0): #didn't chage
        saved_diff = np.array(saved_diff)
        sorted_diff = saved_diff[saved_diff[:,0].argsort()[::-1],:]
        end = int(sorted_diff[0,1])

    #-- search for sharp dropping left (start)
    saved_diff=[]
    for i, v in enumerate(projection_vector[:middle_indx]):
        diff = int(middle_value) - v
        if(diff > ratio*middle_value):
            start = i
        else:
            saved_diff.append((diff, i))

    if (start == 0) and (len(saved_diff)>0): #didn't chage
        saved_diff = np.array(saved_diff)
        sorted_diff = saved_diff[saved_diff[:,0].argsort()[::-1],:]
        start = int(sorted_diff[0,1])
    return start, end

enter image description here enter image description here