0
votes

I have this retinal scanned image with a black rectangular box around this. How do I remove that portion alone using python or opencv since I have to carry out the operation for hundreds of images

The resultant image should look like this:

The cropped final and required image,basically after removing the bounding black box

1
It would be helpful if you can also provide an example of what you want to have as a result of removing the bounding box. - mahesh
I want the final image to be just the retinal eye(circular one) and the entire black color bounding the circle to be eliminated. - chatbot_chakra
@chatbot_chakra So you just want to change the black portion outside the retina's disc to white? I'd suggest flood fill. - beaker
It's not white actually. It's just the circle which i want. This is for feeding the images into a neural network , so the purpose is to feed only the useful part and leaving out the rest. - chatbot_chakra

1 Answers

0
votes

Well, here is something to get started:

import cv2

threshold = 25

img = cv2.imread('D:\\img.jpg', 0) # load grayscale version

# the indeces where the useful region starts and ends
hStrart = 0
hEnd = img.shape[0]
vStart = 0
vEnd = img.shape[1]

# get row and column maxes for each row and column
hMax = img.max(1)
vMax = img.max(0)

hDone_flag = False
vDone_flag = False

# go through the list of max and begin where the pixel value is greater 
# than the threshold
for i in range(hMax.size):
    if not hDone_flag:
        if hMax[i] > threshold:
            hStart = i
            hDone_flag = True

    if hDone_flag:
        if hMax[i] < threshold:
            hEnd = i
            break

for i in range(vMax.size):
    if not vDone_flag:
        if vMax[i] > threshold:
            vStart = i
            vDone_flag = True

    if vDone_flag:
        if vMax[i] < threshold:
            vEnd = i
            break

# load the color image and choose only the useful area from it
img2 = (cv2.imread('D:\\img.jpg'))[hStart:hEnd, vStart:vEnd,:]

# write the cropped image
cv2.imwrite("D:\\clipped.jpg", img2)

It may not be most elegant or the most efficient, but it does the job and you can get started with this. Maybe you can look up documentation and improve this.