0

我正在关注本教程的 Blob Detection for Text 目的并遇到一些问题,请检查是否有人可以提供帮助。

如何以图像的形式提取每个检测到的 blob。我如何绘制一个矩形而不是圆形。

from math import sqrt
from skimage import data
from skimage.feature import blob_dog, blob_log, blob_doh
from skimage.color import rgb2gray
import skimage.io as io
import matplotlib.pyplot as plt
import matplotlib.patches as patches
#from imread import imread_from_blob

image = io.imread('5.png')

#image = (data.hubble_deep_field()[0:500, 0:500])
image_gray = rgb2gray(image)

#blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.1)

# Compute radii in the 3rd column.
#blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)

blobs_dog = blob_dog(image_gray, max_sigma=30, threshold=.1)
#blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2)

#blobs_doh = blob_doh(image_gray, max_sigma=30, threshold=.01)

blobs_list = [ blobs_dog]

colors = ['yellow']
titles = [ 'Difference of Gaussian']
sequence = zip(blobs_list, colors, titles)

fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True,
                         subplot_kw={'adjustable': 'box-forced'})
ax = axes.ravel()

for idx, (blobs, color, title) in enumerate(sequence):
    ax[idx].set_title(title)
    ax[idx].imshow(image, interpolation='nearest')
    for blob in blobs:
        y, x, r = blob
        c = patches.Rectangle((int(x - r),int(y - r)), int(2*r), int(2*r),linewidth=2,edgecolor=color,facecolor='none')

        ax[idx].add_patch(c)
    ax[idx].set_axis_off()
    croppedImage = image[int(x-r):int(x+r),int(y-r):int(y+r)]
    if croppedImage.shape[0] > 0 and croppedImage.shape[1] > 0:
        io.imsave('C:/Users/A/Projects/Image/Test/test.png', croppedImage)
plt.tight_layout()
plt.show()

http://scikit-image.org/docs/dev/auto_examples/features_detection/plot_blob.html

4

2 回答 2

1

首先,要绘制一个矩形,您需要将以下导入语句放在顶部:

import matplotlib.patches as patches
from skimage import io

接下来更改绘制圆圈的线:

c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)

到画线矩形:

c = patches.Rectangle((int(x - r),int(y - r)), int(2*r), int(2*r),linewidth=2,edgecolor=color,facecolor='none')

这将创建一个矩形(实际上是一个正方形),其左上顶点位于 (x - r,y - r),宽度和高度为 2*r。这里 r 是检测 blob 时使用的模糊标准偏差。

现在提取 blob 中的图像:

croppedImage = image[int(x-r):int(x+r),int(y-r):int(y+r)]
if croppedImage.shape[0] > 0 and croppedImage.shape[1] > 0:
    io.imsave('letter_image.png', croppedImage)

将第一个参数更改为任何路径(包括所需的图像名称)。

  • 注意我没有测试上面的代码,所以裁剪可能有反向坐标,还要检查矩形的坐标。

完整的工作代码如下所示:

from math import sqrt
from skimage import data
from skimage.feature import blob_dog, blob_log, blob_doh
from skimage.color import rgb2gray
import skimage.io as io
import matplotlib.pyplot as plt
import matplotlib.patches as patches
#from imread import imread_from_blob

image = io.imread('5.png')

# image = (data.hubble_deep_field()[0:500, 0:500])
image_gray = rgb2gray(image)

# blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.1)

# Compute radii in the 3rd column.
#blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)

blobs_dog = blob_dog(image_gray, max_sigma=30, threshold=.1)
#blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2)

#blobs_doh = blob_doh(image_gray, max_sigma=30, threshold=.01)

blobs_list = [ blobs_dog]

colors = ['yellow']
titles = [ 'Difference of Gaussian']
sequence = zip(blobs_list, colors, titles)

fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True,
                         subplot_kw={'adjustable': 'box-forced'})
ax = axes.ravel()

for idx, (blobs, color, title) in enumerate(sequence):
    ax[idx].set_title(title)
    ax[idx].imshow(image, interpolation='nearest')
    for i,blob in enumerate(blobs):
        y, x, r = blob
        c = patches.Rectangle((int(x - r),int(y - r)), int(2*r), int(2*r),linewidth=2,edgecolor=color,facecolor='none')
        croppedImage = image[int(x-r):int(x+r),int(y-r):int(y+r)]
        if croppedImage.shape[0] > 0 and croppedImage.shape[1] > 0:
            io.imsave('C:/Users/A/Projects/Image/Test/test'+str(i)+'.png', croppedImage)
于 2017-11-01T09:01:42.293 回答
0

代替

c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)

您将需要使用补丁做一些更复杂的例程,这是在图像上绘制矩形的完整示例。您感兴趣的特定行是:

rect = patches.Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none')

完整示例,取自此处

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np

im = np.array(Image.open('stinkbug.png'), dtype=np.uint8)

# Create figure and axes
fig,ax = plt.subplots(1)

# Display the image
ax.imshow(im)

# Create a Rectangle patch
rect = patches.Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none')

# Add the patch to the Axes
ax.add_patch(rect)

plt.show()
于 2017-11-01T08:55:42.377 回答