简体   繁体   中英

How do you get opencv to fill all touching pixels when using shift for subpixel drawing

I am trying to use opencv to rasterize a polygon. I'm using the shift parameter to get subpixel plotting. It isn't behaving as I had hoped. Here's my code and the output

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)

这是输出

I was hoping that every pixel passed through would be filled. Am I misunderstanding the shift parameter?

Can't work out how to do this using OpenCV but the following code works perfectly using the library 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'))

and without a dependency on GDAL which I was trying to avoid because GDAL is so awful to install.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM