栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用numpy高效地将16位图像数据转换为8位进行显示,并进行强度缩放

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用numpy高效地将16位图像数据转换为8位进行显示,并进行强度缩放

你要做的是半色调你的
形象。
其他人提出的方法效果很好,但它们重复了很多次一次又一次的昂贵计算。因为在“uint16”中
大多数65536个不同的值,使用查找表(LUT)可以简化很多事情。既然LUT很小,你就不用担心那么多
关于就地操作,或者不创建布尔数组。以下代码重用Bi-Rico的函数来创建LUT:

import numpy as npimport timeitrows, cols = 768, 1024image = np.random.randint(100, 14000,       size=(1, rows, cols)).astype(np.uint16)display_min = 1000display_max = 10000def display(image, display_min, display_max): # copied from Bi Rico    # Here I set copy=True in order to ensure the original image is not    # modified. If you don't mind modifying the original image, you can    # set copy=False or skip this step.    image = np.array(image, copy=True)    image.clip(display_min, display_max, out=image)    image -= display_min    np.floor_divide(image, (display_max - display_min + 1) / 256,         out=image, casting='unsafe')    return image.astype(np.uint8)def lut_display(image, display_min, display_max) :    lut = np.arange(2**16, dtype='uint16')    lut = display(lut, display_min, display_max)    return np.take(lut, image)>>> np.all(display(image, display_min, display_max) ==lut_display(image, display_min, display_max))True>>> timeit.timeit('display(image, display_min, display_max)',       'from __main__ import display, image, display_min, display_max',        number=10)0.304813282062>>> timeit.timeit('lut_display(image, display_min, display_max)',       'from __main__ import lut_display, image, display_min, display_max',       number=10)0.0591987428298

So there is a x5 speed-up, which is not a bad thing, I guess…



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号