在图像处理中,锐化操作用于增强图像的边缘和细节,使图像看起来更清晰。常见的图像锐化方法包括非锐化掩模(Unsharp Masking)和拉普拉斯滤波器(Laplacian Filter)。

非锐化掩模 (Unsharp Masking)

步骤

  1. 模糊图像:使用高斯模糊滤波器对原图像进行模糊处理,得到模糊图像。
  2. 计算细节层:通过从原图像中减去模糊图像,得到细节层。
  3. 增强图像:将细节层乘以一个增益系数后加回到原图像,得到增强后的图像。

公式
设原图像为 ( I ),模糊图像为 (I blur ),细节层为 ( D ),增益系数为 ( k ),最终的锐化图像 ( I’ ) 计算如下:

D = I − I blur D = I - I_{\text{blur}} D=IIblur

I ′ = I + k ⋅ D I' = I + k \cdot D I=I+kD

代码示例

import cv2
import numpy as np

def unsharp_mask(image, k=1.5):
    # 高斯模糊图像
    blurred = cv2.GaussianBlur(image, (9, 9), 10.0)
    # 计算细节层
    detail = image - blurred
    # 增强图像
    sharpened = image + k * detail
    return np.clip(sharpened, 0, 255).astype(np.uint8)

image = cv2.imread('Task3.jpg')
sharpened_image = unsharp_mask(image)
cv2.imshow('Original Image', image)
cv2.imshow('Unsharp Masked Image', sharpened_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

拉普拉斯滤波器 (Laplacian Filter)

步骤

  1. 计算拉普拉斯图像:使用拉普拉斯算子计算图像的二阶导数,得到拉普拉斯图像。
  2. 增强图像:将拉普拉斯图像加回到原图像中,得到锐化后的图像。

公式
设原图像为 ( I ),拉普拉斯图像为 ( L ),最终的锐化图像 ( I’ ) 计算如下:

L = Δ I = ∂ 2 I ∂ x 2 + ∂ 2 I ∂ y 2 L = \Delta I = \frac{\partial^2 I}{\partial x^2} + \frac{\partial^2 I}{\partial y^2} L=ΔI=x22I+y22I

I ′ = I + k ⋅ L I' = I + k \cdot L I=I+kL

代码示例

import cv2
import numpy as np

def laplacian_sharpen(image, k=1.0):
    # 计算拉普拉斯图像
    laplacian = cv2.Laplacian(image, cv2.CV_64F)
    laplacian = np.uint8(np.absolute(laplacian))
    # 增强图像
    sharpened = cv2.addWeighted(image, 1, laplacian, k, 0)
    return sharpened

image = cv2.imread('path_to_your_image.jpg')
sharpened_image = laplacian_sharpen(image)
cv2.imshow('Original Image', image)
cv2.imshow('Laplacian Sharpened Image', sharpened_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

效果对比

了将非锐化掩模(Unsharp Masking)和拉普拉斯滤波器(Laplacian Filter)的方法整合到一个代码中,并对比展示效果,将两个锐化方法的结果放在同一个窗口中进行展示

import cv2
import numpy as np
import matplotlib.pyplot as plt

def unsharp_mask(image, k=1.5):
    # 高斯模糊图像
    blurred = cv2.GaussianBlur(image, (9, 9), 10.0)
    # 计算细节层
    detail = image - blurred
    # 增强图像
    sharpened = image + k * detail
    return np.clip(sharpened, 0, 255).astype(np.uint8)

def laplacian_sharpen(image, k=1.0):
    # 计算拉普拉斯图像
    laplacian = cv2.Laplacian(image, cv2.CV_64F)
    laplacian = np.uint8(np.absolute(laplacian))
    # 增强图像
    sharpened = cv2.addWeighted(image, 1, laplacian, k, 0)
    return sharpened

def display_images(original, unsharp, laplacian):
    titles = ['Original Image', 'Unsharp Masked ', 'Laplacian Sharpened ']
    images = [original, unsharp, laplacian]
    plt.figure(figsize=(10, 10)) 
    for i in range(3):
        plt.subplot(1, 3, i + 1)
        plt.imshow(cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB))
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])

    plt.show()

def main():
    image_path = 'Task3.jpg'  # 请替换为你的图像路径
    image = cv2.imread(image_path)

    if image is None:
        print(f"Error: Unable to load image at {image_path}")
        return

    unsharp_image = unsharp_mask(image)
    laplacian_image = laplacian_sharpen(image)

    display_images(image, unsharp_image, laplacian_image)

if __name__ == "__main__":
    main()

具体效果对比如下:不同的图片的效果可能不同
在这里插入图片描述

总结

这两种锐化方法各有优缺点,要根据具体需求选择合适的方法:

  • 非锐化掩模

    • 优点:能够灵活控制图像的锐化程度,通过调整增益系数和模糊程度,可以获得较为自然的锐化效果。
    • 缺点:在处理带有高噪声的图像时,容易放大噪声。
  • 拉普拉斯滤波器

    • 优点:计算简单,能够快速增强图像边缘和细节。
    • 缺点:容易引入噪声和伪影,对噪声不敏感的图像效果更好。

通过应用这些方法,可以有效增强图像的边缘和细节,使图像看起来更加清晰和锐利。

GitHub 加速计划 / opencv31 / opencv
233
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
579dfb6e Enable KleidiCV on Linux and Mac Mx by default #27640 OpenCV Extra: https://github.com/opencv/opencv_extra/pull/1296 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
dee619b6 Update deprecated ov::element::undefined #28127 ### Summary Fixing OpenCV build error below. Relates to OpenVINO 2026.0 updates on `ov::element::undefined` (https://github.com/openvinotoolkit/openvino/pull/32573) - replaced by `ov::element::dynamic`. ``` /home/jenkins/agent/workspace/openVINO-builder/opencv_source/opencv-opencv-f627368/modules/gapi/src/backends/ov/govbackend.cpp [2025-12-03T21:25:54.540Z] /home/jenkins/agent/workspace/openVINO-builder/opencv_source/opencv-opencv-f627368/modules/gapi/src/backends/ov/govbackend.cpp: In function ���ov::element::Type toOV(int)���: [2025-12-03T21:25:54.540Z] /home/jenkins/agent/workspace/openVINO-builder/opencv_source/opencv-opencv-f627368/modules/gapi/src/backends/ov/govbackend.cpp:114:25: error: ���undefined��� is not a member of ���ov::element��� [2025-12-03T21:25:54.540Z] 114 | return ov::element::undefined; ... /home/jenkins/agent/workspace/openVINO-builder/opencv_source/opencv-opencv-f627368/modules/gapi/test/infer/gapi_infer_ov_tests.cpp [2025-12-03T21:26:19.642Z] /home/jenkins/agent/workspace/openVINO-builder/opencv_source/opencv-opencv-f627368/modules/gapi/test/infer/gapi_infer_ov_tests.cpp: In function ���ov::element::Type opencv_test::toOV(int)���: [2025-12-03T21:26:19.642Z] /home/jenkins/agent/workspace/openVINO-builder/opencv_source/opencv-opencv-f627368/modules/gapi/test/infer/gapi_infer_ov_tests.cpp:832:25: error: ���undefined��� is not a member of ���ov::element��� [2025-12-03T21:26:19.642Z] 832 | return ov::element::undefined; ``` ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 2 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐