我在这个论坛上看到了几个关于计算掩码数组中值的讨论,比如图像.我想要的是稍微更微妙,它是在我的图像上应用中值滤镜.我知道一种方法可以做到这一点,但速度太慢,并且会喜欢加速这个过程的方法.
例如,假设我有一个蒙版数组形状(10,10),我想应用一个带有框(3,3)的中值滤镜,而不使用那些被遮罩的元素.我的目标是用图像的每个像素中的值替换该框的掩蔽中值.
假设一个非常简单的情况,我们可以构建"图像"和掩码为:
 im = numpy.random.uniform(size=(10,10))
 mask = numpy.zeros_like(im)
 mask[1:3,:] = 1
 masked_im = numpy.ma.array(im, mask=mask)
现在,为了实际制作中值滤波器,我们可以用蛮力的方式做到:
 lx, ly = im.shape
 side = 3
 im_filt = numpy.zeros_like(im)
 for jj in range(ly):
     for ii in range(lx):
         minx, maxx = max([ii-side/2,0]), min([ii+side/2+1,lx])
         miny, maxy = max([jj-side/2,0]), min([jj+side/2+1,ly])
         im_filt[ii,jj] = numpy.ma.median(masked_im[minx:maxx, miny:maxy])
这解决了问题,并给出了一个很好的结果,但正如我所说,它是非常缓慢的.一个(对我来说,令人惊讶)稍微加快过程的方法是分别使用蒙版和图像,如:
 im_filt2 = numpy.zeros_like(im)
 for jj in range(ly):
     for ii in range(lx):
         minx, maxx = max([ii-side/2,0]), min([ii+side/2+1,lx])
         miny, maxy = max([jj-side/2,0]), min([jj+side/2+1,ly])
         zoom_im = im[minx:maxx, miny:maxy]
         zoom_msk = mask[minx:maxx, …I'm trying to do mathematical operations between images. I have defined (simplified version of my real code):
parser = argparse.ArgumentParser(description='Arithmetic operations on images')
parser.add_argument("input1", metavar='input1', action='store',
      help='list of input images to operate with', nargs="+", type=str)
parser.add_argument("operation", metavar='operation', action='store', type=str, 
      help='type of operation (+,-,*,/) to be done', nargs=1)
parser.add_argument("input2",metavar='input2', action='store', nargs="+", 
      type=str, help='image (or value) with which to perform the operation on input1')
This code, produces:
arith.py -h
usage: arith.py [-h] input1 [input1 ...] operation input2 [input2 ...]
so it does …