I’m creating a tkinter user interface to display several images which a user will manipulate (translate and rotate) over each other. As the user is manipulating the images I need to calculate the new position of the images, draw the resultant image and show it in the interface. This will need to happen at approx. 30Hz (there’s no specific speed requirement, it just shouldn’t be slow / juddering).
The data from the images has come from some csv files which I’ve imported and stored in a single numpy array. I’ve then created a subset of this array. This new array, points2
, has the shape (102118, 5)
and the columns are X, Y, R, G, B. The RGB array I’m creating, called rgb_array
, has the shape (471, 255, 3)
. The data I’m importing doesn’t have values for all 471 x 255 pixels so when I try to create the rgb_array I get the following error…
ValueError: shape mismatch: value array of shape (3,102118) could not be broadcast to indexing result of shape (102118,3)
points2 = points[:, [1,2,4,5,6]]
rgb_array = np.zeros((471,255,3), dtype=np.uint8)
rgb_array[points2[:, 0].astype('int'), points2[:, 1].astype('int')] = [points2[:, 2].astype('int'), points2[:, 3].astype('int'), points2[:, 4].astype('int')]
img = Image.fromarray(rgb_array, 'RGB')
How can I quickly reshape the points2
array to an RGB array of the correct size given that some of the data is missing?
4
As @Paul already commented, you problem looks to be related to a simple transpose problem. To better inspect it, you should add your actual data (or part of it), as well as the code you use for importing.
Indeed, I tried to reproduce your case with the following code:
import numpy as np
import cv2
# Create toy example data
POINTS_N = 102118
IMG_ROWS = 471
IMG_COLS = 255
np.random.seed(123)
# Populate the full vector with 471x255 datapoints
points_full = np.empty((IMG_ROWS*IMG_COLS, 5))
x, y = np.meshgrid(np.arange(IMG_ROWS),
np.arange(IMG_COLS)
)
print(x.flatten().shape, x.flatten())
points_full[:, 0] = x.flatten()
print(points_full[:, 0].shape, points_full[:, 0])
points_full[:, 1] = y.flatten()
points_full[:, 2:] = [255, 0, 255] #np.random.rand(IMG_ROWS*IMG_COLS, 3) * 255
# Draw POINTS_N datapoints from the full vector to simulate the actual data
points = points_full[np.random.choice(len(points_full),
size = POINTS_N,
replace = False
)]
# 'Import' and plot the data
points = points.astype(np.int16)
rgb_array = np.zeros((IMG_ROWS,IMG_COLS,3), dtype=np.uint8)
rgb_array[points[:, 0], points[:, 1]] = points[:, 2:]
cv2.imshow('Image with missing values', rgb_array)
cv2.waitKey(0)
cv2.destroyAllWindows()
and it results in
where you can see the missing values in black. If this is what you are looking for, you can try to build on this example to get to a solution.
Let us know!