6.1 图像处理
题目说明:
- 目标:实现简单的图像处理操作,包括反转和模糊
- 输入:一个表示灰度图像的二维数组(0-255)
- 输出:处理后的图像
const int HEIGHT = 8;
const int WIDTH = 8;
void displayImage(int image[HEIGHT][WIDTH]) {
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
if(image[i][j] > 200) cout << "@ ";
else if(image[i][j] > 150) cout << "# ";
else if(image[i][j] > 100) cout << "o ";
else if(image[i][j] > 50) cout << ". ";
else cout << " ";
}
cout << endl;
}
}
void invertImage(int image[HEIGHT][WIDTH], int result[HEIGHT][WIDTH]) {
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
result[i][j] = 255 - image[i][j];
}
}
}
void blurImage(int image[HEIGHT][WIDTH], int result[HEIGHT][WIDTH]) {
for(int i = 1; i < HEIGHT-1; i++) {
for(int j = 1; j < WIDTH-1; j++) {
int sum = 0;
for(int di = -1; di <= 1; di++) {
for(int dj = -1; dj <= 1; dj++) {
sum += image[i+di][j+dj];
}
}
result[i][j] = sum / 9;
}
}
for(int i = 0; i < HEIGHT; i++) {
result[i][0] = image[i][0];
result[i][WIDTH-1] = image[i][WIDTH-1];
}
for(int j = 0; j < WIDTH; j++) {
result[0][j] = image[0][j];
result[HEIGHT-1][j] = image[HEIGHT-1][j];
}
}
int main() {
int image[HEIGHT][WIDTH] = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 100, 100, 100, 100, 100, 100, 0},
{0, 100, 150, 150, 150, 150, 100, 0},
{0, 100, 150, 200, 200, 150, 100, 0},
{0, 100, 150, 200, 200, 150, 100, 0},
{0, 100, 150, 150, 150, 150, 100, 0},
{0, 100, 100, 100, 100, 100, 100, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
};
int result[HEIGHT][WIDTH];
cout << "原始图像:" << endl;
displayImage(image);
invertImage(image, result);
cout << "\n反转后的图像:" << endl;
displayImage(result);
blurImage(image, result);
cout << "\n模糊处理后的图像:" << endl;
displayImage(result);
return 0;
}