选择图像上的圆形区域以进行进一步处理

时间:2014-08-13 10:51:50

标签: python image image-processing numpy scikit-image

enter image description here

我使用skimage.feature.blob_doh检测图像上的斑点,然后以格式获取斑点区域:

  

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         一个2d数组,每行代表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() 

1 个答案:

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

我知道,不是完美的代码,我必须努力,但它完成了我的任务。