Merge remote-tracking branch 'origin/main' into road

This commit is contained in:
2024-06-24 04:47:07 +02:00
6 changed files with 199 additions and 92 deletions

View File

@@ -3,8 +3,8 @@ from PIL import Image, ImageFilter
import numpy as np
from scipy import ndimage
from world_maker.Skeleton import Skeleton
from typing import Union
from random import randint
from world_maker.Position import Position
from random import randint, choice
import cv2
@@ -18,13 +18,13 @@ def get_data(world: World):
return heightmap, watermap, treemap
def handle_import_image(image: Union[str, Image]) -> Image:
def handle_import_image(image: str | Image.Image) -> Image.Image:
if isinstance(image, str):
return Image.open(image)
return image
def filter_negative(image: Union[str, Image]) -> Image:
def filter_negative(image: str | Image.Image) -> Image.Image:
"""
Invert the colors of an image.
@@ -35,7 +35,7 @@ def filter_negative(image: Union[str, Image]) -> Image:
return Image.fromarray(np.invert(np.array(image)))
def filter_sobel(image: Union[str, Image]) -> Image:
def filter_sobel(image: str | Image.Image) -> Image.Image:
"""
Edge detection algorithms from an image.
@@ -68,29 +68,29 @@ def filter_sobel(image: Union[str, Image]) -> Image:
for i in range(1, h - 1):
for j in range(1, w - 1):
horizontalGrad = (
(horizontal[0, 0] * gray_img[i - 1, j - 1])
+ (horizontal[0, 1] * gray_img[i - 1, j])
+ (horizontal[0, 2] * gray_img[i - 1, j + 1])
+ (horizontal[1, 0] * gray_img[i, j - 1])
+ (horizontal[1, 1] * gray_img[i, j])
+ (horizontal[1, 2] * gray_img[i, j + 1])
+ (horizontal[2, 0] * gray_img[i + 1, j - 1])
+ (horizontal[2, 1] * gray_img[i + 1, j])
+ (horizontal[2, 2] * gray_img[i + 1, j + 1])
(horizontal[0, 0] * gray_img[i - 1, j - 1])
+ (horizontal[0, 1] * gray_img[i - 1, j])
+ (horizontal[0, 2] * gray_img[i - 1, j + 1])
+ (horizontal[1, 0] * gray_img[i, j - 1])
+ (horizontal[1, 1] * gray_img[i, j])
+ (horizontal[1, 2] * gray_img[i, j + 1])
+ (horizontal[2, 0] * gray_img[i + 1, j - 1])
+ (horizontal[2, 1] * gray_img[i + 1, j])
+ (horizontal[2, 2] * gray_img[i + 1, j + 1])
)
newhorizontalImage[i - 1, j - 1] = abs(horizontalGrad)
verticalGrad = (
(vertical[0, 0] * gray_img[i - 1, j - 1])
+ (vertical[0, 1] * gray_img[i - 1, j])
+ (vertical[0, 2] * gray_img[i - 1, j + 1])
+ (vertical[1, 0] * gray_img[i, j - 1])
+ (vertical[1, 1] * gray_img[i, j])
+ (vertical[1, 2] * gray_img[i, j + 1])
+ (vertical[2, 0] * gray_img[i + 1, j - 1])
+ (vertical[2, 1] * gray_img[i + 1, j])
+ (vertical[2, 2] * gray_img[i + 1, j + 1])
(vertical[0, 0] * gray_img[i - 1, j - 1])
+ (vertical[0, 1] * gray_img[i - 1, j])
+ (vertical[0, 2] * gray_img[i - 1, j + 1])
+ (vertical[1, 0] * gray_img[i, j - 1])
+ (vertical[1, 1] * gray_img[i, j])
+ (vertical[1, 2] * gray_img[i, j + 1])
+ (vertical[2, 0] * gray_img[i + 1, j - 1])
+ (vertical[2, 1] * gray_img[i + 1, j])
+ (vertical[2, 2] * gray_img[i + 1, j + 1])
)
newverticalImage[i - 1, j - 1] = abs(verticalGrad)
@@ -105,7 +105,7 @@ def filter_sobel(image: Union[str, Image]) -> Image:
return image
def filter_smooth_theshold(image: Union[str, Image], radius: int = 3):
def filter_smooth_theshold(image: str | Image.Image, radius: int = 3):
"""
:param image: white and black image representing the derivative of the terrain (sobel), where black is flat and white is very steep.
:param radius: Radius of the Gaussian blur.
@@ -135,15 +135,14 @@ def filter_smooth_theshold(image: Union[str, Image], radius: int = 3):
return Image.fromarray(bool_array)
def filter_smooth(image: Union[str, Image], radius: int = 3):
def filter_smooth(image: str | Image.Image, radius: int = 3):
image = handle_import_image(image)
image = image.convert('L')
image = image.filter(ImageFilter.GaussianBlur(radius))
return image
def subtract_map(image: Union[str, Image], substractImage: Union[str, Image]) -> Image:
def subtract_map(image: str | Image.Image, substractImage: str | Image.Image) -> Image.Image:
image = handle_import_image(image)
substractImage = handle_import_image(substractImage).convert('L')
@@ -156,7 +155,7 @@ def subtract_map(image: Union[str, Image], substractImage: Union[str, Image]) ->
return Image.fromarray(array_heightmap)
def overide_map(base: Image, top: Image) -> Image:
def overide_map(base: Image, top: Image) -> Image.Image:
base = handle_import_image(base).convert('L')
top = handle_import_image(top).convert('L')
@@ -180,7 +179,7 @@ def overide_map(base: Image, top: Image) -> Image:
return result_image
def group_map(image1: Union[str, Image], image2: Union[str, Image]) -> Image:
def group_map(image1: str | Image.Image, image2: str | Image.Image) -> Image.Image:
image1 = handle_import_image(image1).convert('L')
image2 = handle_import_image(image2).convert('L')
@@ -200,7 +199,7 @@ def filter_smooth_array(array: np.ndarray, radius: int = 3) -> np.ndarray:
return array
def filter_remove_details(image: Union[str, Image], n: int = 20) -> Image:
def filter_remove_details(image: str | Image.Image, n: int = 20) -> Image.Image:
image = handle_import_image(image)
array = np.array(image)
for _ in range(n):
@@ -212,7 +211,7 @@ def filter_remove_details(image: Union[str, Image], n: int = 20) -> Image:
return image
def highway_map() -> Image:
def highway_map() -> Image.Image:
print("[Data Analysis] Generating highway map...")
smooth_sobel = filter_smooth_theshold("./world_maker/data/sobelmap.png", 1)
negative_smooth_sobel = filter_negative(smooth_sobel)
@@ -245,7 +244,7 @@ def create_volume(surface: np.ndarray, heightmap: np.ndarray, make_it_flat: bool
return volume
def convert_2D_to_3D(image: Union[str, Image], make_it_flat: bool = False) -> np.ndarray:
def convert_2D_to_3D(image: str | Image.Image, make_it_flat: bool = False) -> np.ndarray:
image = handle_import_image(image)
heightmap = Image.open(
'./world_maker/data/heightmap_smooth.png').convert('L')
@@ -255,7 +254,7 @@ def convert_2D_to_3D(image: Union[str, Image], make_it_flat: bool = False) -> np
return volume
def skeleton_highway_map(image: Union[str, Image] = './world_maker/data/highwaymap.png') -> Skeleton:
def skeleton_highway_map(image: str | Image.Image = './world_maker/data/highwaymap.png') -> Skeleton:
image_array = convert_2D_to_3D(image, True)
skeleton = Skeleton(image_array)
skeleton.parse_graph(True)
@@ -265,7 +264,7 @@ def skeleton_highway_map(image: Union[str, Image] = './world_maker/data/highwaym
return skeleton
def skeleton_mountain_map(image: Union[str, Image] = './world_maker/data/mountain_map.png') -> Skeleton:
def skeleton_mountain_map(image: str | Image.Image = './world_maker/data/mountain_map.png') -> Skeleton:
image_array = convert_2D_to_3D(image, True)
skeleton = Skeleton(image_array)
skeleton.parse_graph()
@@ -275,7 +274,7 @@ def skeleton_mountain_map(image: Union[str, Image] = './world_maker/data/mountai
return skeleton
def smooth_sobel_water() -> Image:
def smooth_sobel_water() -> Image.Image:
watermap = handle_import_image("./world_maker/data/watermap.png")
watermap = filter_negative(
filter_remove_details(filter_negative(watermap), 5))
@@ -287,32 +286,104 @@ def smooth_sobel_water() -> Image:
return group
def detect_mountain(image: Union[str, Image] = './world_maker/data/sobelmap.png') -> Image:
image = handle_import_image(image)
sobel = np.array(image)
pixels = sobel.reshape((-1, 1))
pixels = np.float32(pixels)
def mountain_map_expend(mountain_map: list[list[int]], starting_point: tuple[int, int], value: int):
explore_points = [starting_point]
while len(explore_points) > 0:
x, y = explore_points.pop(0)
mountain_map[y][x] = value
for i in range(-1, 2):
for j in range(-1, 2):
if (0 <= x + i < len(mountain_map[0]) and 0 <= y + j < len(mountain_map) and
mountain_map[y + j][x + i] == 0):
if (x + i, y + j) not in explore_points:
explore_points.append((x + i, y + j))
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)
k = 3
_, labels, centers = cv2.kmeans(
pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
centers = np.uint8(centers)
segmented_image = centers[labels.flatten()]
segmented_image = segmented_image.reshape(sobel.shape)
mountain = segmented_image == segmented_image.max()
def set_values_of_building_mountain(mountain_map: list[list[int]], area_mountain: list[int],
building_map: str | Image.Image = "./world_maker/data/smooth_sobel_watermap.png"):
building_map = handle_import_image(building_map).convert('L')
for y in range(building_map.size[1]):
for x in range(building_map.size[0]):
if building_map.getpixel((x, y)) > 144:
if mountain_map[y][x] != -1:
area_mountain[mountain_map[y][x] - 1] += 1
contours, _ = cv2.findContours(mountain.astype(
np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
max_contour = max(contours, key=cv2.contourArea)
M = cv2.moments(max_contour)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
def get_index_of_biggest_area_mountain(area_mountain: list[int], exception: list[int]) -> int:
max_value = -1
index = -1
for i in range(len(area_mountain)):
if i not in exception and area_mountain[i] > max_value:
max_value = area_mountain[i]
index = i
return index
print(f"[Data Analysis] The center of the mountain is at ({cX}, {cY})")
return (cX, cY)
def get_random_point_in_area_mountain(mountain_map: list[list[int]], index: int) -> Position | None:
points = []
for y in range(len(mountain_map)):
for x in range(len(mountain_map[0])):
if mountain_map[y][x] == index + 1:
points.append(Position(x, y))
if not points:
return None
return choice(points)
def get_center_of_area_mountain(mountain_map: list[list[int]], index: int) -> Position:
sum_x = 0
sum_y = 0
count = 0
for y in range(len(mountain_map)):
for x in range(len(mountain_map[0])):
if mountain_map[y][x] == index + 1:
sum_x += x
sum_y += y
count += 1
center = Position(sum_x // count, sum_y // count)
if mountain_map[center.y][center.x] != index + 1:
return get_random_point_in_area_mountain(mountain_map, index)
return center
def detect_mountain(number_of_mountain: int = 2, height_threshold: int = 10,
image_heightmap: str | Image.Image = './world_maker/data/heightmap.png') -> list[Position]:
print("[Data Analysis] Detecting mountains...")
image_heightmap = handle_import_image(image_heightmap).convert('L')
avg_height = 0
for y in range(image_heightmap.size[1]):
for x in range(image_heightmap.size[0]):
avg_height += image_heightmap.getpixel((x, y))
avg_height = int(avg_height / (image_heightmap.size[0] * image_heightmap.size[1]))
print("[Data Analysis] Average height:", avg_height)
mountain_map = [[-1 if image_heightmap.getpixel((x, y)) < (avg_height + height_threshold) else 0 for x in
range(image_heightmap.size[0])] for y in
range(image_heightmap.size[1])]
area_mountain = []
for y in range(image_heightmap.size[1]):
for x in range(image_heightmap.size[0]):
if mountain_map[y][x] == 0:
area_mountain.append(0)
mountain_map_expend(mountain_map, (x, y), len(area_mountain))
if not area_mountain:
print("[Data Analysis] No mountain detected.")
return []
set_values_of_building_mountain(mountain_map, area_mountain)
if number_of_mountain < len(area_mountain):
index_mountain = []
for n in range(number_of_mountain):
index_mountain.append(get_index_of_biggest_area_mountain(area_mountain, index_mountain))
else:
index_mountain = [i for i in range(len(area_mountain))]
position_mountain = []
for i in range(len(index_mountain)):
position_mountain.append(get_center_of_area_mountain(mountain_map, index_mountain[i]))
return position_mountain
def rectangle_2D_to_3D(rectangle: list[tuple[tuple[int, int], tuple[int, int]]],
@@ -323,18 +394,21 @@ def rectangle_2D_to_3D(rectangle: list[tuple[tuple[int, int], tuple[int, int]]],
new_rectangle = []
for rect in rectangle:
start, end = rect
avg_height = 0
height = {}
for x in range(start[0], end[0]):
for y in range(start[1], end[1]):
avg_height += image.getpixel((x, y))
avg_height = int(
avg_height / ((end[0] - start[0]) * (end[1] - start[1]))) + 1
if image.getpixel((x, y)) not in height:
height[image.getpixel((x, y))] = 1
else:
height[image.getpixel((x, y))] += 1
max_height = max(height, key=height.get)
new_rectangle.append(
((start[0], avg_height, start[1]), (end[0], avg_height + randint(height_min, height_max), end[1])))
((start[0], max_height, start[1]), (end[0], max_height + randint(height_min, height_max), end[1])))
return new_rectangle
def transpose_form_heightmap(heightmap: Union[str, Image], coordinates, origin: tuple[int, int]) -> tuple[int, int, int]:
def transpose_form_heightmap(heightmap: str | Image.Image, coordinates, origin: tuple[int, int]) -> tuple[
int, int, int]:
heightmap = handle_import_image(heightmap).convert('L')
xMin, zMin = origin