Map colors in image to closest member of a list of colors, in Python

2024/10/9 14:27:19

I have a list of 19 colors, which is a numpy array of size (19,3):

colors = np.array([[0, 0, 0], 
[0, 0, 255], 
[255, 0, 0], 
[150, 30, 150], 
[255, 65, 255], 
[150, 80, 0], 
[170, 120, 65], 
[125, 125, 125], 
[255, 255, 0], 
[0, 255, 255], 
[255, 150, 0], 
[255, 225, 120], 
[255, 125, 125], 
[200, 100, 100], 
[0, 255, 0], 
[0, 150, 80], 
[215, 175, 125], 
[220, 180, 210], 
[125, 125, 255]
])

Now I have an image (numpy array of size (1024,1024,3)), with colors that are somewhat close or equal to all of the colors defined above. However, my program requires that the image can only contain the colors above and not a closeby color, so I need to convert the color of each pixel in the array to the closest color of the 19 colors.

I saw a function to find the closest color from a set of colors that uses only Numpy (which works perfectly) here Python - Find the closest color to a color, from giving list of colors:

def closest_color(colors,color):colors = np.array(colors)color = np.array(color)distances = np.sqrt(np.sum((colors-color)**2,axis=1))index_of_smallest = np.where(distances==np.amin(distances))smallest_distance = colors[index_of_smallest]
return smallest_distance

With this function I can find the closest color to a single color from a predefined list, but in my problem I do not have a single color that I would like to change but a whole image (1024 x 1024 pixels of colors). What is the most efficient way to leverage this function (or using any better function) that uses numpy only to solve my problem. I would like to reduce the amount of for loops as much as possible, since I need process 30,000 images of size 1024 x 1024 in total.

Thanks!

Answer

We can use Cython-powered kd-tree for quick nearest-neighbor lookup and hence achieve our classification/bucketing -

from scipy.spatial import cKDTree# Input image : img
out_img = colors[cKDTree(colors).query(img,k=1)[1]]
https://en.xdnf.cn/q/70005.html

Related Q&A

Storing a file in the clipboard in python

Is there a way to use the win32clipboard module to store a reference to a file in the windows clipboard in python. My goal is to paste an image in a way that allows transparency. If I drag and drop a…

retrieve intermediate features from a pipeline in Scikit (Python)

I am using a pipeline very similar to the one given in this example : >>> text_clf = Pipeline([(vect, CountVectorizer()), ... (tfidf, TfidfTransformer()), ... …

Any way to do integer division in sympy?

I have a very long expression that I think can be simplified, and I thought sympy would be the perfect way to do it. Unfortunately the formula relies on a couple of integer divides, and I cant find any…

Scrapy LinkExtractor - Limit the number of pages crawled per URL

I am trying to limit the number of crawled pages per URL in a CrawlSpider in Scrapy. I have a list of start_urls and I want to set a limit on the numbers pages are being crawled in each URL. Once the l…

Python Invalid format string [duplicate]

This question already has answers here:Python time formatting different in Windows(3 answers)Closed 9 years ago.I am trying to print the date in the following format using strftime: 06-03-2007 05:40PMI…

Python template safe substitution with the custom double-braces format

I am trying to substitute variables in the format {{var}} with Pythons Template. from string import Templateclass CustomTemplate(Template):delimiter = {{pattern = r\{\{(?:(?P<escaped>\{\{)|(?P…

Emit signal in standard python thread

I have a threaded application where I do have a network thread. The UI-part passes a callback to this thread. The thread is a normal python thread - its NO QThread.Is it possible to emit PyQT Slot with…

Sqlalchemy from_statement() cannot locate column

I am following the sqlalchemy tutorial in http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.htmlNevertheless, instead of using a SQLite backend, I am using MySQL. The problem is that when I try to exe…

Python - how to check if weak reference is still available

I am passing some weakrefs from Python into C++ class, but C++ destructors are actively trying to access the ref when the real object is already dead, obviously it crashes...Is there any Python C/API a…

Django using locals() [duplicate]

This question already has answers here:Django template and the locals trick(8 answers)Closed 5 years ago.I am beginner in web development with Django. I have noticed that the locals() function is used …