数字影像金字塔三层不调用库函数能够输出三张图片且能够在visual 2019 上运行的c语言代码
时间: 2023-06-18 12:02:51 浏览: 81
以下是一个简单的数字影像金字塔三层的C语言代码示例,可以在Visual Studio 2019上运行,无需调用库函数:
```c
#include <stdio.h>
#include <stdlib.h>
#define WIDTH 256
#define HEIGHT 256
int main()
{
// 读取原始图像
FILE* fp = fopen("lena.raw", "rb");
unsigned char* img = (unsigned char*)malloc(WIDTH * HEIGHT * sizeof(unsigned char));
fread(img, sizeof(unsigned char), WIDTH * HEIGHT, fp);
fclose(fp);
// 申请三层金字塔图像空间
unsigned char* img1 = (unsigned char*)malloc(WIDTH / 2 * HEIGHT / 2 * sizeof(unsigned char));
unsigned char* img2 = (unsigned char*)malloc(WIDTH / 4 * HEIGHT / 4 * sizeof(unsigned char));
unsigned char* img3 = (unsigned char*)malloc(WIDTH / 8 * HEIGHT / 8 * sizeof(unsigned char));
// 第一层金字塔
for (int i = 0; i < HEIGHT / 2; i++) {
for (int j = 0; j < WIDTH / 2; j++) {
img1[i * WIDTH / 2 + j] = img[i * WIDTH * 2 + j * 2];
}
}
// 第二层金字塔
for (int i = 0; i < HEIGHT / 4; i++) {
for (int j = 0; j < WIDTH / 4; j++) {
img2[i * WIDTH / 4 + j] = img1[i * WIDTH + j * 2];
}
}
// 第三层金字塔
for (int i = 0; i < HEIGHT / 8; i++) {
for (int j = 0; j < WIDTH / 8; j++) {
img3[i * WIDTH / 8 + j] = img2[i * WIDTH / 2 + j * 2];
}
}
// 输出三张金字塔图像
fp = fopen("lena_1.raw", "wb");
fwrite(img1, sizeof(unsigned char), WIDTH / 2 * HEIGHT / 2, fp);
fclose(fp);
fp = fopen("lena_2.raw", "wb");
fwrite(img2, sizeof(unsigned char), WIDTH / 4 * HEIGHT / 4, fp);
fclose(fp);
fp = fopen("lena_3.raw", "wb");
fwrite(img3, sizeof(unsigned char), WIDTH / 8 * HEIGHT / 8, fp);
fclose(fp);
free(img);
free(img1);
free(img2);
free(img3);
return 0;
}
```
上述代码中,我们首先读取了一个名为"lena.raw"的256x256的灰度图像,然后分别对其进行了三层下采样,得到了分别为128x128、64x64、32x32的三张金字塔图像,最后将三张图像输出到文件"lena_1.raw"、"lena_2.raw"和"lena_3.raw"中。在实际使用中,我们可以根据需要修改图像的尺寸和文件名等参数。
阅读全文