2

在此处输入图像描述

我正在使用以下格式检测图像上skimage.feature.blob_doh的 blob,并且正在获取我的 blob 区域的格式:

A = 数组([[121, 271, 30], [123, 44, 23], [123, 205, 20], [124, 336, 20], [126, 101, 20], [126, 153, 20], [156, 302, 30], [185, 348, 30], [192, 212, 23], [193, 275, 23], [195, 100, 23], [197, 44, 20] , [197, 153, 20], [260, 173, 30], [262, 243, 23], [265, 113, 23], [270, 363, 30]])

A : (n, 3) ndarray 一个二维数组,每行代表 3 个值,(y,x,sigma) 其中(y,x)是 blob 的坐标,sigma是高斯核的标准偏差(它大约只是我所在区域的半径)

所以问题是——如何选择所有这些区域进行进一步的数据处理(计算平均特征,进行一些聚类和分类)。现在我只是在绘图上绘制它们,但不能将它们迁移到位图\数组变量。

而且我不想使用 OpenCV 库来完成这个任务,我必须使用 numpy/scipy/skimage 和其他库来完成。

 fig, ax = plt.subplots(1, 1)
    ax.set_title(title)
    ax.imshow(image, interpolation='nearest')
    for blob in blobs:
            y, x, r = blob
            c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)
            print c
            ax.add_patch(c)
    plt.show()

感谢您的任何帮助!

UPD:有一些用于裁剪的代码,但它正在做一些奇怪的事情......它裁剪得很好,但是坐标是什么?

def crop_and_save_blobs(image, blobs):
    image = np.asarray(image)
    for blob in blobs:
            y, x, radius = blob
            center = (x, y)
            mask = np.zeros((image.shape[0],image.shape[1]))
            for i in range(image.shape[0]):
                for j in range(image.shape[1]):
                    if (i-center[0])**2 + (j-center[0])**2 < radius**2:
                        mask[i,j] = 1

            # assemble new image (uint8: 0-255)
            newImArray = np.empty(image.shape,dtype='uint8')
            # colors (three first columns, RGB)
            newImArray[:,:,:3] = image[:,:,:3]
            # transparency (4th column)
            newImArray[:,:,3] = mask*255 
            newIm = Image.fromarray(newImArray, "RGBA")
            plt.imshow(newIm)
            plt.show() 
4

1 回答 1

0

所以,有一种方法我做到了。

def circleToSquare(x,y,r):
'''
    Получить 2 точки, по которым можно определить квадрат, 
    описанный вокруг круга с известным центром и радиусом
'''
temp = [x, y - r]
A = [temp[0] - r, temp[1]]
B = [A[0] + 2*r, A[1]]
C = [B[0], B[1] + 2*r]
return A[0], A[1], C[0], C[1]


def imgCrop(im, x, y, radius): 
    '''
        Обрезать круглую область по квадрату
    '''
    box = circleToSquare(x,y,radius)
    return im.crop(box)  


def separateBlobs(image, blobs):
    '''
        Выделить области, в которых потенциально может быть объект
    '''
    separate = []
    image = np.asarray(image)
    index = 0
    for blob in blobs:
            y, x, radius = blob
            center = y, x
            mask = np.zeros((image.shape[0],image.shape[1]))
            for i in range(image.shape[0]):
                for j in range(image.shape[1]):
                    if (i-center[0])**2 + (j-center[1])**2 < radius**2:
                        mask[i,j] = 1

        # assemble new image (uint8: 0-255)
        newImArray = np.empty(image.shape,dtype='uint8')
        # colors (three first columns, RGB)
        newImArray[:,:,:3] = image[:,:,:3]
        # transparency (4th column)
        newImArray[:,:,3] = mask*255 
        newIm = Image.fromarray(newImArray, "RGBA")
        newIm = imgCrop(newIm, x, y, radius)
        misc.imsave('image' + str(index) + ".png", newIm)
        separate.append(newIm)
        index += 1
return separate       

裁剪这些图像然后运行它的三种方法:

im = Image.open(path).convert("RGBA")    
separateB = separateBlobs(im, blobs)

我知道,不是完美的代码,我必须处理它,但它很好地完成了我的任务。

于 2014-08-13T13:21:17.793 回答