使用C语言完成图像处理
图片格式为raw,所以比较麻烦
这是图片的连接:https://pan.baidu.com/s/1P1g2HXQBQNNRXL5KXM-Fpw
1. Arithmetic operations (10 points)
Write a program for subtraction of lena_edit.raw from lena.raw. Export lena_subtraction.raw as an
output file.
2. Point processing (10 points)
Write a program for generating a negative version of lena.raw. Export lena_negative.raw as an output
file.
3. Geometric transformations (15 points)
Write a program for translating lena.raw by 5 pixels for both x- and y- direction. Export
lena_translation.raw as an output file.
4. Histogram processing (20 points)
Write a program for histogram equalization by taking lena.raw as an input. Export lena_histequal.raw as
an output file.
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void Read2DUCharImg(char *filename, unsigned char **img, int dim_x, int dim_y) {
FILE *fp;
int i;
fp = fopen(filename, "rb");
for (i = 0; i < dim_y; i++)
fread(img[i], sizeof(unsigned char), dim_x, fp);
fclose(fp);
}
void Write2DUCharImg(char *filename, unsigned char **img, int dim_x, int dim_y)
{
int i, k;
FILE *fp;
fp = fopen(filename, "wb");
for (i = 0; i < dim_y; i++)
fwrite(img[i], sizeof(unsigned char), dim_x, fp);
fclose(fp);
}
unsigned char **UCalloc2d(int i_size, int j_size)
{
unsigned char **array;
int i;
array = (unsigned char **)calloc(i_size, sizeof(unsigned char *));
for (i = 0; i < i_size; i++)
array[i] = (unsigned char *)calloc(j_size, sizeof(unsigned char));
return(array);
}
void UCfree2d(unsigned char **array, int i_size)
{
int i;
for (i = 0; i < i_size; i++)
free(array[i]);
free(array);
}
float **Falloc2d(int i_size, int j_size)
{
float **array;
int i;
array = (float **)calloc(i_size, sizeof(float *));
for (i = 0; i < i_size; i++)
array[i] = (float *)calloc(j_size, sizeof(float));
return(array);
}
void Ffree2d(float **array, int i_size)
{
int i;
for (i = 0; i < i_size; i++)
free(array[i]);
free(array);
}
int main(int argc, char *argv[]) {
int function_num;
unsigned char **inputimg, **outputimg, **tempimg;
char inputfilename[100], outputfilename[100];
FILE *fp;
int dim_x, dim_y;
int i, j;
int num_intensity_level = 256;
int max_intensity = num_intensity_level - 1;
int* histogram_bins;
float* histogram;
float* cumul_hist;
int* trans_func;
float **gauss; // gaussian kernel
float **laplacian; // laplacian kernel
int smooth_kernel_size = 5;
int laplacian_kernel_size = 3;
float sigma = 2.0;
float K = 25.0 / 15.0;
dim_x = 512; dim_y = 512;
inputimg = UCalloc2d(dim_y, dim_x);
outputimg = UCalloc2d(dim_y, dim_x);
tempimg = UCalloc2d(dim_y, dim_x);
strcpy(inputfilename, "lena.raw");
strcpy(outputfilename, "lena_output.raw");
histogram_bins = (int*)calloc(num_intensity_level, sizeof(int));
histogram = (float*)calloc(num_intensity_level, sizeof(float));
cumul_hist = (float*)calloc(num_intensity_level, sizeof(float));
trans_func = (int*)calloc(num_intensity_level, sizeof(int));
gauss = Falloc2d(smooth_kernel_size, smooth_kernel_size);
laplacian = Falloc2d(laplacian_kernel_size, laplacian_kernel_size);
// histogram
for (i = 0; i < num_intensity_level; i++) {
histogram_bins[i] = 0;
cumul_hist[i] = 0.0;
trans_func[i] = 0.0;
// Add histogram computation and processing
}
// Gaussian kernel
// To do: Set Gaussian kernel values
// Laplacian kernel
// To do: Set Laplacian kernel values
// read input image
Read2DUCharImg(inputfilename, inputimg, dim_x, dim_y);
// Choose function
function_num = 1;
switch (function_num)
{
case 1: for (i = 0; i < dim_x; i++) {
for (j = 0; j < dim_y; j++) {
outputimg[i][j] = inputimg[i][j] - tempimg[i][j];
}
}
break;
case 2:
// Call function 2
break;
case 3:
// Call function 3
break;
default:
break;
}
Write2DUCharImg(outputfilename, outputimg, dim_x, dim_y);
UCfree2d(inputimg, dim_y);
UCfree2d(outputimg, dim_y);
UCfree2d(tempimg, dim_y);
free(histogram_bins);
free(histogram);
free(cumul_hist);
free(trans_func);
Ffree2d(gauss, smooth_kernel_size);
Ffree2d(laplacian, laplacian_kernel_size);
return EXIT_SUCCESS;
}