The given code is an implementation of a watermarking algorithm using the Discrete Fourier Transform (DFT). It encodes a watermark image onto an input image and saves the encoded image. It also includes a decoding process to extract the watermark from the encoded image.

Here is a breakdown of the code:

  1. Read the input image (P.jpg) and the watermark image (wm.jpg) using OpenCV's cv2.imread() function.
  2. Create a blank image with half the height of the input image and the same width and number of channels as the input image (hwm).
  3. Copy the watermark image onto the blank image (hwm2) by iterating over each pixel.
  4. Randomly shuffle the rows and columns of the blank image (hwm2) using the random.shuffle() function to ensure robustness.
  5. Copy the shuffled watermark image onto the blank image (hwm) by iterating over each pixel.
  6. Create a reflected watermark image (rwm) by copying the shuffled watermark image onto itself in a mirrored pattern.
  7. Perform the DFT on the input image (img) using np.fft.fft2() to obtain the frequency domain representation (f1).
  8. Add the scaled reflected watermark image (alpha * rwm) to the frequency domain representation of the input image (f1) to obtain the encoded frequency domain representation (f2).
  9. Perform the inverse DFT on the encoded frequency domain representation (f2) using np.fft.ifft2() to obtain the encoded image (_img).
  10. Save the encoded image (_img) using OpenCV's cv2.imwrite() function.
  11. Calculate the difference between the original encoded image (img_wm) and the saved encoded image (img_wm2) to measure the quality of the saved image.
  12. Perform the DFT on the saved encoded image (img_wm2) to obtain the frequency domain representation (f2).
  13. Calculate the watermark by subtracting the frequency domain representation of the input image (f1) from the frequency domain representation of the saved encoded image (f2) and dividing by the scaling factor (alpha).
  14. Convert the watermark to the spatial domain by taking the real part of the watermark.
  15. Create a blank image with the same shape as the reflected watermark image (rwm) to store the extracted watermark (wm).
  16. Copy the extracted watermark onto the blank image (wm) by iterating over each pixel in the top half of the image and its mirrored counterpart in the bottom half.
  17. Save the extracted watermark (wm) for further analysis.

To use this code, you need to provide the filenames of the input image (P.jpg), the watermark image (wm.jpg), and the desired filename for the encoded image (SP.jpg). You can also enable or disable debug mode to visualize intermediate results.

Note: This code assumes that the input and watermark images have the same dimensions.

python 程序原图图片Pjpgwmjpg盲水印的图片SP的数学模型使得图片SPjpg与原图P基本形同。生成图片的算法python 程序。和生成的图片中解析出信息的python 程序要求很好的鲁棒性可以参考下边的程序if cmd == encode print images + watermarks - imageencodeds fn1 fn2 fn3 img = cv2imre

原文地址: https://www.cveoy.top/t/topic/ijHp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录