I have a big pandas dataframe with columns X,Y, and I. X and Y are pixel coordinates, I is, let's say, an intensity value between 0 and 255 which I want to be shown at the corresponding X and Y position.
There is not an entry for each pixel of the image, so all pixels' I values that are not listed in the dataframe are set to 0.
Therefore, I initialized an two-dimensional array img
with the image dimensions. Then, I already tried something like
img.at[df.X,df.Y] = df.I
which does not work. I think a simple for
-loop can solve this problem, but I wonder if there is a more efficient way to do this (e.g. call a fancy numpy/opencv/whatever function I don't know...).
Easiest way is probably to use scipy.sparse
arrays, as coo_matrix
is built identically to your inputs.
from scipy.sparse import coo_matrix
sparse_image = coo_matrix((df.I, (df.X, df.Y)), shape = image.shape)
image = sparse_image.todense()
coo_matrix
also has some nice functionalities if your I
values are being accumulated before this step.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments