In order to change the Japanese flag to the Palau flag, we performed simple image processing with numpy.
** Japanese flag-Wikipedia ** The Japanese flag (Nippon no Kokki, Nihon no Kokki) is legally called the Nissho flag, and since ancient times in Japan, it has been generally called the Hinomaru flag.
--Aspect ratio 2: 3 --Circle diameter 3/5 vertical -Position of the circle Center of the national flag
** Flag of Palau-Wikipedia ** The flag of the Republic of Palau (Beluu er a Belau) is a flag with a yellow circle arranged slightly closer to the flagpole in the center and a bright blue background.
--Aspect ratio 5: 8 --The position of the circle is 1/10 off the center of the flagpole
kokki.py
import cv2
import numpy as np
img = cv2.imread("./test/japan.png ").astype(np.float32)
H, W, C = img.shape
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# Gray scale
img = 0.2126 * r + 0.7152 * g + 0.0722 * b
# Binarization
img[img < 100] = 0
H_new = np.ceil((H+W) * 5 / 13).astype(np.int)
W_new = np.ceil((H+W) * 8 / 13).astype(np.int)
_img = np.zeros((H_new, W_new, C), dtype=np.float)
_img[..., 0] = 214
_img[..., 1] = 173
_img[..., 2] = 74
for i in range(H):
for j in range(W):
if img[i, j] == 0:
h = H // 2 - i
w = W // 2 - j
_img[H_new//2 - h, W_new//2 - w - H_new//10, 0] = 0
_img[H_new//2 - h, W_new//2 - w - H_new//10, 1] = 209
_img[H_new//2 - h, W_new//2 - w - H_new//10, 2] = 255
out = _img.astype(np.uint8)
# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("./test/palau.png ", out)
Create an array in which the size of the Japanese flag is converted to the aspect ratio of the Palau flag. After that, add the background color to the empty array. Since the color code is # 4AADD6, use the value obtained by converting this to BGR. Here, a circle is detected from the binarized Japanese flag using raster scan, the coordinates of the Palau flag are obtained from the x and y coordinates, and BGR is inserted. (Color code is # FFD100)
Input image
Output image
I'm lonely in Palau alone, so I'll try it with the Bangladesh flag.
kokki2.py
import cv2
import numpy as np
img = cv2.imread("./test/japan.png ").astype(np.float32)
H, W, C = img.shape
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# Gray scale
img = 0.2126 * r + 0.7152 * g + 0.0722 * b
# Binarization
img[img < 100] = 0
H_new = np.ceil((H+W) * 3 / 8).astype(np.int)
W_new = np.ceil((H+W) * 5 / 8).astype(np.int)
_img = np.zeros((H_new, W_new, C), dtype=np.float)
_img[..., 0] = 81
_img[..., 1] = 102
_img[..., 2] = 0
for i in range(H):
for j in range(W):
if img[i, j] == 0:
h = H // 2 - i
w = W // 2 - j
_img[H_new//2 - h, W_new//2 - w - H_new//10, 0] = 73
_img[H_new//2 - h, W_new//2 - w - H_new//10, 1] = 6
_img[H_new//2 - h, W_new//2 - w - H_new//10, 2] = 233
out = _img.astype(np.uint8)
# Save result
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.imwrite("./test/bangladesh.png ", out)
The aspect ratio for Palau is set to 3: 5 and the color is changed.
Execution result
Palau and Bangladesh are not much different ...
Recommended Posts