If rgb2something can't be vectorized, and you want to process one typical image, then you can get a decent speedup using np.unique. Original answer (perhaps still useful to some) That said the example may overestimate lookup cost, because random pixels are presumably less cache friendly than natural images. We can see that the best lookup beats the best on-the-fly computation by a whisker. Lookup using fancy indexing 0.064 seconds Process on the fly (no lookup) 0.127 seconds Resv = calculate_distance_vectorized(lutv, rgb)Īssert np.allclose(res, resotf) and np.allclose(res, resv) \Īnd np.allclose(res, resbla) and np.allclose(lutv, lutb) Td.append((time.time(), 'lookup using fancy indexing')) Td.append((time.time(), 'process on the fly (blas)')) Td.append((time.time(), 'process on the fly (no lookup)')) Td.append((time.time(), 'lookup using np.take')) Td.append((time.time(), 'create lut using blas')) Td.append((time.time(), 'create lut vectorized')) We only compare a random sample, using the original codeįor r, g, b in np.random.randint(0, 256, (1000, 3)):Īssert np.isclose(lut, rgb2something(r, g, b)) """Because the original lut generator is excruciatingly slow, Return rgb2something_vectorized(np.transpose(np.indices((256, 256, 256))))ĭef calculate_distance_vectorized(lut, input_image): class Full_Model(Model):Ĭh = np.linalg.cholesky(Model.inverse_pooled_covariance) I've also added a random row to x to make its covariance matrix non singular. Unless you are very patient you probably also want to comment out the lut = generate_lut() and result = calculate_distance(lut, rgb) lines and all references to cv2. Just add it to the end of OP's code (under EDIT 2). With blas dtrmm we can exploit its triangular structureĪnd here is the code. (2) use proper lookup, i.e fancy indexing, not np.take (1) vectorize, vectorize! It is not so difficult to vectorize essentially everything in this code. There are several straightforward and very effective optimizations: Return np.take(lut, input_image + (input_image * 256) + (input_image * 65536)) Mah = g.T.dot(Model.inverse_pooled_covariance).dot(g) ** 0.5ĭef calculate_distance(lut, input_image): #Calculates Mahalanobis Distance between pixel and model X Inverse_pooled_covariance = np.linalg.inv(pooled_covariance) Pooled_covariance = (m / q * x_covariance) # + (n / q * y_cov) -< Always 0 for a single point N = 1 # Only ever comparing to a single pixel X_covariance = x_centered.T.dot(x_centered) / len(x_centered) Mah = g.T.dot(inv_pool_cov).dot(g) ** 0.5įull working code sample of what I'm trying to achieve, I am using OpenCV so any OpenCV approaches such as Apply LUT are welcome, as are C/C++ approaches: import matplotlib.pyplot as plt Pool_cov = (m / q * x_cov) + (n / q * y_cov) Y_cov = y_centered.T.dot(y_centered) / len(y_centered) Rgb2something(r, g, b) looks like this: def rgb2something(r, g, b): How can I increase the performance of this? EDIT In 2 dimensions it's rather fast, however in 3 dimensions (r, g and b) it's slow. However both generating the LUT and calculating the result is still slow. Lut = rgb2something(r, g, b)Īnd to convert RGB to the transformed probability image: result = np.take(lut, r_channel + (g_channel * 256) + (b_channel * 65536)) To generate the LUT (Look up table): import numpy as np I have tried the following approach to speed the conversion up. EDIT shader_lut_doc_define.I have a function I will call 'rgb2something' that transforms RGB data into a single value (a probability), looping over each pixel in the input RGB data turns out to be rather slow. Run the example by creating an instance of the object at the IDL command prompt using oLUTshader=OBJ_NEW('shader_lut_doc') or view the file in an IDL Editor window by entering. See shader_lut_doc_define.pro, located in the examples/doc/shaders subdirectory of the IDL distribution, for the complete, working example. Adjusting data ranges such as converting an 11-bit image to 8-bits for display (see “High Precision Images” for more information).Adjusting image brightness, gamma, contrast, color balance and other settings.For example, if your image is 8-bit greyscale and you need to apply an expensive function to each pixel it is normally more efficient to pass each of the 256 greyscale values to the function and store the result in a 256 entry LUT used for drawing. Optimizing the evaluation of expensive functions.LUTs are useful for a number of tasks including: The IMAGE_1D property on the IDLgrImage object lets you load color lookup table (LUT) values into a texture map and pass the LUT to a shader program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |