Artificially increase 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 algorithm is below-

Can anyone please help me how to approach this problem, I am pretty much clue-less how to do it, how to detect the boundary of the image.My image is of the following type-

Any help in solving this kind of problem is highly appreciated.
It looks like your image will always have the same black area due to the mechanism used to take the picture. If that's the case then you don't need to detect the edges. Instead, you can create a static mask ahead of time. The mask can be a single channel image that's white where the eye image is and black elsewhere. You can use that 1 channel mask as the alpha mask for the RGB image you have.

If the image can move around, it looks to me that you could threshold the red channel and say any red value under 0.1, say, is outside of the eye. Another possibility is just let the user select a circular region for the big circle, and then let them select a smaller one for the bump.

To expand the ROI once you've got the mask, you can go pixel by pixel and if the mask is black, follow the vector from the current pixel towards the center of the image until you hit a non-black pixel. When you hit one, just copy that pixel to the one you started with. Do that with each pixel that's not in the eye portion of the image.

EDIT: Given that the mask is static there's an even easier way. In your other question I pointed out that the center of the larger circle was at approximately (279, 283.5) and its radius was approximately 265. So what you could do is simply iterate over all the pixels in the image and if they're farther than 265 pixels away from the center, replace them with the pixel that is 265 pixels away from the center in the same direction. Something like this untested pseudocode:

float centerX = 279;
float centerY = 283.5;
float radius = 265;
for (int row = 0; row &lt; MAX_ROWS; row++)
{
    for (int col = 0; col &lt; MAX_COLS; col++)
    {
        float deltaX = col - centerX;
        float deltaY = row - centerY;
        float dist = sqrt(deltaX * deltaX + deltaY * deltaY);
        if (dist &gt; radius)
        {
            float dirX = deltaX / dist;
            float dirY = delatY / dist;
            float edgeX = centerX + radius * dirX;
            float edgeY = centerY + radius * dirY;
            SetPixel(col, row, GetPixel(edgeX, edgeY));
        }
    }
}


Note that the smaller circle presents another issue. You can test whether a given pixel is in the big circle as above, and if not, then see what the distance to the smaller circle is. If that distance is smaller than the distance to the larger circle, then get the pixel from the smaller circle rather than the larger one.