python+opencv——去除图像光照不均匀
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
·
使用python版本的opencv去除图像中的光照不均匀
在图像处理中,如果图像中存在光照不均匀,则会影响图像处理的效果,比如在图像文本识别和图像分割中。本博客对于图像均衡化的处理主要参考文章:一种基于亮度均衡的图像阈值分割技术,以有关于C++的实现代码:opencv 一种不均匀光照的补偿方法
使用该方法主要的原因是最近在弄Tesseract的文字识别,需要识别拍摄照片中的字母,但是照片为室外拍摄,具有不同的光照影响,导致识别率很低,因此采用该方法进行处理,最后有效的提升了识别率,实现有光照影响的Tesseract文字识别。
全部代码如下:
import cv2
import numpy as np
def unevenLightCompensate(img, blockSize):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
average = np.mean(gray)
rows_new = int(np.ceil(gray.shape[0] / blockSize))
cols_new = int(np.ceil(gray.shape[1] / blockSize))
blockImage = np.zeros((rows_new, cols_new), dtype=np.float32)
for r in range(rows_new):
for c in range(cols_new):
rowmin = r * blockSize
rowmax = (r + 1) * blockSize
if (rowmax > gray.shape[0]):
rowmax = gray.shape[0]
colmin = c * blockSize
colmax = (c + 1) * blockSize
if (colmax > gray.shape[1]):
colmax = gray.shape[1]
imageROI = gray[rowmin:rowmax, colmin:colmax]
temaver = np.mean(imageROI)
blockImage[r, c] = temaver
blockImage = blockImage - average
blockImage2 = cv2.resize(blockImage, (gray.shape[1], gray.shape[0]), interpolation=cv2.INTER_CUBIC)
gray2 = gray.astype(np.float32)
dst = gray2 - blockImage2
dst = dst.astype(np.uint8)
dst = cv2.GaussianBlur(dst, (3, 3), 0)
dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
return dst
if __name__ == '__main__':
file = 'refined_receipt.jpg'
blockSize = 16
img = cv2.imread(file)
dst = unevenLightCompensate(img, blockSize)
result = np.concatenate([img, dst], axis=1)
cv2.imshow('result', result)
cv2.waitKey(0)
效果如下所示:
调节的参数有blockSize的尺寸,以及最后的高斯去噪GaussianBlur和其核的大小,本文取的3.
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
6950bedb
Use Mat::total() in Darknet IO 2 天前
105a7747
doc: update image codec configuration reference (add AVIF, fix JPEG XL) #28393
This PR updates the build configuration documentation to:
- Add AVIF to the supported format list (it was missing).
- Clarify that some codecs (AVIF, JPEG XL) do not support BUILD_* options because OpenCV does not bundle their source code.
- Update the description to include "write" capabilities for these formats.
### 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
- [x] 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.
- [x] The feature is well documented and sample code can be built with the project CMake
3 天前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)