【问题标题】:How to divide image into blocks and find pixels colors如何将图像分成块并找到像素颜色
【发布时间】:2016-11-09 09:09:43
【问题描述】:

我想将 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]]

如何从块中提取所有像素颜色?

【问题讨论】:

    标签: python image-processing


    【解决方案1】:

    你可以试试

    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)
    

    以元组的形式将每个像素附加到 all_pixel。例如

    (111, 222, 255) #R-G-B
    

    【讨论】:

    • 感谢您的回复。我试过 img.getdata() 但它会返回整个图像像素颜色我需要特定的块像素颜色。
    • 很抱歉,您的目标让我有些困惑。你能提供一些预期的结果吗?
    • 谢谢,dpw 的帮助。我想在一个块中将图像分成 8 像素 x 8 像素 = 64 像素的块,我已经用我之前发布的代码完成了。从每个块中,我想列出 RGB(112、231、222)中的像素颜色并将其转换为亮度以获得像素颜色强度我以这种方式执行 - (.299 * red) + (.587 * green) + ( .114 * 蓝色),当我从每个块中获得颜色强度时,我想找到最亮的块。但是在我将图像划分为块之后,我只获得块像素坐标,如何获得像素颜色?我希望我的问题更清楚对不起我的英语。
    猜你喜欢
    • 2023-03-08
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多