简体   繁体   English

如何使用 OpenCV 从扫描图像中去除阴影?

[英]How to remove shadow from scanned images using OpenCV?

I'd like to remove shadow before image binarization using OpenCV.我想在使用 OpenCV 进行图像二值化之前去除阴影。 I've tried Otsu Method and adaptive thresholding, however for images where there are large regions of shadow, these two methods will not give good results.我试过大津法和自适应阈值法,但是对于有大面积阴影的图像,这两种方法不会给出很好的结果。

Any better solutions?有什么更好的解决方案吗? Thanks in advance.提前致谢。

[示例图像 ] 1 ] 1

[示例图像 ] 2 ] 2

Since you didn't specify any language, I'll assume Python to illustrate.由于您没有指定任何语言,我将假设 Python 来说明。

A decent starting point might be taking the approach I show in this answer and expand it to work with multiple channels.一个不错的起点可能是采用我在此答案中展示的方法并将其扩展为使用多个渠道。

Something along the lines of类似的东西

import cv2
import numpy as np

img = cv2.imread('shadows.png', -1)

rgb_planes = cv2.split(img)

result_planes = []
result_norm_planes = []
for plane in rgb_planes:
    dilated_img = cv2.dilate(plane, np.ones((7,7), np.uint8))
    bg_img = cv2.medianBlur(dilated_img, 21)
    diff_img = 255 - cv2.absdiff(plane, bg_img)
    norm_img = cv2.normalize(diff_img,None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
    result_planes.append(diff_img)
    result_norm_planes.append(norm_img)

result = cv2.merge(result_planes)
result_norm = cv2.merge(result_norm_planes)

cv2.imwrite('shadows_out.png', result)
cv2.imwrite('shadows_out_norm.png', result_norm)

The non-normalized result looks as follows:非标准化结果如下所示:

在此处输入图片说明

And the normalized result:和归一化的结果:

在此处输入图片说明

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

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