How to do 3D affine transform where the shape of the output is different than the input

opencv isn’t made for voxel data. one of the fundamental assumptions is that stuff is flat (a picture) and the third dimension is “attributes” (color).

the cv::Mat type, in C++, can carry a limited number of channels (512) but that still doesn’t mean the APIs (warpAffine) can just interpret data as multi-dimensional.

if you’re looking for speed, either write your kernel in python and slap numba on it, or some other thing like it.

or write your kernel as opencl or some other GPU-type thing.

GPUs are good at sampling in voxel data. trilinear interpolation is commonly available.

if you’re trying to visualize voxel data volumetrically, take a class on the subject. there are approaches that run well even on GPUs that are over a decade old.