Home > Net >  removing watermark using opencv
removing watermark using opencv

Time:09-26

I have used opencv and c to remove watermark from image using code below.

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <Windows.h>
#include <string>
#include <filesystem>

namespace fs = std::filesystem;

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
bool debugFlag = true;
std::string path = "C:/test/";
for (const auto& entry : fs::directory_iterator(path))
{
    std::string  fileName = entry.path().string();
    Mat original = imread(fileName, cv::IMREAD_COLOR);
    if (debugFlag) { imshow("original", original); }
    Mat inverted;
    bitwise_not(original, inverted);
    std::vector<Mat> channels;
    split(inverted, channels);

    for (int i = 0; i < 3; i  )
    {
        if (debugFlag) { imshow("chan"   std::to_string(i), channels[i]); }
    }

    Mat bwImg;
    cv::threshold(channels[2], bwImg, 50, 255, cv::THRESH_BINARY);
    if (debugFlag) { imshow("thresh", bwImg); }

    Mat outputImg;
    inverted.copyTo(outputImg, bwImg);

    bitwise_not(outputImg, outputImg);
    if (debugFlag) { imshow("output", outputImg); }

    if (debugFlag) { waitKey(0); }
    else { imwrite(fileName, outputImg); }
}
}

here is result original to removed watermark. enter image description here

Now in previous image as you can see original image has orange/red watermark. I created a mask that would kill the watermark and then applied it to the original image (this pulls the grey text boundary as well). Another trick that helped was to use the red channel since the watermark is most saturated on red ~245). Note that this requires opencv and c 17

But now i want to remove watermark in new image which has similar watermark color as text image is given below as you can see some watermark in image sideway in chinese overlaping with text. how to achieve it with my current code any help is appreciated. enter image description here

CodePudding user response:

Two ideas to try:

1: The watermark looks "lighter" than the primary text. So if you create a grayscale version of the image, you may be able to apply a threshold that keeps the primary text and drops the watermark. You may want to add one pass of dilation on that mask before applying it to the original image as the grey thresh will likely clip your non-watermark characters a bit. (this may pull in too much noise from the watermark though, so test it)

2: Try using the opencv opening function. Your primary text seems thicker than the watermark, so you should be able to isolate it. Similarly after you create the mask of your keep text, dilate once and mask the original image.

  • Related