- Overview
- Project Structure
- How to Run the Project
- Functions Overview
- Image Transformations
- Conclusion
- License
- Contact
This project demonstrates the use of invisible digital watermarking for image protection. The goal is to embed a watermark into an image such that it remains undetectable to the human eye but can be extracted after any image manipulation or tampering. In this project, we use the Discrete Cosine Transform (DCT) for embedding and extracting the watermark, making the process robust against typical image transformations. This is a course project for UESTC4035: Digital Image Processing (2024-2025).
modified_images/ - A folder to store images altered with various transformations (e.g., noise, resizing).
regenerated_watermarks/ - A folder to store watermarks extracted from the modified images.
DCT-Watermark.ipynb - The main script for the project, implementing the DCT-based watermarking algorithm.
input_image.jpg - The input image that will have the watermark applied.
custom_watermark.jpg - The custom image that will serve as the watermark.
watermarked_image.jpg - The resulting image with the watermark applied.
README.md - This file, providing details and instructions for the project.
git clone https://github.com/yourusername/invisible-digital-watermarking.git
cd invisible-digital-watermarking
pip install numpy scipy pillow opencv-python matplotlib
DCT-Watermark.ipynb
Applies the Arnold transformation to scramble the watermark image for additional security. The Arnold transformation shuffles the pixel positions of the watermark image based on a mathematical formula. This transformation is reversible, meaning the original watermark can be restored by applying the inverse transformation with the same number of iterations.
img
: The watermark image to be scrambled (as a NumPy array or PIL Image).iterations
: The number of iterations to apply the transformation. More iterations result in a more scrambled image.
- Shuffles the pixel positions using the formula:
- New row index:
(i + j) % n
- New column index:
(i + 2 * j) % m
- New row index:
- Each iteration further scrambles the image.
# Scramble a watermark image with 10 iterations
scrambled_image = arnold_transform(watermark_image, iterations=10)
Perform the Discrete Cosine Transform (DCT) and its inverse (IDCT) on image blocks. These functions allow operations in the frequency domain, which is essential for embedding and extracting the watermark.
image
: A 2D NumPy array representing an image or a block of an image.
dct2(image)
:- Converts the spatial domain of the image to the frequency domain.
- Helps isolate the low- and high-frequency components of the image.
idct2(image)
:- Reconstructs the original image block from its frequency components.
- Used to revert the modified DCT coefficients back into the spatial domain.
# Compute the DCT and inverse DCT of an image block
dct_block = dct2(image_block)
restored_block = idct2(dct_block)
Embeds the watermark into the image using DCT. This function modifies the frequency domain coefficients of the image to incorporate the watermark, ensuring it remains invisible to the human eye.
image
: The cover image into which the watermark will be embedded.watermark
: The watermark image to be embedded.A
: A scaling factor for adjusting the strength of the watermark in the frequency domain.watermark_scale
: The scale factor for resizing the watermark to fit the cover image.
- Converts both the cover image and the watermark to grayscale.
- Resizes the watermark according to the
watermark_scale
parameter. - Applies the Arnold transformation to scramble the watermark.
- Divides the cover image into 8×8 blocks and computes the DCT for each block.
- Modifies the DCT coefficients of each block based on the scrambled watermark.
- Performs the IDCT to reconstruct the watermarked image.
# Embed a watermark into the cover image
watermarked_image = embed_watermark(cover_image, watermark, A=800, watermark_scale=2)
Extracts the embedded watermark from the watermarked image. This function compares the DCT coefficients of the watermarked and original images to reconstruct the watermark.
watermarked_image
: The image containing the embedded watermark.original_image
: The original image without the watermark.
- Divides both the watermarked and original images into 8×8 blocks.
- Computes the DCT of each block from both images.
- Extracts watermark bits by analyzing the difference between specific DCT coefficients.
- Reconstructs the binary watermark image.
# Extract the watermark from the watermarked image
extracted_watermark = extract_watermark(watermarked_image, original_image)
Evaluates the accuracy of the extracted watermark using template matching. This function compares the extracted watermark against the original and inverted watermark templates to determine the degree of similarity.
image_index
: The index of the image being evaluated (used for file handling).template
: The original watermark template (as a NumPy array).inverted_template
: The inverted version of the watermark template.
- Loads the extracted watermark image and compares it to the
template
andinverted_template
using OpenCV’smatchTemplate
function. - Computes a similarity score (normalized correlation coefficient) for both templates.
- Selects the highest score as the best match and evaluates the accuracy.
# Evaluate the accuracy of the extracted watermark for image 1
template, inverted_template = imageVSwatermark_accuracy(1, template, inverted_template)
Function | Purpose |
---|---|
arnold_transform |
Scrambles the watermark image to add security. |
dct2 and idct2 |
Perform DCT and IDCT to work in the frequency domain. |
embed_watermark |
Embeds the watermark into the cover image using DCT. |
extract_watermark |
Extracts the watermark from the watermarked image. |
imageVSwatermark_accuracy |
Evaluates the similarity between extracted and original watermarks using template matching. |
The project includes several transformations applied to the watermarked image to test the robustness of the watermark:
- Histogram Equalization
- Gaussian Blur
- Mean Filtering
- Salt and Pepper Noise
- Bilateral Filtering
- Rotation
- Sharpening
- K-means Clustering Compression
- Wave Distortion
Each transformation is applied, and the results are saved in the modified_images
directory for further analysis.
This project demonstrates the process of embedding an invisible digital watermark into an image using Discrete Cosine Transform (DCT). The watermark remains undetectable to the human eye but can be extracted after various image transformations. This technique provides a robust solution for protecting the copyright of digital images and ensuring the ownership of the content.
This project is licensed under the MIT License. You are free to use, modify, and distribute this software under the terms of the license. For more details, see the LICENSE file included in this repository.
If you have any questions, suggestions, or feedback regarding this project, feel free to reach out:
- Name: Xu Y.
- Email: [email protected]
- GitHub: XU1hE's GitHub Profile
We appreciate your interest in this project and welcome collaboration opportunities.