0

I am working on medical images. I want to segment each image into sub-segments parts according to the labelled letter in each part of the image. I have tried multiple codes and functions but I don't know how can I do that.

Example of my dataset

The original image

enter image description here

I want to segment the image into subsegments as the image.

enter image description here

Trial Code :

def tile(filename, dir_in, dir_out, d):
    name, ext = os.path.splitext(filename)
    img = Image.open(os.path.join(dir_in, filename))
    w, h = img.size
    
    grid = product(range(0, h-h%d, d), range(0, w-w%d, d))
    for i, j in grid:
        box = (j, i, j+d, i+d)
        out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')
        img.crop(box).save(out)
 
dir_in="/content/drive/MyDrive/Images"
dir_out="/content/drive/MyDrive/output_segments"
filename='image 2.jpg'
d=100
output=tile(filename,dir_in,dir_out,d)

The above code perform general segmentation according to a specific tile not according to the labelled letter found in the original image.

Any help would be appreciated.

Thanks in advance.

Eda
  • 565
  • 1
  • 7
  • 18
  • You could use Tesseract OCR to recognize the letter in each cropped image, check the next tutorial: https://nanonets.com/blog/ocr-with-tesseract/ – PepeChuy Jan 23 '23 at 01:05
  • If you don't want to recognize the letter in the corner of each cropped image, you just have to change the line `out = os.path.join(dir_out, f'{name}_{i}_{j}{ext}')` by something like `out = os.path.join(dir_out, f'{labels[j][i]}{ext}')` where `labels = [['A','B','C'], ['D','E','F'], ... ]` – PepeChuy Jan 23 '23 at 01:21

0 Answers0