问题描述
我想将图像分割成三角形瓷砖(等边)。我尝试使用 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,((x+0.5,(y+1) * h))
first_sc,second_sc,third_sc=(x+1,(x+1.5,(y+1) * h),(x+0.5,(y+1) * h)
return first_c,third_c,first_sc,third_sc
#return [(x,(y+1) * h)],[(x+1,(y+1) * h)]
##Generates the two triangles coordinates
first_c,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,third_sc]
我意识到这可能没有给出我想要的输出。
有关如何解决问题的任何指导将不胜感激。 [1]:https://i.stack.imgur.com/vr7rV.jpg
解决方法
我将此作为答案发布,因为它很长,但这并不是字面上的答案。我希望这会引导您进入设计过程的下一步。
以下是您面临的设计决策,从您的代码中可以清楚地看出,您可以生成三角形坐标列表。很好,接下来呢?您可能知道三角形的边界框(最大 w 和 h)前进,因此您当然可以创建一组包含三角形的图像,用黑色背景或 alpha=0 背景遮蔽。您可以将边界矩形复制到图像,然后使用三角形作为路径创建一个蒙版,并将蒙版外的 alpha 设置为 0。 opencv 应该能够做到这一点。
但是有了这些之后,然后呢?你谈到了匹配边缘。那很复杂。我想您可以从每个三角形的三个边中提取一个像素向量,然后进行某种近似比较。
如果您确实找到了可以将复合材料拼接在一起的匹配项,则可以(假设背景中的 alpha=0)将所有这些三角形重新拼接成更大的图像,有点像绗缝。 openvc 可以通过 alpha 混合进行块复制。
所以,最后,我认为您的问题是可以解决的,但这需要大量的工作,而且可能比我们在这里所能提供的更多。