大家好,正如標題所說,有沒有辦法做到這一點?例如,我想裁剪影像的第四象限,其他區域將變為黑色,同時保持其原始大小。目前,我正在獲取影像的中心寬度和高度,然后訪問像素:
裁剪 = I[centerHeight:,centerWidth:]
但這只是存盤第四象限裁剪的影像。謝謝!
uj5u.com熱心網友回復:
我不認為 OpenCV 中有一個函式可以做到這一點。此功能將解決您的問題。
import numpy as np
def crop_image(img, cx, cy, w, h):
"""
args:
cx: x coordinate of center
cy: y coordinate of center
w: width of crop
h: height of crop
"""
result = np.zeros(img.shape, dtype=np.uint8)
result[cx - w//2 :cx w//2, cy - h//2:cy h//2] = img[cx - w//2 :cx w//2, cy - h//2:cy h//2]
return result
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/313194.html
