将二进制图像划分为像素数据的“块" [英] Dividing binary image into 'blocks' of pixel data

查看:93
本文介绍了将二进制图像划分为像素数据的“块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python和PIL作为在二进制图像中嵌入数据的工作的一部分,并且需要分析像素组以确定要操纵的适当像素才能嵌入数据.需要将图像分成相等的块"像素数据以备分析,但我一直在努力寻找一种合适的方法来进行分析.我已经尝试过使用Python和numPy数组的技术,但是没有成功.任何建议将不胜感激.

I am using Python and PIL as part of my work on embedding data in binary images and need to analyse groups of pixels to determine appropriate pixels to manipulate in order to embed data. The image needs to be split into equal 'blocks' of pixel data ready for analysis, but I am struggling to come up with an appropriate method of doing this. I have tried techniques using Python and numPy arrays, but without success. Any suggestions would be greatly appreciated.

谢谢

推荐答案

您可以使用鲜为人知的跨步技巧来创建由块构建的图像的 view .速度非常快,并且不会占用任何额外的内存(该示例有点冗长):

You can use the little known stride tricks to create a view of your image that is built of blocks. It's very fast and does not take any additional memory (the example is a bit verbose):

import numpy as np

#img = np.array(Image.open(filename), dtype='uint8')

w, h = 5, 4 # width, height of image
bw, bh = 2, 3 # width, height of blocks

img = np.random.randint(2, size=(h, w)) # create a random binary image

# build a blocky view of the image data
sz = img.itemsize # size in bytes of the elements in img
shape = (h-bh+1, w-bw+1, bh, bw) # the shape of the new array: two indices for the blocks,
                                 # two indices for the content of each block
strides = (w*sz, sz, w*sz, sz) # information about how to map indices to image data
blocks = np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)

# now we can access the blocks
print img
[[1 1 0 0 0]
 [0 1 1 0 0]
 [0 0 1 0 1]
 [1 0 1 0 0]]

print blocks[0,0]
[[1 1]
 [0 1]
 [0 0]]

print blocks[1,2]
[[1 0]
 [1 0]
 [1 0]]

这篇关于将二进制图像划分为像素数据的“块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆