如何使用 OpenCV 从扫描图像中去除阴影? [英] How to remove shadow from scanned images using OpenCV?

查看:41
本文介绍了如何使用 OpenCV 从扫描图像中去除阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 OpenCV 进行图像二值化之前去除阴影.我尝试过大津法和自适应阈值法,但是对于阴影区域较大的图像,这两种方法效果不佳.

有更好的解决方案吗?提前致谢.

]

解决方案

由于你没有指定任何语言,我假设用 Python 来说明.

一个不错的起点可能是采用我在

以及归一化的结果:

I'd like to remove shadow before image binarization using 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

]2

解决方案

Since you didn't specify any language, I'll assume Python to illustrate.

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:

这篇关于如何使用 OpenCV 从扫描图像中去除阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆