【发布时间】:2015-02-18 05:54:18
【问题描述】:
我正在尝试用 C++ 编写一个具有以下 3 个功能的程序: (i) read_pgm_image() - 从文件中读取 .pgm 格式的图像; (ii) convolve() - 使用 3X3 低通滤波器对图像执行卷积;和, (iii) write_pgm_image() - 将图像数据写回到单独的文件中
这是我的代码:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void read_pgm_image(char name[]);
void convolve(int img[][256], int M, int N, int img_intensity);
void write_pgm_image(char name[], int img[256][256], int img_data[]);
int main()
{ read_pgm_image("lenna2.pgm");
getchar();
return 0;
}
void read_pgm_image(char name[])
{ string magic, creator_info;
int img_intensity, M, N;
ifstream file(name, ios::binary);
if(file.is_open())
{ cout<<"File successfully opened! \n\n";
cout<<"Reading the file...\n";
cout<<"Name of the file:\t"<<name<<'\n';
getline(file,magic); // get the magic number
getline(file,creator_info); // get the creator information
file>>M; // get width of the image
cout<<"Width of the Image:\t"<<M<<'\n';
file>>N; // get length of the image
cout<<"Length of the Image:\t"<<N<<'\n';
file>>img_intensity; // get the image intensity
cout<<"Maximum Intensity:\t"<<img_intensity<<'\n';
int img[256][256]; // create an array to store image data
for(int i=0; i<N; i++)
{ for(int j=0; j<M; j++)
{ file.read((char*)&img[i][j],sizeof(int));
}
}
file.close();
convolve(img, M, N, img_intensity); // Calling the Convolve function
}
else
{ cout<<"Error in opening image!";
file.close();
}
}
void convolve(int img[256][256], int M, int N, int img_intensity)
{ int con_img[256][256], sum=0, img_data[3]={M,N,img_intensity};
for(int i=0; i<N; i++)
{ for(int j=0; j<M; j++)
{ con_img[i][j]=img[i][j];
}
}
for(int i=1; i<(N-1); i++)
{ for(int j=1; j<(M-1); j++)
{ for(int k=(i-1); k<(i+1); k++)
{ for(int l=(j-1); l<(j+1); l++)
{ sum = sum + img[k][l];
}
}
con_img[i][j] = sum/9;
sum=0;
}
}
write_pgm_image("image_convolve.pgm", con_img, img_data);
}
void write_pgm_image(char name[], int img[256][256], int img_data[3])
{ cout<<"\nCreating image file...\n";
ofstream file(name,ios::binary);
if(file.is_open())
{ cout<<"File successfully created!\n";
cout<<"Writing image data...\n";
file<<"P5\n"<<img_data[0]<<' '<<img_data[1]<<' '<<img_data[2]; //Writing P5, image width, image length and maximum intensity
cout<<"Image data written!\n";
for(int i=0; i<img_data[1]; i++) //Write image data to the file
{ for(int j=0; j<img_data[0]; j++)
{ file.write((char*)&(img[i][j]), sizeof(int));
}
}
cout<<"Image pixel info written!\n";
file.close();
}
else
{ cout <<"Error in creating file!";
file.close();
}
}
convolve() 函数似乎有问题。理想情况下,我应该得到输入图像的模糊版本(我无法上传)。但我得到的是一个垃圾图像。
非常感谢任何帮助... 谢谢。
【问题讨论】:
-
这取决于您所说的“垃圾”是什么意思。除了 elias 指出的小错误外,您的卷积函数看起来不错。输出图像与输入图像完全不同吗?如果不是,则问题可能出在您的文件读取器/写入器中,或两者兼而有之。 PGM 文件规范说像素强度被赋予 1 或 2 个字节的内存,但自
sizeof(int) == 4以来,您一次读取 4 个字节。您应该在读取图像后尝试打印出一些像素值,看看它们是否符合您的预期。 -
如果我在读取原始图像后保存了图像(即在调用 convolve() 函数之前在 read_pgm_image() 函数中调用 write_pgm_image() 函数,我会得到完全相同的图像 - 这是预期的.如果我在执行任何操作之前调用convolve函数中的write函数也是同样的情况。
-
但是,我尝试了一件事。如果我将特定值(例如 0 或 255 或 -1)分配给 2D 数组 con_img[][] 中的元素,则会发生这种情况。如果我分配 0 或正值,我会得到一个黑色像素。如果我分配一个负值,我会得到一个白色像素。这就是我所说的获取“垃圾”图像的意思。
-
在 file.read 和 file.write 行中,我将 sizeof(int) 更改为 (sizeof(int)/4)。这似乎奏效了。对于图像的大部分,我得到了一个模糊的图像。但是,在最后 7 行(从底部算起 7 行)中,我仍然得到“垃圾”。请帮忙!
标签: c++ image convolution