C で大きな .pgm 画像を処理しようとしています。画像は、最初に符号なし char要素の行列として Image 形式で読み取られます。
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)、このアルゴリズムは正しく機能しなくなります。画像が歪んでしまいますが、その理由はわかりません。これは、それほど狭くない他の非正方行列でも機能するためです。誰かが私が間違っていた場所を見つけたり、アドバイスや何かを持っている場合は、非常に感謝しています!
前もって感謝します!