此 mathematica 代码可消除图像中的眩光:
img = Import["foo.png"]
Inpaint[img, Dilation[saturated, DiskMatrix[20]]]
Run Code Online (Sandbox Code Playgroud)
正如这里得到最多支持的答案所示:
https://dsp.stackexchange.com/questions/1215/how-to-remove-a-glare-clipped-brightness-from-an-image
我想使用 opencv 而不是 Mathematica 来获得相同的结果。我如何在 opencv-python 中编写等效代码?
以下是如何在 Python/OpenCV 中执行此操作。
但我认为 OpenCV 修复例程不起作用,或者至少对我的 Python 3.7.5 和 OpenCV 3.4.8 不起作用。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('apple.png')
# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold grayscale image to extract glare
mask = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY)[1]
# Optionally add some morphology close and open, if desired
#kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
#mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations=1)
#kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
#mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)
# use mask with input to do inpainting
result = cv2.inpaint(img, mask, 21, cv2.INPAINT_TELEA)
# write result to disk
cv2.imwrite("apple_mask.png", mask)
cv2.imwrite("apple_inpaint.png", result)
# display it
cv2.imshow("IMAGE", img)
cv2.imshow("GRAY", gray)
cv2.imshow("MASK", mask)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
阈值图像:
结果:
| 归档时间: |
|
| 查看次数: |
7998 次 |
| 最近记录: |