Below is my code.
Mat img = imread(“original.jpg”);
auto img_tensor = torch::from_blob(img.data, {img.rows, img.cols, img.channels()}, at::kByte);
//Save out orignial tensor to check
Mat saveOutOriginal(img_tensor.sizes()[0], img_tensor.sizes()[1], CV_8UC3);
memcpy((void*)saveOutOriginal.data, img_tensor.data_ptr(), sizeof(torch::kU8)img_tensor.numel());
imwrite(“original_copy.jpg”, saveOutOriginal);
//Reshape to do interpolation
img_tensor = img_tensor.permute({2,0,1});
img_tensor = img_tensor.unsqueeze(0);
img_tensor = img_tensor.to(torch::kFloat);
auto resized_tensor = functional::interpolate(img_tensor, functional::InterpolateFuncOptions().size(vector<int64_t>({512,512})).mode(torch::kBilinear));
resized_tensor = resized_tensor.squeeze().detach().permute({1,2,0});
resized_tensor = resized_tensor.to(torch::kU8);
//Save out resized result to check
Mat checkResized(resized_tensor.sizes()[0], resized_tensor.sizes()[0], CV_8UC3);
memcpy((void)checkResized.data, resized_tensor.data_ptr(), sizeof(torch::kU8)*resized_tensor.numel());
imwrite(“resized.jpg”, checkResized);
I check the unchanged tensor “original.jpg”. It’s the same with input image.
The result is as expected.
But after resizing the tensor and save it out as a Mat.
Why it becomes 9 times grid image (see below) ?
How can I get output with only one resized image (512x512) ?
original_copy.jpg

resized.jpg
resized512×512 65.2 KB