我正在尝试在 C 中处理大型 .pgm 图像。首先以 Image 格式读取图像,作为unsigned char元素的矩阵:
struct Matrix{
int rows;
int cols;
unsigned char * data;
int widthStep;
};
typedef struct Matrix Image;
我正在使用以下函数读取图像,使用 netpbm (netpbm/pam.h):
Image * loadPBM(char * fname){
FILE * file;
struct pam inpam;
tuple * tuplerow;
unsigned int row;
Image * image;
int aux;
file=fopen(fname,"r");
pnm_readpaminit(file, &inpam, /*PAM_STRUCT_SIZE(tuple_type)*/
sizeof(struct pam));
printf("Reading image\n");
/* allocating image*/
image=(Image*)malloc(sizeof(Image));
image->cols=inpam.width;
image->rows=inpam.height;
image->widthStep=image->cols;
aux=image->cols & 0x3;
if (aux!=0){
image->widthStep+=4-aux;
}
image->data=(unsigned char *)malloc(image->widthStep*image->rows);
tuplerow = pnm_allocpamrow(&inpam);
for (row = 0; row < inpam.height; row++) {
unsigned int column;
pnm_readpamrow(&inpam, tuplerow);
for (column = 0; column < inpam.width; ++column) {
unsigned int plane;
for (plane = 0; plane < inpam.depth; ++plane) {
image->data[image->widthStep*row+column]= tuplerow[column][plane];
}
}
}
pnm_freepamrow(tuplerow);
fclose(file);
return image;
}
读取后,图像被转换为 ImageF 格式,这样我就可以将元素处理为double,如下所示:
struct MatrixF{
int rows;
int cols;
double * data;
int widthStep;
};
typedef struct MatrixF ImageF;
将图像转换为 ImageF:
for (int i = 0; i < in_img->rows; ++i){
for (int j = 0; j < in_img->cols; ++j){
in_aux->data[i*(in_img->cols)+j] = (double)in_img->data[i*(in_img->cols)+j];
}
}
对于实际的图像处理,我需要对图像进行转置,所以我编写了以下函数:
void transpose(ImageF *in_re, ImageF *out_re){
int rows = in_re->rows;
int cols = in_re->cols;
for(int i = 0 ; i < rows ; ++i){
for(int j = 0 ; j < cols ; ++j){
out_re->data[j*rows+i] = in_re->data[i*cols+j];
}
}
out_re->rows = in_re->cols;
out_re->cols = in_re->rows;
out_re->widthStep = out_re->cols * sizeof(double);
}
转置后,图像从 ImageF 转换回 Image,以存储结果(将 double 转换为 unsigned char):
double val;
for (int i = 0; i < out_aux->rows; i++){
for (int j = 0; j < out_aux->cols; j++){
val = out_aux->data[i*out_aux->cols + j];///((in_img->rows)*(in_img->cols ));
if (val < 0)
val = 0.0;
else if (val > 255)
val = 255.0;
out_img->data[i * out_aux->cols + j] = (unsigned char)val;
}
}
最后使用以下函数存储它们:
void savePBM(char * fname, Image * image){
FILE * file;
struct pam outpam;
tuple * tuplerow;
unsigned int row;
int aux;
file=fopen(fname,"w");
outpam.file=file;
outpam.size=sizeof(struct pam);
outpam.len=sizeof(struct pam);
outpam.format=RPGM_FORMAT;
outpam.plainformat=0;
outpam.height=image->rows;
outpam.width=image->cols;
outpam.depth=1;
outpam.maxval=255;
strcpy(outpam.tuple_type,PAM_PGM_TUPLETYPE);
pnm_writepaminit( &outpam );
printf("Writing image\n");
tuplerow = pnm_allocpamrow(&outpam);
for (row = 0; row < outpam.height; row++) {
unsigned int column;
for (column = 0; column < outpam.width; ++column) {
unsigned int plane;
for (plane = 0; plane < outpam.depth; ++plane) {
tuplerow[column][plane]=image->data[image->widthStep*row+column];
}
}
pnm_writepamrow(&outpam, tuplerow);
}
pnm_freepamrow(tuplerow);
fclose(file);
}
在使用 loadPBM 加载输入图像后,我相信图像内存空间分配是正确完成的,如下所示:
out_img = (Image *)malloc( sizeof(Image) );
out_img->rows = in_img->cols;
out_img->cols = in_img->rows;
out_img->widthStep = out_img->cols * sizeof(unsigned char);
out_img->data = (unsigned char *)malloc( (out_img->rows)*(out_img->cols)*sizeof(unsigned char) );
/*Auxiliary variables*/
in_aux = (ImageF *)malloc(sizeof(ImageF));
in_aux->rows = in_img->rows;
in_aux->cols = in_img->cols;
in_aux->widthStep = in_aux->cols * sizeof(double);
in_aux->data = (double *)malloc( (in_aux->rows)*(in_aux->cols)*sizeof(double) );
out_aux = (ImageF *)malloc(sizeof(ImageF));
out_aux->rows = in_img->rows;
out_aux->cols = in_img->cols;
out_aux->widthStep = out_aux->cols * sizeof(double);
out_aux->data = (double *)malloc( (out_aux->rows)*(out_aux->cols)*sizeof(double) );
出于某种原因,这适用于方形图像甚至分辨率约为 450x700 的图像。但随着图像变窄(例如 170x500),此算法将停止正常运行。图像变得扭曲,我不知道为什么,因为它适用于其他不那么窄的非方阵。如果有人能看到我哪里出错或有一些建议或任何东西,将不胜感激!
提前致谢!