東京大学 情報理工学系研究科 創造情報学専攻 2018年8月実施 プログラミング
Author
Description
A text file contains integers from \(0\) to \(255\) which are separated by a single white-space character. Assume that the number of these integers is a multiple of three. Group every three numbers into a triplet. Each triplet represents the intensities of red, green, and blue of a pixel. For example,
are grouped into four triplets (or pixels), which are \((19, 7, 0)\), \((17, 13, 1)\), \((29, 3, 27)\), and \((5, 11, 23)\). Every pixel has an index. The index of the pixel is i if it is the i-th triplet (\(i \geq 0\)) in the file. In the example above, \((19, 7, 0)\) is the 0th triplet.
We can construct an image by placing these integer triplets, or pixels, from left to right, and when reaching the width of the image, placing them in the next line below, and so forth. Assume that the image is rectangular. Answer the following questions by writing a program if necessary.
(1) Write on the answer sheet the number of the pixels stored in the file image1.txt
.
(2) We construct an image from the pixels stored in the file image1.txt
.
All the rightmost pixels of the image are white, that is, the triplet \((255, 255, 255)\).
The image includes no other white vertical line from the top to the bottom than the rightmost one. Write the width of the image on the answer sheet.
(3) Write a program that prints the \(\frac{n}{2}\)-th triplet (or pixel) and its index when sorting the pixels stored in the file image1.txt
in the ascending order of intensities.
Write down the printed triplet and index on the answer sheet, too.
Here, \(n\) is the number of the pixels and it is an even number.
The first pixel is the zeroth triplet. The intensity of a pixel is \(r^2 + g^2 + b^2\) when the triplet \((r, g, b)\) denotes the pixel.
If there are two pixels with the same intensity, the pixel with a larger index has a lower intensity.
(4) Write a program that selects \(k\) pixels \(e_i\), where \(0 \leq i < k\), from the pixels stored in the file image2.txt
.
The program prints the triplets and the indices denoting these \(k\) pixels.
Here, \(e_i\) is the \(\frac{ni}{k}\)-th pixel when the pixels are sorted in the order mentioned in (3).
\(n\) is the number of pixels and \(n\) is a multiple of \(k\).
Write on the answer sheet all the triplets of the pixels and their indices selected when \(k = 4\).
(5) Write a program that selects \(k\) colors representing all the pixels stored in the given file. \(k\) is an input. The \(k\) representing colors are selected as follows:
-
- Select \(k\) pixels \(e_i\) as mentioned in (4). Let them initial representative pixels \(p^{(0)}_i = e_i\).
-
- Categorize all the pixels into \(k\) clusters. For each pixel \(q_j\), find the nearest representative pixel \(p^{(t)}_i\). Then the pixel \(q_j\) belongs to a cluster \(C^{(t)}_i\), where \(t \geq 0\). The representative pixel \(p^{(t)}_i\) belongs to the cluster \(C^{(t)}_i\).
-
- Compute the centroid of each cluster \(C^{(t)}_i\). In \(C^{(t)}_i\), the nearest pixel to that centroid is a new representative pixel \(p^{(t+1)}_i\). Here, the centroid of pixels is a triplet of the averages (use the floor function after division) of each element \((r, g, b)\) of the pixels.
-
- Categorize pixels again into \(k\) clusters \(C^{(t+1)}_i\) by using the new representative pixels \(p^{(t+1)}_i\).
-
- Repeat this ten times and obtain \(k\) representative pixels \(p^{(10)}_i\). The colors we want to obtain are the triplets of these representative pixels.
The distance between two triplets \((r_i, g_i, b_i)\) and \((r_j, g_j, b_j)\) is \(|r_i - r_j| + |g_i - g_j| + |b_i - b_j|\). For 2 and 3, if multiple pixels have the same distance, select the pixel with the largest index.
Then, compute the representative pixels \(p^{(10)}_i\) of the pixels stored in the file image2.txt
when \(k = 128\).
Write on the answer sheet the triplets of \(p^{(10)}_i\) where \(i = 40, 80, 120\).
Do the same thing for image3.txt
when \(k = 8\), \(i = 2, 4, 6\).
(6) Write a program that reduces the number of colors used in the given image in the method mentioned in (5).
The program reads the image from a file and stores the resulting image into a file image.tif
in the format shown below.
Assume that the shape of the image is square.
After the reduction, the pixels in a cluster \(C^{(10)}_i\) are set to the color of its representative pixel \(p^{(10)}_i\).
Then, assuming \(k = 32\), reduce the number of colors for the image in the file image2.txt
and save the obtained image.tif
into the USB flash drive.
The format of the file image.tif
is as follows. It consists of 104-byte attribute information and pixel data. Each byte of the first 104 bytes in the file is the following number (in decimal notation), respectively.
Here, w0 w1 w2 w3, h0 h1 h2 h3, and s0 s1 s2 s3 denote the 4 byte big-endian values representing the width, the height, and (width) x (height) x 3.
After these 104 bytes, the pixels of the image are stored from the top to the bottom line. For each line, the pixels are stored from left to right. For each pixel, each element of the triplet \((r, g, b)\) is stored in this order as a 1-byte value. For example, when the width is \(100\) pixels and the height is \(50\) pixels, then \(15104\) bytes of data in total are stored.
Kai
Please click here for the sample data files.
(1)
(2)
(3)
(4)
(5)
(6)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|