获取图像的焦点像素

Get In Focus Pixels of an Image

如何检测图像中哪些像素清晰,哪些像素模糊。许多相机都有类似 'Focus Peaking' 的功能?

我们的想法是为聚焦的像素着色,以便在用户单击图片时提供帮助。通过 Python.

寻找实现

您可以找到锐利或高对比度的边缘,然后将它们叠加到原始图像上。

因此,从这张图片开始:

图片来源:Rita Kochmarjova - Fotolia

你可以这样做:

#!/usr/bin/env python3

import numpy as np
from PIL import Image, ImageFilter, ImageChops

# Open input image and make greyscale copy
image = Image.open('bulldog.jpg')
grey  = image.copy().convert('L')

# Find the edges
edges = grey.filter(ImageFilter.FIND_EDGES)
edges.save('edges.png')

# Draw the sharp edges in white over original
RGBedges = Image.merge('RGB',(edges,edges,edges))
image.paste(RGBedges, mask=edges)

# Save
image.save('result.png')

水边的石头效果最明显。

这里是中间件edges.png。您可以稍微扩大白色像素或阈值以使对焦部分更加清晰。


这里放大了一点边缘,使它们更明显:

#!/usr/bin/env python3

import numpy as np
from PIL import Image, ImageFilter
from skimage.morphology import dilation, square

# Open input image and make greyscale copy
image = Image.open('bulldog.jpg')
grey  = image.copy().convert('L')

# Find the edges
edges = grey.filter(ImageFilter.FIND_EDGES)

# Define a structuring element for dilation
selem = square(3)
fatedges = dilation(np.array(edges),selem)
fatedges = Image.fromarray(fatedges)
fatedges.save('edges.png')

# Draw the sharp edges in white over original
RGBedges = Image.merge('RGB',(fatedges,fatedges,fatedges))
image.paste(RGBedges, mask=fatedges)

# Save
image.save('result.png')


您也可以在终端中使用 ImageMagick 完成此操作而无需编写任何代码:

magick bulldog.jpg \( +clone -canny 0x1+10%+30% \) -compose overlay -composite  result.png

或者这个,更类似于Python:

magick bulldog.jpg \( +clone -canny 0x1+10%+30% \) -compose lighten -composite  result.png