OpenStitching / lir

Largest Interior/Inscribed Rectangle implementation in Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using lir with an imported image

jeffemandel opened this issue · comments

The documentation doesn't discuss how to create the grid from an image. It took me a few tries to work this out

mask = np.full(img.shape, 0, dtype=np.uint8)
cv.drawContours(mask,[contour],contourIdx=-1, color=(255,255,255),thickness=-1)
grid = np.array(mask[:,:,1]) > 0
rect = lir.lir(grid)

This takes my original image, makes a copy that is all black, and fills it with the contour in white. We now convert the first dimension from uint8 to bool. This is needed because lir expects array(bool, 2d, C), not array(uint8, 3d, C). There may be other ways to do this, but this may help anyone stuck with:
TypeError: No matching definition for argument type(s) array(uint8, 3d, C)

Thanks for pointing this out! Nice approach. I'll have a look on how to add it to the docs.

with version 0.2.0 you can just pass in the contour as polygon:

contours, _ = cv.findContours(grid, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
polygon = np.array([contours[0][:, 0, :]])

rect = lir.lir(polygon)