我想将RGB图像分成块8x8 = 64(块中的像素)并从块中提取像素颜色。我已将图像分成块8x8:
w, h = img.size # width, height of image
bw, bh = 8, 8 # block size
img = Image.open('anyimage.JPG')
img = np.array(img)
sz = img.itemsize
shape = (h-bh+1, w-bw+1, bh, bw
strides = (w*sz, sz, w*sz, sz)
blocks = np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides)
print blocks[1,1]
我在块中得到这个值:
[[169 185 147 170 186 149 170 187]
[139 161 122 140 162 122 140 162]
[131 135 113 131 135 118 135 142]
[170 186 147 170 186 147 170 188]
[142 164 124 142 164 122 140 162]
[136 138 113 137 141 114 138 142]
[174 191 148 174 191 147 173 190]
[142 164 126 144 166 125 143 165]]
如何从块中提取所有像素颜色?
答案 0 :(得分:0)
你可以尝试
pixels = img.load()
all_pixels = []
for x in range(block_width): #put your block width size
for y in range(block_height): #your block heigh size
cpixel = pixels[x, y] #reading elements from pixel on location (x,y)
all_pixels.append(cpixel)
以tupple的形式将每个像素附加到all_pixel。例如
(111, 222, 255) #R-G-B