【问题标题】:Intersecting figures reckoning相交数字计算
【发布时间】:2020-11-29 20:29:29
【问题描述】:

我正在努力寻找一种方法来计算一个或多个图形(如不同颜色的正方形/矩形)被 PNG 图片中的其他正方形/矩形截取的次数。目标是编写一个函数,将 PNG 图片加载为包含元组的矩阵,其中每个元组代表 RGB 颜色模式下的一个颜色像素:

例如黑色 = (0,0,0),白色 = (255,255,255),红色 = (255,0,0),等等。

确定一个图形与其他图形重叠的次数,并将信息保存在字典中,其中每个键代表图形颜色,每个值代表同一图形的截距。

如上图所示,所有方块都有不同的颜色。我尝试使用以下函数解决此问题。

import images
def intersections_reckoning(image_filename):
    
    white = (255,255,255)
    overlapping = {}
    img = images.load(image_filename)
    for i in img:
        for j in range(len(i)):
            count = 0
            if i[j] == white:  
                continue
            elif i[j] != i[j-1]:
                count +=1
                overlapping[i[j-1]] = count
    return overlapping

这给了我以下错误输出:

{(255, 255, 255): 1,
 (255, 0, 0): 1,
 (0, 0, 255): 1,
 (0, 255, 0): 1,
 (255, 255, 0): 1}

代替

{(255, 0, 0): 4, --> red square is overlapped 4 times 
 (0, 0, 255): 0, --> blue square is not overlapped by any other figures. It intercepts others though 
 (0, 255, 0): 2, --> green square is overlapped twice
 (255, 255, 0): 2} --> yellow square is overlapped twice

我应该如何解决这个问题?任何帮助将不胜感激

谢谢

【问题讨论】:

  • 我不确定你的方法。我的方法是用两个 for 循环在图像周围移动,沿着遇到的每个矩形的边框,然后退出循环,并存储一个包含所有矩形角坐标的列表。完成此操作后,使用数学来确定与其他方格的交点。如果您不关心效率并检查set(XY1) intersects set(XY2) 是否为 XY1 XY2 一对矩形的坐标列表,您也可以对其进行暴力破解。
  • 你会使用嵌套循环吗?
  • 好吧,两个嵌套循环来检查每个像素 (img[i][j]),然后是一个循环来遍历形状。
  • @Guimute 你能详细说明一下吗?

标签: python python-3.x


【解决方案1】:

我会这样做。

首先,我们解析图像并寻找彩色片段。我们根据这些段是垂直的还是水平的,将它们的 x 或 y 坐标添加到字典中。这为每个彩色矩形提供了一个包含四个坐标x1, y1, x2, y2 的字典,它们代表该矩形的。从那里我认为您可以轻松地进行一些数学运算并计算矩形是否相互交叉。

我使用线段而不是邻居检查的原因是,否则如果与另一个矩形的交点角,则此方法将失败。

警告:我的方法在与另一个矩形的交点是矩形的整个边的边缘情况下失败。

import matplotlib.pyplot as plt
import numpy as np

# Create the image and rectangles.
N = 10
image = np.ones((N, N, 3), dtype=np.uint8)*255

blue = (0, 0, 255)
image[1, 2:6] = blue
image[1:6, 6] = blue
image[5, 2:6] = blue
image[2:6, 2] = blue

red = (255, 0, 0)
image[4, 5:9] = red
image[4:9, 5] = red
image[8, 5:9] = red
image[4:9, 9] = red

fig, ax = plt.subplots()
ax.set_xlim(0, N)
ax.set_ylim(0, N)
ax.imshow(image)

# Search for corners.
(X, Y, _) = image.shape
shapes = {}
for (name, color) in (("blue", blue), ("red", red)):
    shape = shapes[name] = {}

    # Searching for horizontal segments.
    for x in range(X):
        right_color = [True for b in image[x, :, :] == color if b.all()]
        if len(right_color) >= 3:
            if shape.get("x1") is None:
                shape["x1"] = x
            elif shape.get("x2") is None:
                shape["x2"] = x

    # Searching for vertical segments.
    for y in range(Y):
        right_color = [True for b in image[:, y, :] == color if b.all()]
        if len(right_color) >= 3:
            if shape.get("y1") is None:
                shape["y1"] = y
            elif shape.get("y2") is None:
                shape["y2"] = y

print(shapes["blue"])
# >>> {'x1': 1, 'x2': 5, 'y1': 2, 'y2': 6}
print(shapes["red"])
# >>> {'x1': 4, 'x2': 8, 'y1': 5, 'y2': 9}

# Find the intersections.
NotImplemented

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多