我正在嘗試使用 opencv 柵格化多邊形。我正在使用 shift 引數來獲取亞像素繪圖。它的行為不像我希望的那樣。這是我的代碼和輸出
import cv2
import matplotlib.pyplot as plt
img = np.zeros((4, 4), dtype=np.uint8)
boundary = [np.array([[32, 128], [128, 32], [223, 128], [128, 223], [32, 128]])]
bx, by = zip(*(boundary[0]/2**6))
plt.plot(bx, by)
cv2.fillPoly(img, boundary, color =255, shift=6)
plt.imshow(img)

我希望通過的每個像素都會被填充。我誤解了 shift 引數嗎?
uj5u.com熱心網友回復:
無法解決如何使用 OpenCV 執行此操作,但以下代碼使用庫 geo-rasterize 可以完美運行
from shapely.geometry import Polygon
import numpy as np
from geo_rasterize import rasterize
geom = Polygon([[32, 128], [128, 32], [223, 128], [128, 223], [32, 128]])
print(rasterize(shapes=[geom], foregrounds=[1], output_shape=(4, 4), dtype='uint8', geo_to_pix=(2**-6, 0.0, 0.0, 2**-6, 0.0, 0.0), algorithm='replace'))
并且不依賴于我試圖避免的 GDAL,因為 GDAL 安裝起來非常糟糕。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/506735.html
上一篇:從JPEG影像目錄中洗掉背景
