Converting a torch tensor into a jpg Image using C++ ---Libtorch

        torch::Tensor out_tensor = module.forward({ input_tensor }).toTensor();
		auto img_output = out_tensor;
		auto img_uint = img_output.toType(torch::kU8);
		auto img = cv::Mat(input_image.rows, input_image.cols, CV_8UC3, img_uint.data_ptr());
		cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
		std::cout << "Tensor converetd to Image!!" << std::endl;
		cv::imwrite("./Images/new_img.jpg", img);

I can’t get the desired output. Instead I get a noisy grayscale image.

@tom Do you have any idea on this issue?

I’m not Tom, but think you might be missing a permutation in the img_output. :wink:
OpenCV uses the channels-last memory layout, while your tensor might be in channels-first, so you would need to permute the dimensions before writing it out as the image.

1 Like

Great.I solved that and thanks