Spaces:
Running
Running
import numpy as np | |
def apply_binary_mask_to_color(base_image,color,mask): | |
""" | |
二値マスクを使用して、画像の一部を別の画像にコピーする。 | |
Args: | |
base_image (np.ndarray): コピー先の画像。 | |
paste_image (np.ndarray): コピー元の画像。 | |
mask (np.ndarray): 二値マスク画像。 | |
Returns: | |
np.ndarray: マスクを適用した画像。 | |
""" | |
# TODO check all shape | |
#print_numpy(base_image) | |
#print_numpy(paste_image) | |
#print_numpy(mask) | |
if mask.ndim == 2: | |
condition = mask == 255 | |
else: | |
condition = mask[:,:,0] == 255 | |
base_image[condition] = color | |
return base_image | |
def apply_binary_mask_to_image(base_image,paste_image,mask): | |
""" | |
二値マスクを使用して、画像の一部を別の画像にコピーする。 | |
Args: | |
base_image (np.ndarray): コピー先の画像。 | |
paste_image (np.ndarray): コピー元の画像。 | |
mask (np.ndarray): 二値マスク画像。 | |
Returns: | |
np.ndarray: マスクを適用した画像。 | |
""" | |
# TODO check all shape | |
#print_numpy(base_image) | |
#print_numpy(paste_image) | |
#print_numpy(mask) | |
if mask.ndim == 2: | |
condition = mask == 255 | |
else: | |
condition = mask[:,:,0] == 255 | |
base_image[condition] = paste_image[condition] | |
return base_image | |
def pil_to_numpy(image): | |
return np.array(image, dtype=np.uint8) | |
def extruce_points(points,index,ratio=1.5): | |
""" | |
indexのポイントをratio倍だけ、点群の中心から、外側に膨らます。 | |
""" | |
center_point = np.mean(points, axis=0) | |
if index < 0 or index > len(points): | |
raise ValueError(f"index must be range(0,{len(points)} but value = {index})") | |
point1 =points[index] | |
print(f"center = {center_point}") | |
vec_to_center = point1 - center_point | |
return vec_to_center*ratio + center_point | |
def bulge_polygon(points, bulge_factor=0.1,isClosed=True): | |
""" | |
ポリゴンの辺の中間に点を追加し、外側に膨らませる | |
ndarrayを返すので注意 | |
""" | |
# 入力 points を NumPy 配列に変換 | |
points = np.array(points) | |
# ポリゴン全体の重心を求める | |
center_point = np.mean(points, axis=0) | |
#print(f"center = {center_point}") | |
new_points = [] | |
num_points = len(points) | |
for i in range(num_points): | |
if i == num_points -1 and not isClosed: | |
break | |
p1 = points[i] | |
#print(f"p{i} = {p1}") | |
# 重心から頂点へのベクトル | |
#vec_to_center = p1 - center_point | |
# 辺のベクトルを求める | |
mid_diff = points[(i + 1) % num_points] - p1 | |
mid = p1+(mid_diff/2) | |
#print(f"mid = {mid}") | |
out_vec = mid - center_point | |
# 重心からのベクトルに bulge_vec を加算 | |
new_point = mid + out_vec * bulge_factor | |
new_points.append(p1) | |
new_points.append(new_point.astype(np.int32)) | |
return np.array(new_points) | |
# image.shape rgb are (1024,1024,3) use 1024,1024 as 2-dimensional | |
def create_2d_image(shape): | |
grayscale_image = np.zeros(shape[:2], dtype=np.uint8) | |
return grayscale_image |