|
import numpy as np |
|
import random |
|
from PIL import Image |
|
import matplotlib.pyplot as plt |
|
from multiprocessing import Pool |
|
import time |
|
import os |
|
|
|
def create_data_folders(size): |
|
data_folder = f'data_{size}' |
|
final_folder = os.path.join(data_folder, 'final') |
|
|
|
if not os.path.exists(data_folder): |
|
os.makedirs(data_folder) |
|
print(f"Folder '{data_folder}' created successfully.") |
|
else: |
|
print(f"Folder '{data_folder}' already exists.") |
|
|
|
if not os.path.exists(final_folder): |
|
os.makedirs(final_folder) |
|
print(f"Folder '{final_folder}' created successfully.") |
|
else: |
|
print(f"Folder '{final_folder}' already exists.") |
|
|
|
def generate_image(height, width): |
|
# Calculate centroid |
|
centroid_x = width // 2 |
|
centroid_y = height // 2 |
|
# Calculate side length |
|
side = int(np.sqrt(height * width * 0.25 / 3)) |
|
# Create an empty image |
|
image = np.zeros((height, width), dtype=np.uint8) |
|
# Calculate the position of the square blob |
|
start_x = centroid_x - side // 2 |
|
end_x = start_x + side |
|
start_y = centroid_y - side // 2 |
|
end_y = start_y + side |
|
# Add the square blob in the center |
|
image[start_y:end_y, start_x:end_x] = 255 |
|
# Randomly select two adjacent sides |
|
selected_sides = random.sample(['top', 'right', 'bottom', 'left'], 2) |
|
# Add the adjacent square blobs |
|
for selected_side in selected_sides: |
|
if selected_side == 'top': |
|
adjacent_start_x = centroid_x - side // 2 |
|
adjacent_end_x = centroid_x + side // 2 |
|
adjacent_start_y = centroid_y - side - side // 2 |
|
adjacent_end_y = centroid_y - side // 2 |
|
elif selected_side == 'right': |
|
adjacent_start_x = centroid_x + side // 2 |
|
adjacent_end_x = centroid_x + side + side // 2 |
|
adjacent_start_y = centroid_y - side // 2 |
|
adjacent_end_y = centroid_y + side // 2 |
|
elif selected_side == 'bottom': |
|
adjacent_start_x = centroid_x - side // 2 |
|
adjacent_end_x = centroid_x + side // 2 |
|
adjacent_start_y = centroid_y + side // 2 |
|
adjacent_end_y = centroid_y + side + side // 2 |
|
else: # selected_side == 'left' |
|
adjacent_start_x = centroid_x - side - side // 2 |
|
adjacent_end_x = centroid_x - side // 2 |
|
adjacent_start_y = centroid_y - side // 2 |
|
adjacent_end_y = centroid_y + side // 2 |
|
# Add the adjacent square blob |
|
image[adjacent_start_y:adjacent_end_y, adjacent_start_x:adjacent_end_x] = 255 |
|
# # Create PIL Image object from NumPy array |
|
# microimg = Image.fromarray(image, 'L') |
|
# # Save the image as PNG |
|
# microimg.save('D:/dragonfruit/microimage.png') |
|
return image |
|
|
|
def dye_image(height, width): |
|
# Create an empty binary image |
|
image = np.zeros((height, width), dtype=np.uint8) |
|
|
|
# Pick a random starting point |
|
x = random.randint(0, width - 1) |
|
y = random.randint(0, height - 1) |
|
|
|
# Generate the lines |
|
for _ in range(2*height): |
|
direction = random.choice(['up', 'down', 'left', 'right', 'diagonal_up_right', 'diagonal_up_left', 'diagonal_down_right', 'diagonal_down_left']) |
|
length = random.randint(10, 20) |
|
|
|
if direction == 'up': |
|
y -= length |
|
y = max(y, 0) |
|
image[y:y+length, x] = 255 |
|
elif direction == 'down': |
|
y += length |
|
y = min(y, height - 1) |
|
image[y-length:y, x] = 255 |
|
elif direction == 'left': |
|
x -= length |
|
x = max(x, 0) |
|
image[y, x:x+length] = 255 |
|
elif direction == 'right': |
|
x += length |
|
x = min(x, width - 1) |
|
image[y, x-length:x] = 255 |
|
elif direction == 'diagonal_up_right': |
|
for i in range(length): |
|
y -= 1 |
|
x += 1 |
|
y = max(y, 0) |
|
x = min(x, width - 1) |
|
image[y, x] = 255 |
|
elif direction == 'diagonal_up_left': |
|
for i in range(length): |
|
y -= 1 |
|
x -= 1 |
|
y = max(y, 0) |
|
x = max(x, 0) |
|
image[y, x] = 255 |
|
elif direction == 'diagonal_down_right': |
|
for i in range(length): |
|
y += 1 |
|
x += 1 |
|
y = min(y, height - 1) |
|
x = min(x, width - 1) |
|
image[y, x] = 255 |
|
elif direction == 'diagonal_down_left': |
|
for i in range(length): |
|
y += 1 |
|
x -= 1 |
|
y = min(y, height - 1) |
|
x = max(x, 0) |
|
image[y, x] = 255 |
|
return image |
|
|
|
# Calculate the number of white pixels in the image |
|
def count_white_pixels(image): |
|
return np.sum(image == 255) |
|
|
|
def subtract_images(image1, image2): |
|
result = image1 - image2 |
|
result = np.clip(result, 0, 255) # Clip values to the range [0, 255] |
|
return result.astype(np.uint8) |
|
|
|
def merge_chunks(image_chunks, height, width, chunk_height, chunk_width): |
|
# Function to merge image chunks into a single image |
|
merged_image = np.zeros((height, width), dtype=np.uint8) |
|
row_idx = 0 |
|
col_idx = 0 |
|
for chunk in image_chunks: |
|
merged_image[row_idx:row_idx + chunk_height, col_idx:col_idx + chunk_width] = chunk |
|
col_idx += chunk_width |
|
if col_idx >= width: |
|
col_idx = 0 |
|
row_idx += chunk_height |
|
return merged_image |
|
|
|
def main(height, width, num_samples=3): |
|
create_data_folders(height) |
|
chunk_size = 100 # Chunk size for height and width |
|
num_chunks = height // chunk_size # Number of chunks in each dimension |
|
|
|
for i in range(num_samples): |
|
# Initialize empty list to collect micro and cancer image chunks |
|
micro_chunks = [] |
|
cancer_chunks = [] |
|
|
|
for row in range(num_chunks): |
|
for col in range(num_chunks): |
|
# Generate micro image chunk |
|
micro_chunk = generate_image(chunk_size, chunk_size) |
|
micro_chunks.append(micro_chunk) |
|
|
|
# Generate dye image chunk |
|
dye_chunk = dye_image(chunk_size, chunk_size) |
|
# Subtract dye from micro image chunk to get cancer image chunk |
|
cancer_chunk = subtract_images(micro_chunk, dye_chunk) |
|
cancer_chunks.append(cancer_chunk) |
|
|
|
# Save micro and cancer images |
|
micro_img = Image.fromarray(micro_chunk, 'L') |
|
micro_img.save(f'Data_{height}/micro_image_{i}_{row}_{col}.png') |
|
|
|
cancer_img = Image.fromarray(cancer_chunk, 'L') |
|
cancer_img.save(f'Data_{height}/cancer_image_{i}_{row}_{col}.png') |
|
|
|
|
|
# Merge micro and cancer image chunks |
|
merged_micro_image = merge_chunks(micro_chunks, height, width, chunk_size, chunk_size) |
|
merged_cancer_image = merge_chunks(cancer_chunks, height, width, chunk_size, chunk_size) |
|
|
|
# Count white pixels for micro and cancer images |
|
micro_pixel_count = count_white_pixels(merged_micro_image) |
|
cancer_pixel_count = count_white_pixels(merged_cancer_image) |
|
|
|
# Calculate the percentage of dye present in parasite |
|
if micro_pixel_count != 0: |
|
cancer_threshold = (1 - (cancer_pixel_count / micro_pixel_count)) * 100 |
|
else: |
|
cancer_threshold = float('inf') # Handle division by zero |
|
|
|
print(f"Sample {i+1}:") |
|
print("Micro image white pixel count:", micro_pixel_count) |
|
print("Cancer image white pixel count:", cancer_pixel_count) |
|
print("Dye present in parasite (%):", cancer_threshold) |
|
|
|
if cancer_threshold > 10: |
|
print("Cancer detected") |
|
else: |
|
print("Cancer not detected") |
|
print() |
|
|
|
# Save final merged micro and cancer images |
|
final_merged_micro_img = Image.fromarray(merged_micro_image, 'L') |
|
final_merged_micro_img.save(f'Data_{height}/final/merged_micro_image_{i}.png') |
|
|
|
final_merged_cancer_img = Image.fromarray(merged_cancer_image, 'L') |
|
final_merged_cancer_img.save(f'Data_{height}/final/merged_cancer_image_{i}.png') |
|
|
|
if __name__ == "__main__": |
|
start_time = time.time() # Record the start time |
|
main(1000,1000) # Call the main function |
|
#main(100000,100000) |
|
end_time = time.time() # Record the end time |
|
|
|
# Calculate the elapsed time |
|
elapsed_time = end_time - start_time |
|
print(f"Execution time: {elapsed_time} seconds") |
Uh oh!
There was an error while loading. Please reload this page.