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)
總結
以上是生活随笔為你收集整理的python中直方图-Numpy,Python中的“拉伸”直方图(级别)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK源码——JDK8源码编译全流程(l
- 下一篇: python自然语言处理案例-Pytho