数字水印技术C语言实现
时间: 2024-05-04 15:01:59 浏览: 19
数字水印技术是一种在数字媒体上嵌入信息的技术,用于保护和识别数字媒体的版权和来源。在C语言中,实现数字水印技术需要掌握以下几个方面的知识:
1. 图像处理:C语言中可以使用OpenCV等图像处理库来实现图像处理功能,如读取图像、修改图像像素值等。
2. 数字水印算法:数字水印算法包括频域和空域算法,其中频域算法包括DCT、DWT等,空域算法包括LSB、LSI等。在C语言中可以使用相关算法库来实现数字水印算法。
3. 数据结构和算法:实现数字水印技术需要使用一些数据结构和算法,如哈希表、位运算等。
下面是一个简单的数字水印技术的C语言实现示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <opencv2/opencv.hpp>
using namespace cv;
// 将数字转为二进制串
void int2bin(int num, char *binstr, int len) {
for (int i = len-1; i >= 0; i--) {
binstr[i] = num % 2 + '0';
num /= 2;
}
binstr[len] = '\0';
}
// 将二进制串转为数字
int bin2int(char *binstr, int len) {
int num = 0;
for (int i = 0; i < len; i++) {
num += (binstr[i] - '0') * pow(2, len-i-1);
}
return num;
}
// 在图像的指定位置嵌入数字水印
void embedWatermark(Mat &img, int watermark, int x, int y, int len) {
int rows = img.rows, cols = img.cols;
if (x+len >= cols || y+len >= rows) {
printf("Error: watermark exceeds image boundary!\n");
return;
}
char *binstr = (char*)malloc(sizeof(char)*(len+1));
int2bin(watermark, binstr, len);
for (int i = 0; i < len; i++) {
int r = y+i, c = x+i;
uchar pixel = img.at<uchar>(r, c);
int value = pixel & 0xFE; // 将最低位清零
if (binstr[i] == '1') value |= 0x01; // 将最低位置1
img.at<uchar>(r, c) = value;
}
free(binstr);
}
// 从图像的指定位置提取数字水印
int extractWatermark(Mat &img, int x, int y, int len) {
int rows = img.rows, cols = img.cols;
if (x+len >= cols || y+len >= rows) {
printf("Error: watermark exceeds image boundary!\n");
return -1;
}
char *binstr = (char*)malloc(sizeof(char)*(len+1));
for (int i = 0; i < len; i++) {
int r = y+i, c = x+i;
uchar pixel = img.at<uchar>(r, c);
int value = pixel & 0x01; // 取最低位
binstr[i] = value + '0';
}
binstr[len] = '\0';
int watermark = bin2int(binstr, len);
free(binstr);
return watermark;
}
int main(int argc, char **argv) {
if (argc != 4) {
printf("Usage: watermark <image> <watermark> <output>\n");
return 0;
}
char *imgfile = argv[1];
int watermark = atoi(argv[2]);
char *outfile = argv[3];
Mat img = imread(imgfile, IMREAD_GRAYSCALE);
if (img.empty()) {
printf("Error: failed to read image file %s!\n", imgfile);
return 0;
}
int x = img.cols/2, y = img.rows/2, len = 8;
embedWatermark(img, watermark, x, y, len);
imwrite(outfile, img);
printf("Watermark %d embedded in image %s and saved as %s.\n", watermark, imgfile, outfile);
int extracted = extractWatermark(img, x, y, len);
if (extracted == watermark) {
printf("Watermark %d extracted successfully from image %s.\n", extracted, outfile);
} else {
printf("Error: failed to extract watermark from image %s!\n", outfile);
}
return 0;
}
```
该程序实现了在图像的指定位置嵌入数字水印和从图像的指定位置提取数字水印的功能。数字水印为一个8位的整数,嵌入位置为图像中心,提取位置为嵌入位置。具体实现中,将数字水印转为8位的二进制串,然后将二进制串中的每一位嵌入到图像像素的最低位中。提取数字水印时,只需要从图像对应位置的像素的最低位中取出8位二进制串,然后将其转为数字即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)