寻找能够容纳形状的二进制图像中的空白空间。

huangapple go评论73阅读模式
英文:

Find an empty space in a binary image that can fit a shape

问题

我有这张图片
我需要找到一个能容纳这个形状的空白区域
以便最终结果类似于这样
寻找能够容纳形状的二进制图像中的空白空间。

英文:

I have this image
<br>
寻找能够容纳形状的二进制图像中的空白空间。
<br>
I need to find an empty area that can fit this shape
<br>
寻找能够容纳形状的二进制图像中的空白空间。
<br>
so that the end result is something like this
<br>
寻找能够容纳形状的二进制图像中的空白空间。

答案1

得分: 2

以下是对该代码部分的翻译:

这是一个简单但天真的解决此问题的方法。正如UnquoteQuote所提到的,它使用了2D卷积。

import random
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
import cv2

# 载入图像
image = (cv2.imread('image.jpg').mean(axis=2) > 127).astype(np.float32)
shape = (cv2.imread('shape.jpg').mean(axis=2) > 127).astype(np.float32)

# 执行2D卷积
conv = sig.convolve2d(image, shape, mode='valid')
solutions = np.where(conv == 0)

# 绘制一些解决方案
plt.figure(figsize=(16, 4))
for i in range(4):
    r = random.randint(0, solutions[0].shape[0] - 1)
    x, y = solutions[0][r], solutions[1][r]

    solution_plot = np.zeros((*image.shape, 3))
    solution_plot[:, :, 0] = image
    solution_plot[x:x + shape.shape[0], y:y + shape.shape[1], 1] = shape

    plt.subplot(1, 4, i + 1)
    plt.imshow(solution_plot)

plt.show()

示例结果:
寻找能够容纳形状的二进制图像中的空白空间。

此算法找到了所有可能的解决方案。如果您只需要一个解决方案,可以优化它,以获取随机的(x, y)点,并执行形状和裁剪图像区域[x:x+shape_width, y:y+shape_height]的点积,以检查是否有空间,直到找到正确的点。

可以像这样执行:

while True:
    x = random.randint(0, image.shape[0] - shape.shape[0])
    y = random.randint(0, image.shape[1] - shape.shape[1])

    if np.sum(shape*image[x:x + shape.shape[0], y:y + shape.shape[1]]) == 0:
        break

# x, y 是解决方案

与卷积相比,这个方法要快得多(但这取决于解决方案的数量):

  • 卷积:6.65 秒 ± 21.4 毫秒每次循环(7次运行的平均值 ± 标准偏差,每次循环1次)
  • 随机搜索:1.31 毫秒 ± 31.2 微秒每次循环(7次运行的平均值 ± 标准偏差,每次循环1000次)
英文:

Here is a simple yet naive solution to this problem. It uses 2D convolution as mentioned by UnquoteQuote.

import random
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
import cv2

# load images
image = (cv2.imread(&#39;image.jpg&#39;).mean(axis=2) &gt; 127).astype(np.float32)
shape = (cv2.imread(&#39;shape.jpg&#39;).mean(axis=2) &gt; 127).astype(np.float32)

# perform 2D convolution
conv = sig.convolve2d(image, shape, mode=&#39;valid&#39;)
solutions = np.where(conv == 0)

# draw some solutions
plt.figure(figsize=(16, 4))
for i in range(4):
    r = random.randint(0, solutions[0].shape[0] - 1)
    x, y = solutions[0][r], solutions[1][r]

    solution_plot = np.zeros((*image.shape, 3))
    solution_plot[:, :, 0] = image
    solution_plot[x:x + shape.shape[0], y:y + shape.shape[1], 1] = shape

    plt.subplot(1, 4, i + 1)
    plt.imshow(solution_plot)

plt.show()

Example results:
寻找能够容纳形状的二进制图像中的空白空间。

This algorithm finds all possible solutions. If you only need one, you can optimize it so that it gets a random (x, y) point and perform dot product of the shape and a cropped image area [x:x+shape_width, y:y+shape_height] to check if there is a space until you find the right point.

This can be done for example like this:

while True:
    x = random.randint(0, image.shape[0] - shape.shape[0])
    y = random.randint(0, image.shape[1] - shape.shape[1])

    if np.sum(shape*image[x:x + shape.shape[0], y:y + shape.shape[1]]) == 0:
        break

# x, y is the solution

Compared to convolution this one is much faster (but it depends on the number of the solutions):

  • convolution: 6.65 s &#177; 21.4 ms per loop (mean &#177; std. dev. of 7 runs, 1 loop each)

  • random search: 1.31 ms &#177; 31.2 &#181;s per loop (mean &#177; std. dev. of 7 runs, 1,000 loops each)

huangapple
  • 本文由 发表于 2023年6月22日 04:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定