Beginner
Beginner

Reputation: 749

Artificially increasing the region of interest of an image

I am solving an image segmentation problem. To increase the accuracy of the model, I came across the following preprocessing step-

First, the set of pixels of the exterior border of the ROI is de termined, i.e., pixels that are outside the ROI and are neighbors (using four-neighborhood) to pixels inside it. Then, each pixel value of this set is replaced with the mean value of its neighbors (this time using eight-neighborhood) inside the ROI. Finally, the ROI is expanded by inclusion of this altered set of pixels. This process is repeated and can be seen as artificially increasing the ROI.

The image after applying the following step is below- enter image description here

The image I am applying the above step is below- enter image description here

On asking the question here, I got the initial approach to take the black area surrounding my image and use it as a alpha mask.
My approach to solve this problem is to find all the boundary pixels(outside the FOV) which is the first black pixels outside the red part which I obtained using the below code-

    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])

My idea behind the above code is, I can divide the mask in 4 sections and then can find the boundary pixels according to that- I have marked 4 sections below(Apologies for the poor illustration)-

enter image description here

Now, after determining the boundary pixels outside the FOV, I need to replace them with mean of neighbour pixels (8-neighbours) which will be the mean of 3 pixels in this case as the pixel which we are modifying will always be in the corner as shown below-

enter image description here

So my questions are-
1)Is my thought process correct?
2)I don't know how to modify the pixels with neighbourhood pixels?
Any other approach to solve it is also welcomed.

Edit---Solution to the problem according to the answer

 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)

Edit: Image obtained-
enter image description here

Upvotes: 0

Views: 314

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32607

The approach you cited is actually much simpler. If you have the original ROI as a per-pixel mask, then the algorithm looks like this in pseudo-code (boundary-handling omitted for clarity):

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

Upvotes: 1

Related Questions