灰度直方图:
问题描述
一幅长宽分别为n个像素和m个像素的灰度图像可以表示为一个n×m大小的矩阵A。
其中每个元素 Aij(0≤i<n、0≤j<m)是一个[0,L)范围内的整数,表示对应位置像素的灰度值。
具体来说,一个8比特的灰度图像中每个像素的灰度范围是[0,128)。
一副灰度图像的灰度统计直方图(以下简称“直方图”)可以表示为一个长度为L的数组h,其中h[x](0≤x<L)表示该图像中灰度值为 的像素个数。显然,h[0]到h[L-1]的总和应等于图像中的像素总数n×m。
已知一副图像的灰度矩阵A,试计算其灰度直方图h[0],h[1],…,h[L-1]。
输入格式
输入共n+1行。
输入的第一行包含三个用空格分隔的正整数n、m和L,含义如前文所述。
第二到第n+1行输入矩阵A。第i+2(0≤i<n)行包含用空格分隔的m个整数,依次为Ai0,Ai1,…,Ai(m-1)。
输出格式
输出仅一行,包含用空格分隔的L个整数h[0],h[1],…,h[L-1],表示输入图像的灰度直方图。
样例输入
4 4 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
样例输出
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 • 1
样例输入
7 11 8 0 7 0 0 0 7 0 0 7 7 0 7 0 7 0 7 0 7 0 7 0 7 7 0 0 0 7 0 0 0 7 0 7 7 0 0 0 0 7 0 0 7 7 0 7 0 0 0 0 0 7 0 7 0 0 7 0 7 0 7 0 7 0 7 0 0 0 7 0 0 0 7 0 0 7 0 0
样例输出
48 0 0 0 0 0 0 29 • 1
代码
C++
/* CCF202104-1 灰度直方图 */ #include <bits/stdc++.h> using namespace std; const int L = 256; int cnt[L]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(cnt, 0, sizeof cnt); int n, m, l; cin >> n >> m >> l; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x; cin >> x; cnt[x]++; } for (int i = 0; i < l; i++) cout << cnt[i] << ' '; cout << endl; return 0; }
C
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int main() { int n, m, l; int count[256]; memset(count,0,sizeof(count)); scanf("%d%d%d",&n,&m,&l); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x; scanf("%d",&x); count[x]++; } for (int i = 0; i < l; i++) printf("%d ",count[i]); printf("\n"); return 0; }