人为地增加图像的关注区域

时间:2019-10-01 14:02:02

标签: python image image-processing graphics computer-vision

我正在解决图像分割问题。为了提高模型的准确性,我遇到了以下预处理步骤-

  

首先,ROI外边界的像素集为   术语,即在ROI之外并且是相邻像素   (使用四个邻域)到其中的像素。然后,每个像素值   该集合中的替换为其邻居的平均值(此   在ROI中使用8个社区)。最后,投资回报率是   通过包含此更改的像素集进行扩展。这个过程是   重复,可以看作是人为地增加了ROI。

应用以下步骤后的图像如下- enter image description here

我正在上述步骤中应用的图像如下- enter image description here

在询问问题here时,我采用了最初的方法来拍摄图像周围的黑色区域并将其用作Alpha蒙版。
解决此问题的方法是找到所有边界像素(在FOV之外),这是我使用以下代码获得的红色部分之外的第一个黑色像素-

    my_list = []
for i in range(img.shape[0]):
    for j in range(img.shape[1]):
        if (j+1)<=564:
            if (img[i][j+1]==255) and (img[i][j-1]!=255):
                my_list.append([i,j])
        elif (j+1)<=564:
            if img[i][j+1]==0 and img[i][j]!=0:
                my_list.append([i,j])
        elif (i-1)>=0:
            if img[i-1][j]==255 and img[i][j]==0 :
                my_list.append([i,j])
        elif (i+1) >=0:
            if img[i+1][j]==255 and img[i][j]==0:
                my_list.append([i,j])

我在上面代码后面的想法是,我可以将遮罩分成4个部分,然后根据该像素找到边界像素- 我在下面标记了4个部分(不好意思的插图表示歉意)-

enter image description here

现在,在确定FOV之外的边界像素之后,我需要用相邻像素(8个邻居)的均值替换它们,在这种情况下,这将是3个像素的均值,因为我们要修改的像素将始终为在角落,如下所示-

enter image description here

所以我的问题是-
1)我的思维过程正确吗?
2)我不知道如何用邻域像素修改像素?
也欢迎任何其他解决方法。

编辑---根据答案解决问题

 while(notroi):
    border_pixels = []
    for i in range(img.size[0]):
        for j in range(img.size[1]):
            if [i,j] not in roi and ([i+1, j] in roi or [i-1, j] in roi or [i, j+1] in roi or [i, j-1] in roi):
                border_pixels.append([i,j])

    for (each_i,each_j) in border_pixels:
        color_sum = 0
        count = 1
        eight_neighbourhood = [[each_i-1,each_j],[each_i+1,each_j],[each_i,each_j-1],[each_i,each_j+1],[each_i-1,each_j-1],[each_i-1,each_j+1],[each_i+1,each_j-1],[each_i+1,each_j+1]]
        for pix_i,pix_j in eight_neighbourhood:
            if (pix_i,pix_j) in roi:
                color_sum+=pixelMap[pix_i,pix_j]
                count+=1
        pixelMap[each_i,each_j]=(color_sum//count)

    for (each_i,each_j) in border_pixels:
        roi.append([each_i,each_j])
        border_pixels.remove([each_i,each_j])
        notroi = notroi-1
        print(notroi)

编辑: 获得的图像-
enter image description here

1 个答案:

答案 0 :(得分:1)

您引用的方法实际上要简单得多。如果您将原始ROI作为每个像素的蒙版,则该算法在伪代码中看起来像这样(为清晰起见,省略了边界处理):

while there are pixels not in the ROI:
    border_pixels = []

    # find the border pixels
    for each pixel p=(i, j) in image
        if p is not in ROI and ((i+1, j) in ROI or (i-1, j) in ROI or (i, j+1) in ROI or (i, j-1) in ROI)):
            add p to border_pixels

    # calculate the averages
    for each pixel p in border_pixels:
        color_sum = (0, 0, 0)
        count = 0
        for each pixel n in 8-neighborhood of p:
            if n in ROI:
                color_sum += color(n)
                count += 1
        color(p) = color_sum / count

    # update the ROI
    for each pixel p=(i, j) in border_pixels:
        set p to be in ROI