在图像上选择圆形区域以便后续处理

2 投票
1 回答
2015 浏览
提问于 2025-04-18 17:07

在这里输入图片描述

我正在使用 skimage.feature.blob_doh 来检测图像中的斑点(blob),并且我得到了斑点区域的数据,格式如下:

A = array([[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) 的数组,
这是一个二维数组,每一行代表三个值,(y,x,sigma)
其中 (y,x) 是斑点的坐标,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()

谢谢任何帮助!

更新:我有一些裁剪的代码,但它做的有点奇怪……裁剪得很好,但坐标怎么回事?

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

所以,我有一种方法可以做到这一点。

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)

我知道,这段代码并不完美,我还需要改进,但它能很好地完成我的任务。

撰写回答