你如何将图像/数组分成块? [英] How do you divide an image/array into blocks?

查看:45
本文介绍了你如何将图像/数组分成块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将图像分成块,例如 8x8 块(每块 64 像素)并对每个块执行直方图功能并将结果保存到新图像中而不是单独图像?

I am wondering is it possible to divide image into blocks for example 8x8 blocks (64 pixels per block) and perform histogram function for each block and save results into a new image not to separately images?

def apply_histogram(block):
    h, b = np.histogram(block.flatten(), 256, normed=True)
    cdf = h.cumsum()
    cdf = 255 * cdf / cdf[-1]
    return np.interp(block.flatten(), b[:-1], cdf).reshape(block.shape)

推荐答案

为什么不循环遍历图像中的所有 8x8 块?

Why not loop through all 8x8 blocks in the image?

image = ...
block_img = np.zeros(image.shape)
im_h, im_w = image.shape[:2]
bl_h, bl_w = 8, 8

for row in np.arange(im_h - bl_h + 1, step=bl_h):
    for col in np.arange(im_w - bl_w + 1, step=bl_w):
        block_img[row:row+bl_h, col:col+bl_w] = apply_histogram(image[row:row+bl_h, col:col+bl_w])

图片:

block_img:

这篇关于你如何将图像/数组分成块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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