2.15 Image Transformation
2.15.1 时空限制
Time limit: 1 Seconds Memory limit: 32768K
2.15.2 题目内容
The image stored on a computer can be represented as a matrix of pixels. In the RGB (Red-Green-Blue) color system, a pixel can be described as a triplex integer numbers. That is, the color of a pixel is in the format “r g b” where r, g and b are integers ranging from 0 to 255 (inclusive) which represent the Red, Green and Blue level of that pixel.
Sometimes however, we may need a gray picture instead of a colorful one. One of the simplest way to transform a RGB picture into gray: for each pixel, we set the Red, Green and Blue level to a same value which is usually the average of the Red, Green and Blue level of that pixel (that is (r + g + b)/3, here we assume that the sum of r, g and b is always dividable by 3).
You decide to write a program to test the effectiveness of this method.
Input
The input contains multiple test cases!
Each test case begins with two integer numbers N and M (1≤N, M≤100) meaning the height and width of the picture, then three N * M matrices follow; respectively represent the Red, Green and Blue level of each pixel.
A line with N = 0 and M = 0 signals the end of the input, which should not be proceed.
Output
For each test case, output “Case #:” first. “#” is the number of the case, which starts from 1. Then output a matrix of N * M integers which describe the gray levels of the pixels in the resultant grayed picture. There should be N lines with M integers separated by a comma.
Sample Input
2 2
1 4
6 9
2 5
7 10
3 6
8 11
2 3
0 1 2
3 4 2
0 1 2
3 4 3
0 1 2
3 4 4
0 0
Sample Output
Case 1:
2,5
7,10
Case 2:
0,1,2
3,4,3
2.15.3 题目来源
Zhejiang Provincial Programming Contest 2007 (Author: ZHOU, Yuan)
2.15.4 汉语翻译
1.题目
图 像 转 换
图像是以像素矩阵的形式存储在计算机里。在红绿蓝三色系统中,一个像素采用三个整数来表示。也就是说,一个像素的颜色以“r g b”的格式表示,这里,r,g 和 b 是 0~255之间(包括 0 和 255)的整数,分别代表该像素红、绿和蓝的程度。
然而有时候,我们需要灰度图像而不是彩色图像。把 RGB 图像转换为灰度图像的一种最简便的方法是:把一个像素的红、绿和蓝的值都设置为一个相同的值(即(r+g+b)/3,这里假定(r+g+b)总能被 3 整除)。
你决定编写一个程序来测试这种方法的有效性。
2.输入描述
输入包含多个测试案例!
每个测试案例以两个整数 N 和 M(1≤N, M≤100)打头,表示图像的高度和宽度,接下来,是三个 N * M 矩阵,分别代表每个像素的红、绿和蓝的值。
在一行上,N =0 和 M =0 表示输入的结束,这行数据不需处理。
3.输出描述
对于每个测试案例,先输出“Case #:”。“#”是测试案例的序号,从 1 开始计数。然后,输出一个 N * M 的矩阵,它描述了最后的灰度图像每个像素的灰度值。应当有 N 行,每行上有 M 个整数,其间用逗号隔开。
4.输入样例
2 2
1 4
6 9
2 5
7 10
3 6
8 11
2 3
0 1 2
3 4 2
0 1 2
3 4 3
0 1 2
3 4 4
0 0
5.输出样例
Case 1:
2,5
7,10
Case 2:
0,1,2
3,4,3
2.15.5 解题思路
本题看似简单,实际上很不容易。简单是指计算方法十分简单,只需使用(r+g+b)/3 即可得出灰度值;而难在理解题意上,输入数据中,先列出了图像中所有像素的红色值,再列出所有像素(N * M 个)的绿色值,最后列出的是所有像素的蓝色值。很多同学都认为每三个数据分别是指一个像素的红、绿和蓝值,这样理解就错了。遇到这样的问题,大家可以事先从输入、输出样例中推断。
2.15.6 参考答案
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
vector<int>r;
vector<int>g;
vector<int>b;
int n,m;
int rr,gg,bb;
int w=0;//样例数量
while(cin>>n>>m)
{
r.clear();
g.clear();
b.clear();
w++;
if(n==0 && m==0)break;
for(int i=0;i<n*m;i++)
{
cin>>rr;
r.push_back(rr);
}
for(int j=0;j<n*m;j++)
{
cin>>gg;
g.push_back(gg);
}
for(int k=0;k<n*m;k++)
{
cin>>bb;
b.push_back(bb);
}
cout<<"Case "<<w<<":"<<endl;
for(int p=0;p<n*m;p++)
{
cout<<(r[p]+g[p]+b[p])/3;
if((p+1)%m==0)cout<<endl;
else cout<<",";
}
}
return 0;
}