简体   繁体   English

Skimage斑点检测文本

[英]Skimage Blob detection for Text

I am following this tutorial of Blob Detection for Text purpose and facing some issues, please check if anyone could help. 我正在关注针对文本的Blob检测教程,并且遇到了一些问题,请检查是否有人可以提供帮助。

How to extract each detected blob in form of image. 如何提取图像形式的每个检测到的斑点。 How i can draw a rectangle instead of circle. 我如何绘制矩形而不是圆形。

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 http://scikit-image.org/docs/dev/auto_examples/features_detection/plot_blob.html

First, to draw a rectangle you need to put the following import statement on top: 首先,要绘制一个矩形,您需要在顶部放置以下import语句:

import matplotlib.patches as patches
from skimage import io

next change the line that draws the circles: 接下来更改绘制圆圈的线:

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

to a line drawing rectangles: 到绘制矩形的线:

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

This will create a rectangle (actually a square) with top left vertex at (x - r,y - r) and of width and height of 2*r. 这将创建一个矩形(实际上是一个正方形),其左上角顶点位于(x-r,y-r)且宽度和高度为2 * r。 here r is the standard deviation of the blur used while detecting the blob. 在此,r是检测斑点时使用的模糊的标准偏差。

Now to extract the image within the 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)

Change first argument as any path (including desired image name). 将第一个参数更改为任何路径(包括所需的图像名称)。

  • Note I have not tested the above code so the crop could have reverse coordinates, also check the coordinates of the rectangle. 注意我没有测试上面的代码,因此裁剪可能具有反向坐标,还要检查矩形的坐标。

Full working code looks like below: 完整的工作代码如下所示:

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)

Instead of 代替

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

You will need to do a little more complicated routine using patches, here is a complete example of drawing a rectangle on an image. 您将需要使用补丁执行一些更复杂的例程,这是在图像上绘制矩形的完整示例。 The specific line in here you are interested in is: 您感兴趣的特定行是:

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

Complete Example, taken from 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM