0

我想将图像拆分为三角形瓷砖(等边)。我尝试使用https://alexwlchan.net/2016/10/tiling-the-plane-with-pillow/中的函数生成三角形的坐标。我的代码:

#import opencv 
import math 
image_path="/content/newspaper-icon-in-transparent-style-news-on-vector-25591681.jpg"

#Create Triangles
# https://alexwlchan.net/2016/10/tiling-the-plane-with-pillow/
#A horrizontal offset is added to ensure that images line up
#https://stackoverflow.com/questions/22588074/polygon-crop-clip-using-python-pil
def generate_coordinates_for_unit_triangles(image_width,image_height):
     image_width=50;
     image_height=50;
     h=math.sin(math.pi/3)
     for x in range(image_width):
        for y in range(int(image_height / h)):
            first_c,second_c,third_c=(x, y * h), (x+1, y * h), ((x+0.5, (y+1) * h))
            first_sc, second_sc,third_sc=(x+1, y * h), (x+1.5, (y+1) * h), (x+0.5, (y+1) * h)
        return first_c, second_c,third_c, first_sc, second_sc,third_sc                                            
            #return [(x, y * h), (x+1, y * h), (x+0.5, (y+1) * h)] ,[(x+1, y * h), (x+1.5, (y+1) * h), (x+0.5, (y+1) * h)]

##Generates the two triangles  coordinates
first_c, second_c,third_c, first_sc, second_sc,third_sc=generate_coordinates_for_unit_triangles(50,50)

#convert image into numpy array
image_read=Image.open(image_path)
image_to_numpy=np.asarray(image_read)
shape_of_array=image_to_numpy.shape

print(shape_of_array)
mask_image=[first_c, second_c,third_c, first_sc, second_sc,third_sc] 

我意识到这可能无法给出我想要的输出。

预期输入输出如下: [预期输入输出][1]

任何有关如何解决该问题的指导将不胜感激。[1]:https ://i.stack.imgur.com/vr7rV.jpg

4

1 回答 1

0

我将其发布为答案,因为它很长,但实际上并不是答案。我希望这将引导您进入设计过程的下一步。

这是您面临的设计决策,从您的代码中可以清楚地看出,您可以生成三角坐标列表。好,接下来呢?您可能知道三角形的边界框(最大 w 和 h)提前,因此您当然可以创建一组包含三角形的图像,用黑色背景或 alpha=0 背景遮盖。您可以将边界矩形复制到图像,然后使用三角形作为路径创建蒙版,并将蒙版外的 alpha 设置为 0。opencv 应该能够做到这一点。

但是当你有了这些之后,然后呢?你谈到了匹配边缘。这很复杂。我想你可以从每个三角形的三个边缘中提取一个像素向量,然后进行某种近似比较。

如果您确实找到了允许您将合成物拼接在一起的匹配项,则可以(假设您在背景中具有 alpha=0)将所有这些三角形重新拼接成更大的图像,有点像绗缝。openvc 可以使用 alpha 混合进行块复制。

所以,最后,我认为你的问题是可以解决的,但这将是很多工作,而且可能比我们在这里提供的更多。

于 2021-03-11T22:08:51.177 回答