The affine transformation parameters obtained by cv2.getaffinetransform are not correct

username: 111220
cooked: <p>I am training a network similar to Spatial Transformer Networks, which is used to estimate affine transformation parameters according to the incoming heatmap. The labels are the parameters of affine transformation through the coordinates of the target box. So I tried cv2.getaffinetransform to get the parameters of the corresponding affine transformation, but it seemed that the final result was not ideal.</p>
<h1><a name="code-1" class="anchor" href="#code-1"></a>Code:</h1>
<p>This code is to display the bbox and (minx,miny,maxx,maxy) is the coordinates of the bbox</p>
<pre><code class="lang-auto">miny=127
minx=191
maxx=351
maxy=351
print(miny,minx,maxx,maxy)
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(image)
rect=patches.Rectangle((minx, miny), maxx - minx, maxy - miny,
                              fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/d/1d488063bfc2ad5432057b883a630eddc9291e29.jpeg" alt="1" data-base62-sha1="4b3bbBkWjJ1nywcDF3iyoem8muJ" width="366" height="361"></p>
<p>And I get the parameters :</p>
<pre><code class="lang-auto">points1=np.float32([[191,127],[191,351],[351,351]])
points2=np.float32([[0,0],[0,448],[448,448]])
M=cv2.getAffineTransform(points2,points1)
M

</code></pre>
<p>Finally, I use the steps of affine transformation in Spatial Transformer Networks to transform：</p>
<pre><code class="lang-auto">theta=torch.from_numpy(M).float()
grid=F.affine_grid(theta.unsqueeze(0),tensor_image.shape)
grid=grid.cuda()
output=F.grid_sample(tensor_image,grid)

</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/2/627552daaf8e5375ae884e86ee8cdd5580bd947a.png" alt="下载" data-base62-sha1="e309IYY6A4g2Jhd2AetzYDHifTI" width="257" height="252"></p>
<p>You can see that the affine transformation is not exactly the region of the target box</p>
<p>Is there something wrong with my code？</p>

username: ptrblck
cooked: <p>I guess there might be a mismatch between the x and y coordinates in the image coordinate system and the rows and columns in <code>affine_grid</code>. Could you try to swap the coordinates of the axes and rerun the code?</p>

username: 111220
cooked: <p>I’m sorry to reply you so late. I tried to exchange the axis, but the final result was not very good either</p>
<pre><code class="lang-auto">points1=np.float32([[127,191],[351,191],[351,351]])
points2=np.float32([[0,0],[448,0],[448,448]])
M=cv2.getAffineTransform(points2,points1)
M

</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/d/adc4086f26aeed8179a15d808e76d9cc171df82f.png" alt="下载 (1)" data-base62-sha1="oNcBgo4IqcgUCiEaAGWS90gmkOP" width="257" height="252"></p>

username: 111220
cooked: <p>I wonder if it’s a matter of paneling, because the paneling in M that cv2 gets is the pixel value, and the paneling in affine_grid is supposed to be proportional, so I deal with paneling in this way</p>
<p>The image shape is 448*448</p>
<pre><code class="lang-auto">M[0][2]=M[0][2]/448
M[1][2]=M[1][2]/448
</code></pre>