File size: 5,114 Bytes
c204f33 |
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import numpy as np
#2024-12-03 rotate_point_euler
#2024-12-04 load_data
def load_data(filepath):
"""
カンマ区切りのテキストファイルからデータをNumPy配列に読み込みます。
Args:
filepath: データファイルのパス
Returns:
NumPy配列: 読み込まれたデータ。エラーが発生した場合はNone。
"""
try:
data = np.loadtxt(filepath, delimiter=",")
return data
except (FileNotFoundError, ValueError) as e:
print(f"Error loading data: {e}")
return None
def rotate_point_euler(point, angles,order="xyz",is_degree=False):
"""
オイラー角を使って3Dポイントを回転させる関数
Args:
point: 回転させる3Dポイント (x, y, z)
angles: 各軸周りの回転角度 (rx, ry, rz) [ラジアン]
Returns:
回転後の3Dポイント (x', y', z')
"""
if is_degree:
angles = [np.deg2rad(value) for value in angles]
rx, ry, rz = angles
point = np.array(point)
# X軸周りの回転
Rx = np.array([
[1, 0, 0],
[0, np.cos(rx), -np.sin(rx)],
[0, np.sin(rx), np.cos(rx)]
])
# Y軸周りの回転
Ry = np.array([
[np.cos(ry), 0, np.sin(ry)],
[0, 1, 0],
[-np.sin(ry), 0, np.cos(ry)]
])
# Z軸周りの回転
Rz = np.array([
[np.cos(rz), -np.sin(rz), 0],
[np.sin(rz), np.cos(rz), 0],
[0, 0, 1]
])
# 回転行列の合成 (Z軸 -> Y軸 -> X軸 の順で回転)
order = order.lower()
if order == "xyz":
R = Rx @ Ry @ Rz
elif order == "xzy":
R = Rx @ Rz @ Ry
elif order == "yxz":
R = Ry @ Rx @ Rz
elif order == "yzx":
R = Ry @ Rz @ Rx
elif order == "zxy":
R = Rz @ Rx @ Ry
else:#zyx
R = Rz @ Ry @ Rx
# 回転後のポイントを計算
rotated_point = R @ point
return rotated_point
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 |