python中直方图-Numpy,Python中的“拉伸”直方图(级别)

这是一种方法-

def stretch(a,lower_thresh,upper_thresh):

r = 255.0/(upper_thresh-lower_thresh+2) # unit of stretching

out = np.round(r*(a-lower_thresh+1)).astype(a.dtype) # stretched values

out[a

out[a>upper_thresh] = 255

return out

根据OP,设置的标准是:

>将246以上的每个像素“移动”到255,因此247及以上的像素应变为255.

> 186以下的每个像素都为零,因此185以下的像素应变为0.

>因此,基于上述两个要求,186应该变为大于0的值,依此类推,直到246应该小于255.

另外,我们也可以使用np.where使它更紧凑-

def stretch(a,upper_thresh):

r = 255.0/(upper_thresh-lower_thresh+2) # unit of stretching

out = np.round(r*np.where(a>=lower_thresh,a-lower_thresh+1,0)).clip(max=255)

return out.astype(a.dtype)

样品运行-

# check out first row input,output for variations

In [216]: a

Out[216]:

array([[186,187,188,246,247],[251,195,103,9,211],[ 21,242,36,87,70]],dtype=uint8)

In [217]: stretch(a,lower_thresh=186,upper_thresh=246)

Out[217]:

array([[ 4,8,12,251,255],[255,41,107],[ 0,234,0]],dtype=uint8)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值