图像照明校正

Ros*_*han 6 c# image image-processing aforge emgucv

我有使用相机拍摄的图像。有时,图像中的光线不均匀。有一些深色阴影。这会导致EMGU以及Aforge处理OCR图像的最佳阈值设置错误。

这是图像: 在此处输入图片说明

这是我获得阈值后得到的:

在此处输入图片说明

我该如何校正照明?我尝试了自适应阈值,结果大致相同。也使用以下代码尝试了伽玛校正:

 ImageAttributes attributes = new ImageAttributes();
            attributes.SetGamma(10);

            // Draw the image onto the new bitmap
            // while applying the new gamma value.
            System.Drawing.Point[] points =
   {
    new System.Drawing.Point(0, 0),
    new System.Drawing.Point(image.Width, 0),
    new System.Drawing.Point(0, image.Height),
   };
            Rectangle rect =
                new Rectangle(0, 0, image.Width, image.Height);

            // Make the result bitmap.
            Bitmap bm = new Bitmap(image.Width, image.Height);
            using (Graphics gr = Graphics.FromImage(bm))
            {
                gr.DrawImage(HSICONV.Bitmap, points, rect,
                    GraphicsUnit.Pixel, attributes);
            }
Run Code Online (Sandbox Code Playgroud)

同样的结果。请帮忙。

更新:根据Nathancy的建议,我将他的代码转换为c#以进行不均匀的照明校正,并且它的工作原理是:

   Image<Gray, byte> smoothedGrayFrame = grayImage.PyrDown();
                smoothedGrayFrame = smoothedGrayFrame.PyrUp();
                //canny
                Image<Gray, byte> cannyFrame = null;

                cannyFrame = smoothedGrayFrame.Canny(50, 50);
                //smoothing

                grayImage = smoothedGrayFrame;
                //binarize
                Image<Gray, byte> grayout = grayImage.Clone();
                CvInvoke.AdaptiveThreshold(grayImage, grayout, 255, AdaptiveThresholdType.GaussianC, ThresholdType.BinaryInv, Convert.ToInt32(numericmainthreshold.Value) + Convert.ToInt32(numericmainthreshold.Value) % 2 + 1, 1.2d);
                grayout._Not();
                Mat kernelCl = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new System.Drawing.Point(-1, -1));
                CvInvoke.MorphologyEx(grayout, grayout, MorphOp.Close, kernelCl, new System.Drawing.Point(-1, -1), 1, BorderType.Default, new MCvScalar());
Run Code Online (Sandbox Code Playgroud)

nat*_*ncy 5

这是一种方法:

  • 将图像转换为灰度,将高斯模糊转换为平滑图像
  • 自适应阈值获取二值图像
  • 执行形态转换以平滑图像
  • 扩张以增强文字
  • 反转图像

转换为灰度和模糊后,我们自适应阈值

有小孔和瑕疵,因此我们执行变形以使图像平滑

从这里我们可以选择扩展以增强文字

现在我们反转图像以获得结果

我在OpenCV和Python中实现了此方法,但是您可以将相同的策略应用于C#

import cv2

image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
         cv2.THRESH_BINARY_INV,9,11)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
dilate = cv2.dilate(close, kernel, iterations=1)
result = 255 - dilate 

cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)