SimSIMD icon indicating copy to clipboard operation
SimSIMD copied to clipboard

Feature: Faster version of cv2.mixChannels

Open ternaus opened this issue 11 months ago • 0 comments

Describe what you are looking for

There is a transform in Albumentations, called ChannelShuffle, it takes numpy array and shuffles channels.

Could be done in cv2:

def channel_shuffle(img: np.ndarray, channels_shuffled: np.ndarray) -> np.ndarray:    
    from_to = []
    for i, j in enumerate(channels_shuffled):
        from_to.extend([i, j])
    cv2.mixChannels([img], [img], from_to)
    return img

in numpy

def channel_shuffle(img: np.ndarray, channels_shuffled: np.ndarray) -> np.ndarray:
    return img[..., channels_shuffled]

The request it have similar operation in simsimd that can:

  • runs inplace (cv2 version runs inplace)
  • can work with any shape, say (height, width, num_channels) or (depth, height, width, num, channels), or (num_volumes, depth, height, width, num_channels)) (numpy allows this, cv2 does not, but it could be achieved with reshape)
  • The most important: Run faster, as right now, benchmark shows that torchvision performs this operation on tensors faster than OpenCV on numpy arrays.

Can you contribute to the implementation?

  • [ ] I can contribute

Is your feature request specific to a certain interface?

Python bindings

Contact Details

No response

Is there an existing issue for this?

  • [x] I have searched the existing issues

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

ternaus avatar Jan 24 '25 16:01 ternaus