| | import random |
| | import math |
| | import numpy as np |
| | from PIL import Image |
| | from skimage.draw import line |
| | from skimage import morphology |
| | import cv2 |
| |
|
| | def line_crosses_cracks(start, end, img): |
| | rr, cc = line(start[0], start[1], end[0], end[1]) |
| | |
| | if len(rr) > 1 and len(cc) > 1: |
| | return np.any(img[rr[1:], cc[1:]] == 255) |
| | return False |
| |
|
| | def random_walk(img_array, k=8, m=0.1, min_steps=50, max_steps=200, length=2, degree_range=30, seed=None): |
| | |
| | if seed is not None: |
| | random.seed(seed) |
| | np.random.seed(seed) |
| |
|
| | |
| | img_array = cv2.ximgproc.thinning(img_array) |
| |
|
| | rows, cols = img_array.shape |
| | |
| | white_pixels = np.column_stack(np.where(img_array == 255)) |
| | original_crack_count = len(white_pixels) |
| |
|
| | |
| | if white_pixels.size == 0: |
| | raise ValueError("No initial crack pixels found in the image.") |
| | if k > len(white_pixels): |
| | raise ValueError("k is greater than the number of existing crack pixels.") |
| | initial_points = white_pixels[random.sample(range(len(white_pixels)), k)] |
| |
|
| | |
| | step_counts = {i: random.randint(min_steps, max_steps) for i in range(k)} |
| | |
| | main_angles = {i: random.uniform(0, 360) for i in range(k)} |
| |
|
| | grown_crack_count = 0 |
| |
|
| | |
| | for idx, point in enumerate(initial_points): |
| | current_pos = tuple(point) |
| | current_steps = 0 |
| | while current_steps < step_counts[idx]: |
| | |
| | current_ratio = np.sum(img_array == 255) / (rows * cols) |
| | if current_ratio >= m: |
| | return img_array, {'original_crack_count': original_crack_count, 'grown_crack_count': grown_crack_count} |
| |
|
| | |
| | main_angle = main_angles[idx] |
| | angle = math.radians(main_angle + random.uniform(-degree_range, degree_range)) |
| | |
| | |
| | delta_row = length * math.sin(angle) |
| | delta_col = length * math.cos(angle) |
| | next_pos = (int(current_pos[0] + delta_row), int(current_pos[1] + delta_col)) |
| | |
| | |
| | if 0 <= next_pos[0] < rows and 0 <= next_pos[1] < cols and not line_crosses_cracks(current_pos, next_pos, img_array): |
| | |
| | rr, cc = line(current_pos[0], current_pos[1], next_pos[0], next_pos[1]) |
| | img_array[rr, cc] = 255 |
| | grown_crack_count += len(rr) |
| | current_pos = next_pos |
| | current_steps += 1 |
| | else: |
| | |
| | break |
| |
|
| | return img_array, {'original_crack_count': original_crack_count, 'grown_crack_count': grown_crack_count} |
| |
|
| | |
| | |
| |
|
| |
|
| | |
| | if __name__ == "__main__": |
| | |
| | k = 8 |
| | m = 0.1 |
| | min_steps = 50 |
| | max_steps = 200 |
| | img_path = '/data/leiqin/diffusion/huggingface_diffusers/crack_label_creator/random_walk/thindata_256/2.png' |
| | img = Image.open(img_path) |
| | img_array = np.array(img) |
| | length = 2 |
| |
|
| | |
| | result_img_array_mod, pixels_dict = random_walk(img_array.copy(), k, m, min_steps, max_steps, length) |
| |
|
| | |
| | result_img_mod = Image.fromarray(result_img_array_mod.astype('uint8')) |
| |
|
| | |
| | result_img_path_mod = 'resutls.png' |
| | result_img_mod.save(result_img_path_mod) |
| | print(pixels_dict) |
| |
|