将数字中的白色虚线点替换为图像中的黑色?

时间:2019-07-18 08:31:18

标签: visual-c++ opencv3.0

我想自动化将一组图像输入到数字生成系统中的任务,在此之前,我想删除点状水印(RECT),这在这些图像中很常见,如下所示。 ..

在Google上进行了大量搜索并尝试了多种语言的代码示例后,我在这篇文章中遇到了@dhanushka共享的最有用的代码(Opencv和VC ++): How to erase the dotted watermark from set of similar images?

编辑后的工作代码(Opencv和VC ++):

#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/photo.hpp>
#include <iostream>

using namespace cv;
using namespace std;

Mat im, bg, bw, dark, kernel2, image_out, dst;

int main(int argc, char* argv[])
{           
    // load image
    im = imread("d:\\Docs\\WFH_Work\\Companies\\Manthan-Solutions\\test1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    if (im.rows > 0)
    {           
        // make a copy & approximate the background
        bg = im.clone();            
        for (int r = 1; r < 10; r++) // 8 better, 10 best
        {                           
            kernel2 = getStructuringElement(MORPH_RECT, Size(2 * r + 1, 2 * r + 1));
            morphologyEx(bg, bg, CV_MOP_CLOSE, kernel2);                
        }

        // find the difference = background - initial
        Mat dif = bg - im;
        // threshold the difference image so we get dark letters, CV_THRESH_BINARY_INV CV_THRESH_OTSU
        threshold(dif, bw, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

        // threshold the background image so we get dark region, CV_THRESH_OTSU 
        threshold(bg, dark, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

        // extract pixels in the dark region
        vector<unsigned char>darkpix(countNonZero(dark));
        int index = 0;
        for (int r = 0; r < dark.rows; r++)
        {
            for (int c = 0; c < dark.cols; c++)
            {
                if (dark.at<unsigned char>(r, c))
                {
                    darkpix[index++] = im.at<unsigned char>(r, c);
                }
            }
        }
        // threshold the dark region so we get the darker pixels inside it, CV_THRESH_BINARY
        threshold(darkpix, darkpix, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);         

        // paste the extracted darker pixels
        index = 0;
        for (int r = 0; r < dark.rows; r++)
        {
            for (int c = 0; c < dark.cols; c++)
            {
                if (dark.at<unsigned char>(r, c))
                {
                    bw.at<unsigned char>(r, c) = darkpix[index++];
                }
            }
        }
        // identify img dimensions
        int top = (int)(0.05*bw.rows); 
        int bottom = (int)(0.05*bw.rows);
        int left = (int)(0.05*bw.cols); 
        int right = (int)(0.05*bw.cols);

        top = (int)(0.05*bw.rows); bottom = top;
        left = (int)(0.05*bw.cols); right = left;

        // add extra white border for ocr
        cv::copyMakeBorder(bw, image_out, top, bottom, left, right, cv::BORDER_CONSTANT, Scalar(255, 255, 255));

        cv::imshow(filenm, image_out);          

    waitKey(0);
    return 0;
    }
}

我根据自己的要求进行了相关的代码更改,执行后得到了下图...

现在,我只是停留在问题的最后一部分,我想通过在数字边框附近填充缺失的黑点来使数字更黑,更易读。因此,请分享一些示例代码,以便我可以继续进行下去...

在此先感谢您帮助我...

0 个答案:

没有答案