简体   繁体   English

如何去除 opencv 中图像的眩光?

[英]How to remove glare from images in opencv?

This mathematica code removes glare from an image:这个mathematica代码从图像中去除眩光:

img = Import["foo.png"]
Inpaint[img, Dilation[saturated, DiskMatrix[20]]]

as shown in the most upvoted answer here:如此处最受好评的答案所示:

https://dsp.stackexchange.com/questions/1215/how-to-remove-a-glare-clipped-brightness-from-an-image https://dsp.stackexchange.com/questions/1215/how-to-remove-a-glare-clipped-brightness-from-an-image

I want to use opencv instead of Mathematica to get the same result.我想使用 opencv 而不是 Mathematica 来获得相同的结果。 How would I write equivalent code in opencv-python?我将如何在 opencv-python 中编写等效代码?

Here is how to do that in Python/OpenCV.以下是如何在 Python/OpenCV 中执行此操作。

But I do not think the OpenCV inpainting routines are working or at least are not working well for my Python 3.7.5 and OpenCV 3.4.8.但我认为 OpenCV 修复例程不起作用,或者至少对于我的 Python 3.7.5 和 Z5BD4C87976F48E6A53919D53E14025E 不起作用。

Input:输入:

在此处输入图像描述

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)


Thresholded image:阈值图像:

在此处输入图像描述

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM