use*_*510 5 python image pillow
我正在尝试在 python 枕头中进行一些图像堆叠。我想做的是拍摄大量图像(比如 10 张),然后对于每个像素,取这样的中值:http : //petapixel.com/2013/05/29/a-look-at -减少照片中的噪点-使用中值混合/。
现在,我可以以令人难以置信的蛮力方式(使用 getpixel 和 put pixel)来完成它,但这需要很长时间。
这是我到目前为止所拥有的:
import os
from PIL import Image
files = os.listdir("./")
new_im = Image.new('RGB', (4000,3000))
ims={}
for i in range(10,100):
ims[i]={}
im=Image.open("./"+files[i])
for x in range(400):
ims[i][x]={}
for y in range(300):
ims[i][x][y]=im.getpixel((x,y))
for x in range(400):
for y in range(300):
these1=[]
these2=[]
these3=[]
for i in ims:
these1.append(ims[i][x][y][0])
these2.append(ims[i][x][y][1])
these3.append(ims[i][x][y][2])
these1.sort()
these2.sort()
these3.sort()
new_im.putpixel((x,y),(these1[len(these1)/2],these2[len(these2)/2],these3[len(these3)/2]))
new_im.show()
Run Code Online (Sandbox Code Playgroud)
你可以用数组向量化很多这样的循环。例如,np.array(im)将返回一个像素数组,形状为 (400, 300, 3)。所以把所有的东西都存储在一个数组中。
image_stacks = np.zeros(shape=(10, 400, 300, 3), dtype=np.uint8)
for i in xrange(image_stacks.shape[0]):
# open image file and store in variable `im`, then
image_stacks[i] = np.array(im)
Run Code Online (Sandbox Code Playgroud)
现在你可以用你喜欢的方式计算中位数,但 numpy 也有一个方法。
image_median = np.median(image_stacks, axis=0).astype(np.uint8)
image = Image.fromarray(image_median)
Run Code Online (Sandbox Code Playgroud)
这里要注意的两件事是它np.median()会返回一个浮点型数组,我们希望将其转换为 unsigned int8。另一件事是,如果元素数是偶数,则中位数计算为两个中间值的平均值,最终可能是奇数除以 2,例如 13.5。但是当它转换为整数时,它会被四舍五入。这种微小的精度损失不应在视觉上影响您的结果。
| 归档时间: |
|
| 查看次数: |
5020 次 |
| 最近记录: |