用像素数据分割多边形并拟合最佳lin

2024-09-29 01:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用two intersecting pentagons的jpeg。我已经用PIL将图像转换成像素数据,但是我在分割多边形的每一个线段时遇到了困难。我想在每个线段上放置一条最佳拟合线来处理角度和线段,以便与其他图形进行比较。我目前正在设置一个左/右/低/高界限来捕获每个片段。它可以工作,但不能捕获每个像素。必须有更好的方法来分离线段或分析多边形。感谢任何帮助或指导。在

img = Image.open(path)

data = img.getdata()
height, width = img.size

pixelList = []
pix = img.load()

for x in range(height):
    for y in range(width):
        pixelList.append((x, y, pix[x,y]))

black_coords = []
x_coords = []
y_coords = []

for i in pixelList:
    if i[2][0] < 50:
        black_coords.append((i[0],i[1]))
        x_coords.append(i[0])
        y_coords.append(i[1])

plt.scatter(x_coords,y_coords)

def segment_bound(left_bound, right_bound, low_bound, high_bound):
    segX = []
    segY = []
    for i in black_coords:
        if i[0] > left_bound and i[0] < right_bound and i[1] > low_bound and 
i[1] < high_bound:
            segX.append(i[0])
            segY.append(i[1])
    return segX, segY

这是每个段的代码,虽然很乏味,但很管用

^{pr2}$

Tags: andinimgfor像素coords多边形black