如何在Python中将图像分为n个不同的多边形

时间:2018-11-02 21:20:13

标签: python image image-processing image-segmentation

我是python的新手,正在尝试使用python将图像划分为'n'个不同的多边形。我的目标是将图像转换为n个随机多边形图像。我尝试了Voronoi算法,但是有点混乱。我真的很感谢您的帮助。其他任何细分方法等

我以前的代码:

import random
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d

img = plt.imread("abc.jpg")
fig, ax = plt.subplots()
ax.imshow(img)

def points(radius,rangeX,rangeY,qty):
    deltas = set()
    for x in range(-radius, radius+1):
        for y in range(-radius, radius+1):
            if x*x + y*y <= radius*radius:
                deltas.add((x,y))

    randPoints = []
    excluded = set()
    i = 0
    while i<qty:
        x = random.randrange(*rangeX)
        y = random.randrange(*rangeY)
        if (x,y) in excluded: continue
        randPoints.append((x,y))
        i += 1
        excluded.update((x+dx, y+dy) for (dx,dy) in deltas)
    return randPoints



def plot1(randPoints,fig):
    points = np.array(randPoints)
    vor = Voronoi(points)
    print vor.vertices
    voronoi_plot_2d(vor,ax = fig.gca())
    #plt.savefig('abc.png')
    plt.show()

radius = 20
rangeX = (0, 960)
rangeY = (0, 480)
qty = 9

points = points(radius, rangeX, rangeY, qty)
plot1(points,fig)

我的输入:  enter image description here

我的输出: enter image description here

这是针对n = 9,我将不胜感激。

0 个答案:

没有答案