我正在尝试用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),此算法将无法正常运行。图像变得扭曲,我不知道为什么,因为它适用于其他不那么窄的非方形矩阵。如果有人能看到我哪里出错或有任何建议或任何意见,那将非常感激!
提前致谢!
答案 0 :(得分:0)
经过与a good friend的讨论和讨论,我们发现函数loadPBM插入的填充,用于内存对齐,本节:
aux=image->cols & 0x3;
if (aux!=0){
image->widthStep+=4-aux;
}
导致每个矩阵元素的字节写入过度到下一个元素,对于宽度不是4的多个图像,由于改变了widthStep而使它们失真。这解释了为什么算法适用于某些非方形矩阵,但不是全部。 对于那些看过帖子并且也想知道为什么会发生这种情况的人,希望这会有所帮助!你可以删除这个添加的填充,它就像一个魅力