gpt4 book ai didi

python - 获取图像的焦点像素

转载 作者:行者123 更新时间:2023-12-02 16:08:28 24 4
gpt4 key购买 nike

与模糊像素相比,如何检测图像中哪些像素清晰。许多相机都具有类似“对焦峰值”的功能?

想法是为焦点像素着色,以便在用户单击图片时提供帮助。寻找通过 Python 的实现。

最佳答案

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

因此,从这张图片开始:

enter image description here图片来源: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')

enter image description here

您可以在水边的石头中最清楚地看到这种效果。

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

enter image description here


这里稍微扩大了边缘以使其更加明显:

#!/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')

enter image description here


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

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

enter image description here

或者这个,更类似于Python:

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

enter image description here

关于python - 获取图像的焦点像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60872630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com