I have images like this:

where all objects are connected through a horizontal line. all the symbols have different widths so can't extract all shapes using a fixed width or splitting points like when we split an array.
In this image, there are 4 symbols but in some other cases it can be more or less than 4 so here also I can't perform something like "4 symbols so divide the input image width vertically into 4 different widths. so I don't know how to extract whatever symbols the image might have efficiently like this output I want
.
Can you guys help me with this?

to

CodePudding user response:
with
col =
130 243 351
CodePudding user response:
import urllib.request
from PIL import Image
from scipy import signal
import numpy as np
from matplotlib import pyplot as plt
urllib.request.urlretrieve(
'https://i.stack.imgur.com/iMKIP.jpg',
"img.png")
img = Image.open("img.png")
img.show()
arr = (np.array(img)<(255)/2) #I'm considering black parts as filled
plt.imshow(arr)
plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(arr.sum(axis=0))
plt.xlim(0,arr.shape[1])
Then, in order to find local min:
x_lines = signal.find_peaks(-arr.sum(axis=0))[0]
plt.imshow(arr, aspect="auto")
plt.vlines(x_lines, 0, arr.shape[0], color="r")
Finally if we consider a threshold (for example 50 filled pixel in columns):
threshold = 50
x_lines = signal.find_peaks(-arr.sum(axis=0))[0]
x_lines = x_lines[arr.sum(axis=0)[x_lines]<threshold]
plt.imshow(arr, aspect="auto")
plt.vlines(x_lines, 0, arr.shape[0], color="r")






