请根据8-连接数将renketsu.png
上色。
这里,8-连接数通过以下等式,将各个x_*的值反转0和1计算:
S = (x_1 – x_1\ x_2\ x_3) + (x_3 – x_3\ x_4\ x_5) + (x_5 – x_5\ x_6\ x_7) + (x_7 – x_7\ x_8\ x_1)
python实现:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# connect 8
def connect_8(img):
# get shape
H, W, C = img.shape
# prepare temporary
_tmp = np.zeros((H, W), dtype=np.int)
# get binarize
_tmp[img[..., 0] > 0] = 1
# inverse for connect 8
tmp = 1 - _tmp
# prepare image
out = np.zeros((H, W, 3), dtype=np.uint8)
# each pixel
for y in range(H):
for x in range(W):
if _tmp[y, x] < 1:
continue
S = 0
S += (tmp[y,min(x+1,W-1)] - tmp[y,min(x+1,W-1)] * tmp[max(y-1,0),min(x+1,W-1)] * tmp[max(y-1,0),x])
S += (tmp[max(y-1,0),x] - tmp[max(y-1,0),x] * tmp[max(y-1,0),max(x-1,0)] * tmp[y,max(x-1,0)])
S += (tmp[y,max(x-1,0)] - tmp[y,max(x-1,0)] * tmp[min(y+1,H-1),max(x-1,0)] * tmp[min(y+1,H-1),x])
S += (tmp[min(y+1,H-1),x] - tmp[min(y+1,H-1),x] * tmp[min(y+1,H-1),min(x+1,W-1)] * tmp[y,min(x+1,W-1)])
if S == 0:
out[y,x] = [0, 0, 255]
elif S == 1:
out[y,x] = [0, 255, 0]
elif S == 2:
out[y,x] = [255, 0, 0]
elif S == 3:
out[y,x] = [255, 255, 0]
elif S == 4:
out[y,x] = [255, 0, 255]
out = out.astype(np.uint8)
return out
# Read image
img = cv2.imread("renketsu.png").astype(np.float32)
# connect 8
out = connect_8(img)
# Save result
cv2.imwrite("out.png", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
输入(renketsu.png):
输出: