raaj
raaj

Reputation: 3291

Converting an RGB to BGR for Torch Tensor in C++

I have a torch tensor of size H, W, 3. I can easily swap this in Pytorch with image[:, :, [2,1,0]]

However, in C++ Libtorch I cannot figure it out:

I tried:

std::vector<long> index_vector = {0,1,2};
auto index_tensor = torch::from_blob((long*)(index_vector.data()), {3}, torch::kLong);
std::vector<at::indexing::TensorIndex> index;
index = {"...", "...", index_tensor};
img_tensor = img_tensor.index(index);

But it does not work

Upvotes: 2

Views: 2626

Answers (1)

Shai
Shai

Reputation: 114786

I would try using flip on the third dimension:

img_tensor = img_tensor.flip(2);

Upvotes: 2

Related Questions