C 语言图像处理:提取图像中心区域
#include<stdlib.h> #include<stdio.h> #include<string.h>
#define height 256 #define width 256 #define new_height 128 #define new_width 128
int main() { FILE *fpin; FILE *fpout; unsigned char srcimg[height][width]; unsigned char newimg[new_height][new_width]; int j, i;
fpin = fopen("C:/barbara512.raw", "rb");
if (fpin == NULL) {
printf("Failed to open input file.\n");
return 1;
}
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
fread(&srcimg[i][j], 1, 1, fpin);
}
}
fclose(fpin);
// Extracting the center region
int start_row = (height - new_height) / 2;
int start_col = (width - new_width) / 2;
for (i = 0; i < new_height; i++) {
for (j = 0; j < new_width; j++) {
newimg[i][j] = srcimg[start_row + i][start_col + j];
}
}
// Save the new image as a file
fpout = fopen("C:/center_region.raw", "wb+");
if (fpout == NULL) {
printf("Failed to open output file.\n");
return 1;
}
for (i = 0; i < new_height; i++) {
for (j = 0; j < new_width; j++) {
fwrite(&newimg[i][j], 1, 1, fpout);
}
}
fclose(fpout);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pv8D 著作权归作者所有。请勿转载和采集!