擅长:python、mysql、java
<p>我在这里有错误的补偿问题,在类似的问题张贴解决方案。
所以我做了数学计算,得出了以下有效的解决方案:</p>
<pre><code>def subimage(self,image, center, theta, width, height):
theta *= 3.14159 / 180 # convert to rad
v_x = (cos(theta), sin(theta))
v_y = (-sin(theta), cos(theta))
s_x = center[0] - v_x[0] * ((width-1) / 2) - v_y[0] * ((height-1) / 2)
s_y = center[1] - v_x[1] * ((width-1) / 2) - v_y[1] * ((height-1) / 2)
mapping = np.array([[v_x[0],v_y[0], s_x],
[v_x[1],v_y[1], s_y]])
return cv2.warpAffine(image,mapping,(width, height),flags=cv2.WARP_INVERSE_MAP,borderMode=cv2.BORDER_REPLICATE)
</code></pre>
<p>作为参考,这里有一张图片解释了其背后的数学原理:</p>
<p>请注意</p>
<pre><code>w_dst = width-1
h_dst = height-1
</code></pre>
<p>这是因为最后一个坐标的值是<code>width-1</code>,而不是<code>width</code>;或者<code>height</code>。</p>
<p><img src="https://i.stack.imgur.com/KixsQ.png" alt=""/></p>
<p>如果有关于数学的问题,把它们当作评论来问,我会尽力回答。</p>