用python将图像分成5x5个块,并计算每个b的直方图

2024-03-28 09:16:30 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python,我必须:

  • Test_ImageReference_image分成5x5个块
  • 计算每个块的直方图,并将其与另一幅图像中的同一块进行比较。
    例如:image1(1,1)image2(1,1)
  • 比较两个图像之间的相似性(应该是变换不变量)。

到目前为止,我已经使用hist=numpy.histogram(image,bins=256)计算了整个图像的直方图

我想分割图像,然后计算所有这些块的直方图。我还想用巴特查里亚系数来衡量相似性。

有人能教我怎么通过这一关吗?提前谢谢:)


Tags: test图像imagenumpy直方图相似性histhistogram
3条回答

这对我有效。它有能力分成n*m块。相应地贴上你的图片。

def chunkify(img, block_width=4, block_height=4):
  shape = img.shape
  x_len = shape[0]//block_width
  y_len = shape[1]//block_height

  chunks = []
  x_indices = [i for i in range(0, shape[0]+1, block_width)]
  y_indices = [i for i in range(0, shape[1]+1, block_height)]

  shapes = list(zip(x_indices, y_indices))

  for i in range(len(shapes)):
      try:
        start_x = shapes[i][0]
        start_y = shapes[i][1]
        end_x = shapes[i+1][0]
        end_y = shapes[i+1][1]
        chunks.append( shapes[start_x:end_x][start_y:end_y] )
      except IndexError:
        print('End of Array')

  return chunks

https://github.com/QuantumNovice/ImageProcessing/blob/master/image_chunkify.py

如果您的图像很大,可以通过操纵数组的跨步来生成所需的窗口来提高性能。下面将使用在Efficient Overlapping Windows with Numpy找到的通用滑动窗口函数-我将在末尾包含它。

import numpy as np
image1 = np.arange(100).reshape(10,10)
image2 = np.arange(100).reshape(10,10)

from itertools import izip
window_size = (5,5)
windows1 = sliding_window(image1, window_size)
windows2 = sliding_window(image2, window_size)
histograms = [(np.histogram(window1,bins=256),np.histogram(window2,bins=256))
              for window1, window2 in izip(windows1, windows2)]

for h1, h2 in histograms:
    print np.all(h1[0] == h2[0])

滑动窗口功能:

from numpy.lib.stride_tricks import as_strided as ast
from itertools import product

def norm_shape(shape):
    '''
    Normalize numpy array shapes so they're always expressed as a tuple, 
    even for one-dimensional shapes.

    Parameters
        shape - an int, or a tuple of ints

    Returns
        a shape tuple
    '''
    try:
        i = int(shape)
        return (i,)
    except TypeError:
        # shape was not a number
        pass

    try:
        t = tuple(shape)
        return t
    except TypeError:
        # shape was not iterable
        pass

    raise TypeError('shape must be an int, or a tuple of ints')


def sliding_window(a,ws,ss = None,flatten = True):
    '''
    Return a sliding window over a in any number of dimensions

    Parameters:
        a  - an n-dimensional numpy array
        ws - an int (a is 1D) or tuple (a is 2D or greater) representing the size 
             of each dimension of the window
        ss - an int (a is 1D) or tuple (a is 2D or greater) representing the 
             amount to slide the window in each dimension. If not specified, it
             defaults to ws.
        flatten - if True, all slices are flattened, otherwise, there is an 
                  extra dimension for each dimension of the input.

    Returns
        an array containing each n-dimensional window from a

    from http://www.johnvinyard.com/blog/?p=268
    '''

    if None is ss:
        # ss was not provided. the windows will not overlap in any direction.
        ss = ws
    ws = norm_shape(ws)
    ss = norm_shape(ss)

    # convert ws, ss, and a.shape to numpy arrays so that we can do math in every 
    # dimension at once.
    ws = np.array(ws)
    ss = np.array(ss)
    shape = np.array(a.shape)


    # ensure that ws, ss, and a.shape all have the same number of dimensions
    ls = [len(shape),len(ws),len(ss)]
    if 1 != len(set(ls)):
        raise ValueError(\
        'a.shape, ws and ss must all have the same length. They were %s' % str(ls))

    # ensure that ws is smaller than a in every dimension
    if np.any(ws > shape):
        raise ValueError('ws cannot be larger than a in any dimension. a.shape was %s and ws was %s' % (str(a.shape),str(ws)))

    # how many slices will there be in each dimension?
    newshape = norm_shape(((shape - ws) // ss) + 1)
    # the shape of the strided array will be the number of slices in each dimension
    # plus the shape of the window (tuple addition)
    newshape += norm_shape(ws)
    # the strides tuple will be the array's strides multiplied by step size, plus
    # the array's strides (tuple addition)
    newstrides = norm_shape(np.array(a.strides) * ss) + a.strides
    strided = ast(a,shape = newshape,strides = newstrides)
    if not flatten:
        return strided

    # Collapse strided so that it has one more dimension than the window.  I.e.,
    # the new array is a flat list of slices.
    meat = len(ws) if ws.shape else 0
    firstdim = (np.product(newshape[:-meat]),) if ws.shape else ()
    dim = firstdim + (newshape[-meat:])
    # remove any dimensions with size 1
    dim = filter(lambda i : i != 1,dim)
    return strided.reshape(dim)

如果要将图像分成四部分,则需要计算wsss参数。如果两个维度都可以被两个整除,那么wsss是相同的值(未指定时,ss默认为ws)。Numpy能够将数组维度视为(column,row)或(row,column)-我没有更改任何默认值,我的是(row,column)。对于18x26图片,ws = (26/2, 18/2)-每个窗口将是13x9,并且通过将窗口以相等的量(没有重叠)siliding来获得相邻的窗口。如果一个维度不能被2整除,ss也需要确定,并且在窗口中会有一些重叠。对于18x33图像:

>>> 
>>> rows = 33
>>> columns = 18
>>> divisor = 2
>>> col_size, col_overlap = divmod(columns, divisor)
>>> row_size, row_overlap = divmod(rows, divisor)
>>> ws = (row_size, col_size)
>>> ss = (row_size - row_overlap, col_size - col_overlap)
>>> ws, ss
((16, 9), (15, 9))
>>> 

对于3d窗口(来自具有颜色维度的图像的数据)wsss需要具有三维。15x15图像将有9个5x5x3窗口

from PIL import Image
import numpy as np

img = Image.open('15by15.bmp')
a = np.asarray(img)
window_size = (5,5,3)
windows = sliding_window(a, window_size)
print windows.shape

>>> (9, 5, 5, 3)

for window in windows:
    print window.shape

>>> (5, 5, 3) (5, 5, 3) (5, 5, 3) (5, 5, 3) (5, 5, 3) (5, 5, 3) (5, 5, 3) (5, 5, 3) (5, 5, 3)

不确定你要找的是不是这样的东西, 这是蛮力版的,而且速度可能很慢,但它确实起到了作用 但是你必须决定如何处理边界。 这将不包括边界,除非窗口完全适合

import numpy as numpy

grey_levels = 256
# Generate a test image
test_image = numpy.random.randint(0,grey_levels, size=(11,11))

# Define the window size
windowsize_r = 5
windowsize_c = 5

# Crop out the window and calculate the histogram
for r in range(0,test_image.shape[0] - windowsize_r, windowsize_r):
    for c in range(0,test_image.shape[1] - windowsize_c, windowsize_c):
        window = test_image[r:r+windowsize_r,c:c+windowsize_c]
        hist = numpy.histogram(window,bins=grey_levels)

下面是结果,完整图像在最后。 r、 c表示窗口的左上角

r=0,c=0
[[ 63 173 131 205 239]
 [106  37 156  48  81]
 [ 85  85 119  60 228]
 [236  79 247   1 206]
 [ 97  50 117  96 206]]

r=0,c=5
[[108 241 155 214 183]
 [202   2 236 183 225]
 [214 141   1 185 115]
 [  4 234 249  95  67]
 [232 217 116 211  24]]

r=5,c=0
[[179 155  41  47 190]
 [159  69 211  41  92]
 [ 64 184 187 104 245]
 [190 199  71 228 166]
 [117  56  92   5 186]]

r=5,c=5
[[ 68   6  69  63 242]
 [213 133 139  59  44]
 [236  69 148 196 215]
 [ 41 228 198 115 107]
 [109 236 191  48  53]]

[[ 63 173 131 205 239 108 241 155 214 183  42]
 [106  37 156  48  81 202   2 236 183 225   4]
 [ 85  85 119  60 228 214 141   1 185 115  80]
 [236  79 247   1 206   4 234 249  95  67 203]
 [ 97  50 117  96 206 232 217 116 211  24 242]
 [179 155  41  47 190  68   6  69  63 242 162]
 [159  69 211  41  92 213 133 139  59  44 196]
 [ 64 184 187 104 245 236  69 148 196 215  91]
 [190 199  71 228 166  41 228 198 115 107  82]
 [117  56  92   5 186 109 236 191  48  53  65]
 [177 170 114 163 101  54  80  25 112  35  85]]

相关问题 更多 >