OpenCV: Create mask for certificate


I'm currently working on my first project in regards to Image Processing and OpenCV.
I'm using [OpenCvSharp][1] as an wrapper for C#, but I'm able to translate code from any other programming language like Python or C++.

Description

For my project, I receive images (photos or scans) of certificates, my job now is to mask, cut out and then transform them.

Currently I have some problems with the first task, masking the documents.

My main problem is to find an algorithm which will work for each image.

(Shadow, Light or Dark Background …)

Here are some example images which I might get:

(I applied some Opening morphologic transformations to remove personal data)

for (int i = 1; i < 15; i++)
    using (Mat kernel = Mat.Ones(new Size(2 * i + 1, 2 * i + 1), MatType.CV_8U))
        mask = mask.MorphologyEx(MorphTypes.Open, kernel, null, 1);

As you can see, there is a variety of different types of images.

What have I tried

For images with an dark background I’m able to generate the mask using a simple Threshold:

mask = mask
    .CvtColor(ColorConversionCodes.BGR2GRAY)
    .Threshold(0, 255, ThresholdTypes.Otsu);

As you can see, this only works when the contrast between certificate and background is big enough.

Otherwise the mask will just be garbage.

I also tried some algorithms for Edge Detection (Canny, Sobel and Laplacian) and extract the contour of the certificate.

But the results of these were unfortunately not satisfactory either.

(I could upload some images of them if you want me to)

Additionally I tried the GrabCut algorithm to generate the mask, but this also just worked for some of the pictures.

Question

I wonder if someone could give me some hints on how I could go about masking these images.

Nice greetings and thanks for your help,

fishy_coding

related: