【问题标题】:Get In Focus Pixels of an Image获得图像的焦点像素
【发布时间】:2020-03-26 17:13:49
【问题描述】:

与模糊像素相比,如何检测图像的哪些像素处于焦点位置。像很多相机都有的“对焦峰值”功能吗?

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

【问题讨论】:

    标签: python opencv image-processing computer-vision signal-processing


    【解决方案1】:

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

    所以,从这张图片开始:

    图片来源: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
    

    【讨论】:

    • 有没有办法调整 PIL 模块中边缘检测的参数?还是我应该使用其他东西来找到边缘?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多