【问题标题】:Selecting circle areas on image for further processing选择图像上的圆形区域以进行进一步处理
【发布时间】:2014-08-13 21:21:17
【问题描述】:

我正在使用 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) 数组 一个二维数组,每行代表 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() 

【问题讨论】:

  • 您能否轻松上传带有圆圈的实际图像,而不是带有轴和边框的图片?
  • @MarkSetchell 抱歉,我是 Python 新手,我找不到如何用圆圈保存图像的示例,但我可以上传我正在使用的整个代码和图像

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


【解决方案1】:

所以,我有办法做到这一点。

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)

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

【讨论】:

  • 您可以对mask 生成进行矢量化处理,例如:rows = np.arange(image.shape[0]); cols = np.arange(image.shape[1]); mask = ((rows-center[0])**2)[:, None] + (cols - center[1])**2 &lt; radius**2
猜你喜欢
  • 2013-06-12
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多