[Solved] How to extract the 9 images from following image without text?


In case you don’t like the command-line approach, I have recast my ImageMagick answer in Python. I am quite a beginner in Python so there may be inefficiencies and poor practices in my code, but it works. The technique is exactly the same as the other answer:

  • convert to HSV colourspace, and locate highly saturated (coloured) pixels,
  • run label() from skimage to do “Connected Component” labelling,
  • extract blobs and save as files.

#!/usr/local/bin/python3

import numpy as np
from PIL import Image
from skimage import color
from skimage.measure import label, regionprops

# Load image and convert to RGB discarding any alpha
im=np.array(Image.open('dna.png').convert('RGB'))

# Add 1px black border all around so shapes don't touch edges
blackcanvas=np.zeros((im.shape[0]+2,im.shape[1]+2,3))
blackcanvas[1:-1,1:-1]=im
im=blackcanvas

# Convert to HSV colourspace, discard H, V and find saturated (i.e. brightly coloured) pixels
HSV=color.rgb2hsv(im)
S=HSV[:,:,1]
bw=(S>0.5)*255

# Image is now white blobs on black background, so label() it
label_image=label(bw)

# Iterate through blobs, saving each to disk
i=0
for region in regionprops(label_image):
   if region.area >= 100:
      # Extract rectangle containing blob and save
      name="blob-" + str(i) + ".png"
      minr, minc, maxr, maxc = region.bbox
      Image.fromarray(im[minr:maxr,minc:maxc,:].astype(np.uint8)).save(name)
      i = i + 1

enter image description here

0

solved How to extract the 9 images from following image without text?