使用 python/opencv/deep 学习从图像中删除 logo/watermark 个给定位置
Remove logo/watermark given locations from an image using python/opencv/deep learning
我对使用 python 的 opencv 和深度学习还很陌生。我正在尝试从图像中删除 watermark/logo。我可以通过在图像中找到原始图像中裁剪水印图像的位置来找到水印的位置,这对所有图像都是不变的。我需要删除找到的水印。
这里是原图:[![original_image.jpeg][1]][1]
原图2:[![enenter code here
此处为图片说明][2]][2]
原图3:[![在此处输入图片描述][3]][3]
从原始图像裁剪的水印图像:[![cropped_image.jpeg][4]][4]
图片中找到水印:
[![定位水印][5]][5]
我尝试了各种使用 tensorflow/deep 学习的代码,这些代码在 运行 时无法概括并给出各种错误。
例如,我尝试了自动水印检测(https://github.com/rohitrango/automatic-watermark-detection),但它不起作用。此库中的 crop_watermark() 函数不适用于我的 image.It 正在裁剪图像的其他部分,这不是水印还有许多其他代码问题。
类似地,我尝试了很多其他的深度学习库,但都没有成功。
我也想尝试 cv2.inpaint(img,mask,3,cv2.INPAINT_TELEA)
但我没有遮罩图像。我只有一张带有水印的图像,如下所示,因此也无法使用 inpaint()
功能。
目前正在尝试使用下面的简单代码找出图像中水印的确切位置(通过手动裁剪水印并在原始图像中找到位置)。
import numpy as np
import cv2
img = cv2.imread('original_image.jpeg')
print(img.shape)
h,w,c = img.shape
logo = cv2.imread("cropped_image.jpeg")
print(logo.shape)
hl,wl,cl = logo.shape
x1 = int(w/2-wl/2)
y1 = int(h/2-hl)
x2 = int(w/2+wl/2)
y2 = int(h/2)
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
cv2.imwrite("my.png",img)
cv2.imshow("lalala", img)
以上代码能够找到正确的水印坐标。从这里开始我不知道如何继续去除水印。如果您能提供一些示例代码以及概念,那就太好了。
感谢您的帮助。
你可以试试OpenCV的inpaint()
函数contrib_module,你首先需要创建一个mask并指出图像上logo所在的区域,然后将图像和掩码,然后将结果存储在目标图像中。
@param src source image, it could be of any type and any number of channels from 1 to 4. In case of
3- and 4-channels images the function expect them in CIELab colorspace or similar one, where first
color component shows intensity, while second and third shows colors. Nonetheless you can try any
colorspaces.
@param mask mask (CV_8UC1), where non-zero pixels indicate valid image area, while zero pixels
indicate area to be inpainted
@param dst destination image
@param algorithmType see xphoto::InpaintTypes
*/
CV_EXPORTS_W void inpaint(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType);
创建遮罩的一些提示:当您使用某些工具(图像编辑器)创建遮罩图像时,背景必须为黑色,徽标区域必须为白色.然后当之前创建的图像用作蒙版时,您应该先将图像转换为灰色,然后使用 THRESH_BINARY
标志对图像进行阈值处理。
更新: 实现,这是代码,是用C++写的,不过你可以考虑一下步骤就完事了一样。
cv::namedWindow("Original_Image", cv::WINDOW_FREERATIO);
cv::namedWindow("Result", cv::WINDOW_FREERATIO);
cv::Mat originalImg = cv::imread("y25av.jpg");
cv::Mat mask = cv::imread("mask.jpg");
// to gray
cv::Mat gray;
cv::cvtColor(mask, gray, cv::COLOR_BGR2GRAY);
cv::threshold(gray, mask, 180, 255, cv::THRESH_BINARY);
cv::Mat dst;
cv::inpaint(originalImg, mask, dst, 10, cv::INPAINT_TELEA);
cv::imshow("Original_Image", originalImg);
cv::imshow("Result", dst);
cv::waitKey();
您的原图:
使用的掩码:
最终结果:
我对使用 python 的 opencv 和深度学习还很陌生。我正在尝试从图像中删除 watermark/logo。我可以通过在图像中找到原始图像中裁剪水印图像的位置来找到水印的位置,这对所有图像都是不变的。我需要删除找到的水印。
这里是原图:[![original_image.jpeg][1]][1]
原图2:[![enenter code here
此处为图片说明][2]][2]
原图3:[![在此处输入图片描述][3]][3]
从原始图像裁剪的水印图像:[![cropped_image.jpeg][4]][4]
图片中找到水印: [![定位水印][5]][5] 我尝试了各种使用 tensorflow/deep 学习的代码,这些代码在 运行 时无法概括并给出各种错误。
例如,我尝试了自动水印检测(https://github.com/rohitrango/automatic-watermark-detection),但它不起作用。此库中的 crop_watermark() 函数不适用于我的 image.It 正在裁剪图像的其他部分,这不是水印还有许多其他代码问题。
类似地,我尝试了很多其他的深度学习库,但都没有成功。
我也想尝试 cv2.inpaint(img,mask,3,cv2.INPAINT_TELEA)
但我没有遮罩图像。我只有一张带有水印的图像,如下所示,因此也无法使用 inpaint()
功能。
目前正在尝试使用下面的简单代码找出图像中水印的确切位置(通过手动裁剪水印并在原始图像中找到位置)。
import numpy as np
import cv2
img = cv2.imread('original_image.jpeg')
print(img.shape)
h,w,c = img.shape
logo = cv2.imread("cropped_image.jpeg")
print(logo.shape)
hl,wl,cl = logo.shape
x1 = int(w/2-wl/2)
y1 = int(h/2-hl)
x2 = int(w/2+wl/2)
y2 = int(h/2)
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
cv2.imwrite("my.png",img)
cv2.imshow("lalala", img)
以上代码能够找到正确的水印坐标。从这里开始我不知道如何继续去除水印。如果您能提供一些示例代码以及概念,那就太好了。
感谢您的帮助。
你可以试试OpenCV的inpaint()
函数contrib_module,你首先需要创建一个mask并指出图像上logo所在的区域,然后将图像和掩码,然后将结果存储在目标图像中。
@param src source image, it could be of any type and any number of channels from 1 to 4. In case of
3- and 4-channels images the function expect them in CIELab colorspace or similar one, where first
color component shows intensity, while second and third shows colors. Nonetheless you can try any
colorspaces.
@param mask mask (CV_8UC1), where non-zero pixels indicate valid image area, while zero pixels
indicate area to be inpainted
@param dst destination image
@param algorithmType see xphoto::InpaintTypes
*/
CV_EXPORTS_W void inpaint(const Mat &src, const Mat &mask, Mat &dst, const int algorithmType);
创建遮罩的一些提示:当您使用某些工具(图像编辑器)创建遮罩图像时,背景必须为黑色,徽标区域必须为白色.然后当之前创建的图像用作蒙版时,您应该先将图像转换为灰色,然后使用 THRESH_BINARY
标志对图像进行阈值处理。
更新: 实现,这是代码,是用C++写的,不过你可以考虑一下步骤就完事了一样。
cv::namedWindow("Original_Image", cv::WINDOW_FREERATIO);
cv::namedWindow("Result", cv::WINDOW_FREERATIO);
cv::Mat originalImg = cv::imread("y25av.jpg");
cv::Mat mask = cv::imread("mask.jpg");
// to gray
cv::Mat gray;
cv::cvtColor(mask, gray, cv::COLOR_BGR2GRAY);
cv::threshold(gray, mask, 180, 255, cv::THRESH_BINARY);
cv::Mat dst;
cv::inpaint(originalImg, mask, dst, 10, cv::INPAINT_TELEA);
cv::imshow("Original_Image", originalImg);
cv::imshow("Result", dst);
cv::waitKey();
您的原图:
使用的掩码:
最终结果: