用于文本的Skimage Blob检测

时间:2017-11-01 08:17:45

标签: python opencv scikit-image cvblobslib

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

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

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

2 个答案:

答案 0 :(得分:1)

首先,要绘制一个矩形,您需要将以下import语句放在最上面:

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中提取图像:

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)

答案 1 :(得分: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')

完整示例,摘自here

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()