Arnav
Arnav

Reputation: 314

Find the color of text in an image using opencv

I have let's say the following image:

enter image description here

enter image description here

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

enter image description here

Upvotes: 0

Views: 2015

Answers (1)

Hisan
Hisan

Reputation: 2655

I came across this problem almost 2 years ago and here's what I did to solve the issue.

I used kmeans on image to cluster it into k = 2 clusters. The output would be 2 most prominent clusters namely the background and the foreground. You can convert this into a binary image by using open-cv binary thresholding. Now at this point you don't know which one is foreground and which one is background, so you use pixel count on the binary image. In my case the background always had more pixels so that was really easy to distinguish the text (foreground) from the background.

With this method, it doesn't matter which color is the background and which color is the foreground text, it also doesn't matter if you have minor noise cause it would cope up with it.

This technique solved it entirely for me, I hope it does the same for you or at least give you some leads.

Upvotes: 2

Related Questions