简体   繁体   English

使用 opencv 从图像中移除 flash 眩光

[英]Remove flash glare from image using opencv

I'm working on extracting the the electricity meter number from a photos of a watt-hour meter using OpenCV python.我正在使用 OpenCV python 从电能表的照片中提取电表编号。 I have the photos cropped/detected using Yolov4 like this: The original Image我使用 Yolov4 裁剪/检测到照片,如下所示:原始图像

Then I do some operations on it including, making it gray, blur, thresholding, and then closing.然后我对其进行一些操作,包括使其变灰、模糊、阈值化,然后关闭。 Photos without glare are working fine.没有眩光的照片效果很好。 But the image like the one above is not.但像上面的图像不是。

`#gray scaling gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY) `#灰度缩放 gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)

blur_img = cv.GaussianBlur(gray_img, (5,5), 0) blur_img = cv.GaussianBlur(gray_img, (5,5), 0)

rect_kern = cv.getStructuringElement(cv.MORPH_RECT, (5,5)) rect_kern = cv.getStructuringElement(cv.MORPH_RECT, (5,5))

ret, thresh = cv.threshold(blur_img, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV) ret, thresh = cv.threshold(blur_img, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY_INV)

closing = cv.morphologyEx(thresh, cv.MORPH_CLOSE, rect_kern, iterations=2)关闭= cv.morphologyEx(thresh,cv.MORPH_CLOSE,rect_kern,迭代=2)

dilation = cv.dilate(thresh, rect_kern, iterations = 1)膨胀 = cv.dilate(thresh, rect_kern, 迭代 = 1)

#finding the contours #寻找轮廓

contours, hierarchy = cv.findContours(closing, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)轮廓,层次结构 = cv.findContours(关闭,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)

sorted_contours = sorted(contours, key=lambda ctr: cv.boundingRect(ctr)[0])` sorted_contours = sorted(contours, key=lambda ctr: cv.boundingRect(ctr)[0])`

Then after that, I compute the ratio of the width and height of every contour to find the numbers and pass them to the ocr, but that's not my purpose of asking this question.然后,我计算每个轮廓的宽度和高度的比率以找到数字并将它们传递给 ocr,但这不是我问这个问题的目的。 The result of the above image and the above code is the following.上图和上面代码的结果如下。

The final image before reading with the ocr用ocr读取前的最终图像

As you can see around the number 9 and 1 there is the glare which resulted in not being able to read the numbers.正如您在数字 9 和 1 周围看到的那样,有眩光导致无法读取数字。 Is there anyone to solve this issue.有没有人来解决这个问题。 I'm open to any kind of advice, even probably using something other than OpenCV.我愿意接受任何形式的建议,甚至可能使用 OpenCV 以外的东西。 Btw I'm using tesseract as an ocr.顺便说一句,我正在使用 tesseract 作为 ocr。

Glare is in fact destroying any information locally, in an unrecoverable way. Glare 实际上是以不可恢复的方式在本地破坏任何信息。

A solution could be to use an OCR engine that allows "don't know" pixels, but AFAIK Tesseract does not support that.一种解决方案可能是使用允许“不知道”像素的 OCR 引擎,但 AFAIK Tesseract 不支持这一点。

The damage is lessened if you turn the bright pixels to dark before other processing.如果在其他处理之前将亮像素变为暗像素,则可以减少损坏。

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

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