CSP202104-1 grayscale histogram (Python)

Article directory

topic

Question No.: 202104-1
Question name: Gray histogram
time limit: 1.0s
Memory limit: 512.0MB

Problem Description
  A grayscale image whose length and width are n pixels and m pixels respectively can be expressed as a matrix A of n × m size.
  Each element A ij (0 ≤ i<n, 0 ≤ j<m) is an integer in the range of [0,L), representing the gray value of the corresponding pixel.
  Specifically, the gray range of each pixel in an 8-bit grayscale image is [0,128).
  The gray statistical histogram of a gray image (hereinafter referred to as "histogram") can be expressed as an array h of length L, where h[x] (0 ≤ x<L) indicates that the gray value in the image is The number of pixels in x. Obviously, the sum of h[0] to h[L-1] should be equal to the total number n·m of pixels in the image.
  Knowing the grayscale matrix A of an image, try to calculate its grayscale histogram h[0],h[1],...,h[L-1],.

Input format
  Input a total of n+1 lines.
  The first line of input contains three positive integers n, m, and L separated by spaces, with the meanings described above.
  The second to n+1th rows are input to matrix A .
  Line i+2 (0 ≤ i<n) contains m integers separated by spaces, which are A i0 , A i1 ,···,A i(m-1) in turn .

Output format
  The output is only one line, containing L integers separated by spaces, representing the grayscale histogram of the input image.

Sample input
  4 4 16
  0 1 2 3
  4 5 6 7
  8 9 10
  11 12 13 14 15

Sample output
  1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample input
  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

Sample output
  48 0 0 0 0 0 0 29

Evaluation case scale and agreement
  All test data satisfy 0<n, m ≤ 500 and 4 ≤ L ≤ 256.

code

# 输入矩阵大小n,m和灰度范围L
n,m,L = list(map(int,input().split()))
# 初始化灰度统计列表
statistics = [0 for i in range(L)]
# 循环n次
for i in range(n):
    # 按行输入数据
    grayscales = list(map(int,input().split()))
    # 遍历每行灰度元素
    for grayscale in grayscales:
        # 灰度值对应位置统计数字自增
        statistics[grayscale] += 1
# 输出
for i in range(L):
    print(statistics[i],end=' ')

Guess you like

Origin blog.csdn.net/qq_45899597/article/details/117869493