I am solving a problem of increasing the ROI of a retinal image using the below algorithm-
First, the set of pixels of the exterior border of the ROI is determined, i.e., pixels that are outside the ROI and are neighbors (using four-neighbourhood) to pixels inside it. Then, each pixel value of this set is replaced with the mean value of its neighbors (this time using eight-neighbourhood) inside the ROI. Finally, the ROI is expanded by the inclusion of this altered set of pixels. This process is repeated and can be seen as artificially increasing the ROI.
Pseudocode for the above approach which I got from here is below-
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
and my implementation for the above pseudocode is below-
img = Image.open(path_dir) pixelMap = img.load() @jit def roifun(img,pixelMap): roi =  for i in range(img.size): for j in range(img.size): if pixelMap[i,j] == 255: roi.append([i,j]) return roi roi= roifun(img,pixelMap) notroi = img.size*img.size - len(roi) @jit def border_enhance(img,pixelMap,roi,notroi): while(notroi): border_pixels =  for i in range(img.size): for j in range(img.size): 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) border_enhance(img,pixelMap,roi,notroi)
I run this code for image of dimension 50×50 and it is running correctly but for an image of larger size like 512×512, it is taking too long a time. I also tried modifying it using numba but it gave me plenty of warnings and then taking the same time as before.