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

Time:09-14

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)

this is the output

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

CodePudding user response:

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.

  • Related