擅长:python、mysql、java
<p>要裁剪图像,我使用<code>mask</code>和<code>cv2.bitwise_and()</code>。在</p>
<p>源图像:</p>
<p><a href="https://i.stack.imgur.com/E5W69.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E5W69.png" alt="source image"/></a></p>
<p>面具:</p>
<pre><code># Create a mask image with a triangle on it
y,x,_ = img.shape
mask = np.zeros((y,x), np.uint8)
triangle_cnt = np.array( [(x,y), (x,0), (0,y)] )
cv2.drawContours(mask, [triangle_cnt], 0, 255, -1)
</code></pre>
<p><a href="https://i.stack.imgur.com/uF8ed.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uF8ed.png" alt="mask image"/></a></p>
<p>输出:</p>
^{pr2}$
<p><a href="https://i.stack.imgur.com/7dlgX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7dlgX.png" alt="output"/></a></p>