Hello World / plɹoM ollǝH

Programmers Live in Vain

cv2 voronoi mosaic in python

Python版のサンプルが意外となかったのでメモ

Input Output
f:id:dungeonneko:20200224042947p:plain:w240 f:id:dungeonneko:20200224042958p:plain:w240
import cv2
import numpy as np

def voronoi_facets(in_img, in_k):
    h, w = in_img.shape[0], in_img.shape[1]
    subdiv = cv2.Subdiv2D()
    subdiv.initDelaunay((0, 0, w, h))
    points = np.append(np.random.rand(in_k) * (w - 1), np.random.rand(in_k) * (h - 1)).reshape((in_k, 2), order='F')
    points = np.append(points, ([0, 0], [w - 1, 0], [0, h - 1], [w - 1, h - 1]), axis=0)
    subdiv.insert(tuple(map(tuple, points)))
    return subdiv.getVoronoiFacetList(None)


def polygon_color(in_img, in_polygon):
    c = 0
    i = 0
    h, w = in_img.shape[0], in_img.shape[1]
    for v in in_polygon:
        x, y = int(v[0]), int(v[1])
        if (w > x >= 0) and (h > y >= 0):
            c += in_img[y][x].astype(float)
            i += 1
    return c / i if i > 0 else 0


if __name__ == '__main__':
    img = cv2.imread("lena.png")
    dst = np.zeros(in_img.shape, np.uint8)
    polygons, centers = voronoi_facets(img, 512)
    for p in polygons:
        c = polygon_color(img, p)
        cv2.fillConvexPoly(dst, points=np.array(p).reshape((-1, 1, 2)).astype(np.int32), color=c)
    cv2.imwrite("result.png", img)
参考(C++

aa-deb.hatenablog.com