与previous 1 numpy的阵列上比较当前像素值 [英] Compare current pixel value with the previous one on numpy array
问题描述
有可能实现在numpy的阵列这个图像过滤过程?我需要检查previous列和previous行的像素是differente当前像素。
Is possible to implement this image filtering process in numpy array ? I need to check if the pixel in the previous column and previous row is differente of the current pixel.
width, height = orig_bin.size
pixels = orig_bin.load()
delta = 50
begin = 10
min_w = 30
max_w = 260
min_h = 10
max_h = 40
w_range = range(begin, width - min_w - delta)
h_range = range(begin, height - min_h - delta)
is_changing = False
for x in w_range:
for y in h_range:
change_pixel = False
current_pixel = pixels[x,y]
if current_pixel != pixels[x, y+1]:
change_pixel = True
if current_pixel != pixels[x+1, y]:
change_pixel = True
if change_pixel:
pixels[x,y] = (0,0,0)
else:
pixels[x,y] = (255,255,255)
最好的问候,
埃米利奥
Best regards, Emilio
推荐答案
下面是一个方法。以一个例如图片:
Here's one approach. Take an example image:
您没有说您的 orig_bin
是从哪里来的,所以我使用的 scipy.misc.imread
:
You didn't say where your orig_bin
came from, so I've used scipy.misc.imread
:
from scipy.misc import imread, imsave
img = imread('input.png')
首先,创建是从上面的像素不同像素面具(这里使用一个想法从毕波多黎各的回答 ):
up = (img[1:,1:] != img[:-1,1:]).any(axis=2)
注意 imread
载荷行优先顺序排列的图像,所以第一numpy的轴是垂直轴。请参见<一个href=\"http://docs.scipy.org/doc/numpy/reference/internals.html#multidimensional-array-indexing-order-issues\"相对=nofollow>多维数组索引顺序问题解释了。
Note that imread
loads images in row-major order, so the first NumPy axis is the vertical axis. See "Multidimensional Array Indexing Order Issues" for an explanation.
类似地,创建一个掩模是从像素向左不同像素:
Similarly, create a mask for pixels that are different from the pixel to the left:
left = (img[1:,1:] != img[1:,:-1]).any(axis=2)
结合这些能为那些对以上任一像素或左不同像素的面具:
Combine these to get a mask for pixels that are different to either the pixel above or left:
mask = numpy.zeros(img.shape[:2], dtype=bool)
mask[1:,1:] = left | up
创建一个大小合适的黑色输出图像;然后将面膜白:
Create a black output image of the right size; then set the mask to white:
output = numpy.zeros(img.shape)
output[mask] = (255, 255, 255)
imsave('output.png', output)
和这里的结果是:
或者,如果你想的颜色是圆的其他方式,反转面膜:
or if you want the colours to be the other way round, invert the mask:
output[~mask] = (255, 255, 255)
这篇关于与previous 1 numpy的阵列上比较当前像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!