Why saved out (to Mat) interpolated tensor is a grid image

username: Liao_Phil
cooked: <p>Below is my code.</p>
<p>Mat img = imread(“original.jpg”);<br>
auto img_tensor = torch::from_blob(img.data, {img.rows, img.cols, img.channels()}, at::kByte);<br>
//Save out orignial tensor to check<br>
Mat saveOutOriginal(img_tensor.sizes()[0], img_tensor.sizes()[1], CV_8UC3);<br>
memcpy((void*)saveOutOriginal.data, img_tensor.data_ptr(), sizeof(torch::kU8)<em>img_tensor.numel());<br>
imwrite(“original_copy.jpg”, saveOutOriginal);<br>
//Reshape to do interpolation<br>
img_tensor = img_tensor.permute({2,0,1});<br>
img_tensor = img_tensor.unsqueeze(0);<br>
img_tensor = img_tensor.to(torch::kFloat);<br>
auto resized_tensor = functional::interpolate(img_tensor, functional::InterpolateFuncOptions().size(vector&lt;int64_t&gt;({512,512})).mode(torch::kBilinear));<br>
resized_tensor = resized_tensor.squeeze().detach().permute({1,2,0});<br>
resized_tensor = resized_tensor.to(torch::kU8);<br>
//Save out resized result to check<br>
Mat checkResized(resized_tensor.sizes()[0], resized_tensor.sizes()[0], CV_8UC3);<br>
memcpy((void</em>)checkResized.data, resized_tensor.data_ptr(), sizeof(torch::kU8)*resized_tensor.numel());<br>
imwrite(“resized.jpg”, checkResized);</p>
<p>I check the unchanged tensor “original.jpg”. It’s the same with input image.<br>
The result is as expected.<br>
But after resizing the tensor and save it out as a Mat.<br>
Why it becomes 9 times grid image (see below) ?<br>
How can I get output with only one resized image (512x512) ?</p>
<p>original_copy.jpg<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c2fb0135804401f90ab3ad4ee922f3046bed2a4.jpeg" alt="original_copy" data-base62-sha1="6iTb7lhKmXYRHMReTjk53x8wMGo" width="352" height="240"></p>
<p>resized.jpg<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/f/1f8ca71f95ce18a05d956f77076d4668519a5cb5.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1f8ca71f95ce18a05d956f77076d4668519a5cb5" title="resized"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/f/1f8ca71f95ce18a05d956f77076d4668519a5cb5_2_500x500.jpeg" alt="resized" data-base62-sha1="4v69egEeMz5HgV4k26StCuvyZzD" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/f/1f8ca71f95ce18a05d956f77076d4668519a5cb5_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/1/f/1f8ca71f95ce18a05d956f77076d4668519a5cb5.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/f/1f8ca71f95ce18a05d956f77076d4668519a5cb5.jpeg 2x" data-dominant-color="9B9B9B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">resized</span><span class="informations">512×512 65.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Liao_Phil
cooked: <p>I dont know why, but I solved my problem by spliting the saved mat and merging .</p>
<p>int width = 512;<br>
int height = 512;<br>
uchar* data_encode = checkResized.data;<br>
cv::Mat b(height,width,CV_8UC1,&amp;data_encode[0]);<br>
cv::Mat g(height,width,CV_8UC1,&amp;data_encode[(height<em>width)<em>sizeof(uchar)]);<br>
cv::Mat r(height,width,CV_8UC1,&amp;data_encode[(2</em>height</em>width)*sizeof(uchar)]);<br>
test.push_back(b);<br>
test.push_back(g);<br>
test.push_back( r );<br>
cv::Mat out ;<br>
cv::merge(test,out);</p>
<p>The final output out is correct.<br>
For your reference.</p>