gpt4 book ai didi

python - 使用图像作为卷积核

转载 作者:行者123 更新时间:2023-12-02 17:34:12 25 4
gpt4 key购买 nike

我目前正在尝试实现Google发布的this paper关于如何从图像中删除水印的结果。我创建了约80张带有自己的水印的图片(整个图片上方的字体和交叉线),并且能够使用拉普拉斯滤镜检测边缘。

我的问题是边缘不足以去除图像中的水印。当线条宽于1个像素时,边缘之间会留有间隙。该文件说:

Specifically, for a given watermarked image, we obtain a verbose edge map (using Canny edge detector), and compute its Euclidean distance transform, which is then convolved with the watermark edges (flipped horizontally and vertically) to get the Chamfer distance from each pixel to the closest edge. Lastly, the watermark position is taken to be the pixel with minimum distance in the map.



我可以使用以下代码进行距离转换:
### Detect water mark edges
imgs = glob.glob("images/*.jpg")
mean = np.zeros((1200, 1600))

for i, filename in enumerate(imgs):
img = cv2.imread(filename,0)
mean += cv2.Laplacian(img,cv2.CV_64F,ksize=3)

mean /= len(imgs)

#### Edge map & distance transform
img = cv2.imread("images/1.jpg", 0)

can = cv2.Canny(img, 100, 200)
dist = cv2.distanceTransform(can, cv2.DIST_L2, 3)

但是我现在应该如何进行卷积?我的内核应该是什么?我的水印的线条穿过整个图片,因此水印边缘图像的尺寸与原始图像的尺寸相同。

基于@Cris Luengo的 编辑答案:
_, mean = cv2.threshold(mean, 64, 255, cv2.THRESH_BINARY)

meanFFT = np.fft.fft2(mean)
distFFT = np.fft.fft2(dist)

conj = np.conjugate(meanFFT)
res = distFFT * meanFFT

cv2.imwrite('watermark.png', np.fft.ifft(res).real)

最佳答案

您在论文中提供的引言是“然后将其与水印边缘(水平和垂直翻转)进行卷积”。

图像水平和垂直翻转的卷积就是与该图像的互相关。因此,这里我们正在计算图像中边缘与水印中边缘的距离变换的互相关。互相关最小的偏移是水印边缘与图像边缘最匹配的偏移。

使用Canny可以获取水印边缘,就像获取图像的边缘一样。

要计算互相关,请使用傅立叶域:

  • 确保两个图像(距离变换和水印边缘)的大小相同。用零填充它们以使其大小匹配。
  • 计算两者的FFT。
  • 计算水印图像的FFT的复共轭(这对应于在空间域中垂直和水平翻转图像)。
  • 乘以两个
  • 计算逆变换,并裁剪掉与添加到距离变换图像(如果有)上的填充相对应的区域。
  • 关于python - 使用图像作为卷积核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53036180/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com