I have let's say the following image:
I've figured out how to get each line using EASYOCR. However, I want to know what color is of the text. I've tried to apply a threshold and use bitmasking, but what would I do if the background color is of anything other color than white?
As of the below comment, I have changed my code to this:
def dominant_colors(image,n): # PIL image input
image = image.resize((150, 150)) # optional, to reduce time
ar = np.asarray(image)
shape = ar.shape
ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)
kmeans = sklearn.cluster.MiniBatchKMeans(
n_clusters=n,
init="k-means++",
max_iter=20,
random_state=1000
).fit(ar)
codes = kmeans.cluster_centers_
vecs, _dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, _bins = np.histogram(vecs, len(codes)) # count occurrences
colors = []
for index in np.argsort(counts)[::-1]:
# if index!=3:
colors.append(tuple([int(code) for code in codes[index]]))
return colors
dc = dominant_colors(Image.open('./mix.png'),2)
Now, it is working, however it is highly dependent on the image provided. When the FONTS AND WORDS are different, the results are quite different.
On getting the result, and on drawing back on image, it can clearly be seen that for some part, the detected is incorrect


