Recently I wrote the algorithm to quantize an RGB image. Every pixel is represented by an (R,G,B) vector, and quantization codebook is a couple of 3-dimensional vectors. Every pixel of the image needs to be mapped to (say, “replaced by”) the codebook pixel closest in terms of euclidean distance (more exactly, squared euclidean).
I did it as follows:
class EuclideanMetric(DistanceMetric):
def __call__(self, x, y):
d = x - y
return sqrt(sum(d * d, -1))
class Quantizer(object):
def __init__(self, codebook, distanceMetric = EuclideanMetric()):
self._codebook = codebook
self._distMetric = distanceMetric
def quantize(self, imageArray):
quantizedRaster = zeros(imageArray.shape)
X = quantizedRaster.shape[0]
Y = quantizedRaster.shape[1]
for i in xrange(0, X):
print i
for j in xrange(0, Y):
dist = self._distMetric(imageArray[i,j], self._codebook)
code = argmin(dist)
quantizedRaster[i,j] = self._codebook[code]
return quantizedRaster
…and it works awfully, almost 800 seconds on my Pentium Core Duo 2.2 GHz, 4 Gigs of memory and an image of 2600*2700 pixels:(
Is there a way to somewhat optimize this? Maybe the other algorithm or some Python-specific optimizations.
UPD: I tried to use the squared euclidean and still get an enormous time.
One simple optimization is to drop the
sqrtcall. x is monotonic with sqrt(x), and since you don’t need the actual distance, just the min distance, use x^2 instead. Should help a bit since sqrt is expensive.This trick is used a lot when working with distances. For instance, if you have a distance threshold, you can use threshold^2 and drop the sqrt in the distance calculation. Really, the sqrt is only necessary when absolute distance is needed. For relative distances, drop the sqrt.
Update: an algorithmic change is probably needed then. Right now you are comparing every codebook vector to every pixel. It would speed things up to reduce the number of distance calculations.
You might do better using a kd-tree for this, which will reduce the search for each pixel from O(codebook) to O(log(codebook)). I’ve never done this in python, but some googling gave an implementation that might work here.