我已经使用opencv和c++从图像中去除水印,使用下面的代码。
#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); }
}
}
这是删除水印的原始结果。
现在在前面的图像中,你可以看到原始图像有橙色/红色的水印。我创建了一个可以杀死水印的蒙版,然后将其应用于原始图像(这也会拉出灰色文本边界)。另一个有用的技巧是使用红色通道,因为水印在红色~245上最饱和)。请注意,这需要opencv和c++17
但是现在我想要删除新图像中的水印,它具有与文本相似的水印颜色,下面给出了图像,因为你可以在中文图像中看到一些水印与文本的横向重叠。如何用我目前的代码来实现它,任何帮助都是非常感谢的。
发布于 2021-09-23 23:18:57
有两个想法可以尝试:
1:水印看起来比原始文本“浅”。因此,如果您创建了图像的灰度版本,则可以应用一个阈值来保留主要文本并删除水印。在将蒙版应用于原始图像之前,您可能想要在该蒙版上添加一次膨胀,因为灰色阈值可能会对非水印字符进行一点裁剪。(这可能会从水印中引入太多噪声,所以请测试它)
2:尝试使用opencv opening function。您的主要文本似乎比水印厚,所以您应该能够隔离它。同样,在创建keep文本的蒙版后,展开一次并遮罩原始图像。
https://stackoverflow.com/questions/69307684
复制相似问题