This is used a lot of model to solve gastic cancer problem
|
|
@ -16,7 +16,6 @@ class Image_generator():
|
|||
|
||||
def Processing_Main(self, DataLoader):
|
||||
data_size = 0
|
||||
self.stop = 12
|
||||
File = Process_File()
|
||||
torch.manual_seed(Training_Config["Random_Seed"])
|
||||
|
||||
|
|
@ -28,7 +27,7 @@ class Image_generator():
|
|||
產生出資料強化後的影像
|
||||
'''
|
||||
# data_size = self.Generator_main(DataLoader, 5, data_size) # 執行
|
||||
|
||||
self.stop = len(DataLoader) * 3
|
||||
for i in range(1, 6, 1):
|
||||
print(f"\nAugmentation {i} Generator image")
|
||||
data_size = self.Generator_main(DataLoader, i, data_size) # 執行
|
||||
|
|
@ -119,7 +118,7 @@ class Image_generator():
|
|||
if judge == 1:
|
||||
return v2.Compose([
|
||||
v2.RandomRotation(30),
|
||||
# v2.RandomResizedCrop(224, scale=(0.8, 1.0)),
|
||||
v2.RandomResizedCrop(224, scale=(0.8, 1.0)),
|
||||
v2.RandomHorizontalFlip(),
|
||||
v2.GaussianBlur(7,3),
|
||||
v2.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
|
||||
|
|
@ -127,14 +126,14 @@ class Image_generator():
|
|||
elif judge == 2:
|
||||
return v2.Compose([
|
||||
v2.RandomRotation(180),
|
||||
# v2.RandomResizedCrop(224, scale=(0.7, 1.0)),
|
||||
v2.RandomResizedCrop(224, scale=(0.7, 1.0)),
|
||||
v2.RandomHorizontalFlip(),
|
||||
v2.RandomVerticalFlip(),
|
||||
])
|
||||
elif judge == 3:
|
||||
return v2.Compose([
|
||||
v2.RandomRotation(45, expand = True),
|
||||
# v2.RandomResizedCrop(224, scale=(0.9, 1.0)),
|
||||
v2.RandomResizedCrop(224, scale=(0.9, 1.0)),
|
||||
v2.RandomAffine(degrees=20, shear=0.2),
|
||||
v2.ColorJitter(brightness=0.2, contrast=0.2),
|
||||
v2.RandomHorizontalFlip(),
|
||||
|
|
@ -143,7 +142,7 @@ class Image_generator():
|
|||
elif judge == 4:
|
||||
return v2.Compose([
|
||||
v2.RandomRotation(50),
|
||||
# v2.RandomResizedCrop(224, scale=(0.75, 1.0)),
|
||||
v2.RandomResizedCrop(224, scale=(0.75, 1.0)),
|
||||
v2.RandomAffine(degrees=30, shear=0.25),
|
||||
v2.GaussianBlur(7,3),
|
||||
v2.ColorJitter(brightness=0.3, contrast=0.3, saturation=0.3, hue=0.2),
|
||||
|
|
|
|||
171
Image_Process/preprocess_training_data.py
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
import sys
|
||||
from glob import glob
|
||||
from tqdm import tqdm
|
||||
|
||||
# Add parent directory to path to import utils
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
try:
|
||||
from utils.Stomach_Config import Loading_Config, Training_Config
|
||||
except ImportError:
|
||||
print("Warning: Could not import Loading_Config. Using default labels.")
|
||||
Loading_Config = {
|
||||
"Training_Labels": ["stomach_cancer_Crop", "Normal_Crop", "Have_Question_Crop"]
|
||||
}
|
||||
Training_Config = {
|
||||
"Image_Size": 224
|
||||
}
|
||||
|
||||
def preprocess_image(image_path, save_path,
|
||||
target_size=(224, 224),
|
||||
blur_ksize=(5, 5),
|
||||
adaptive_block_size= 13,
|
||||
adaptive_C=4,
|
||||
gradient_ksize=3,
|
||||
closing_ksize=5,
|
||||
min_area=100):
|
||||
"""
|
||||
Process image using the modified pipeline:
|
||||
1. Resize (縮放)
|
||||
2. Grayscale (灰階)
|
||||
3. GaussianBlur (3-7)
|
||||
4. Adaptive Threshold (blockSize 11-51 odd, C 2-10, THRESH_BINARY or INV)
|
||||
5. Morphological Gradient (kernel 3x3 - 7x7)
|
||||
6. Closing (Fill holes / Connect edges)
|
||||
7. Connected Components Filter (Remove small components)
|
||||
8. FindContours
|
||||
9. Generate Mask and Apply to Original
|
||||
"""
|
||||
|
||||
# 1. Read Image
|
||||
img = cv2.imread(image_path)
|
||||
if img is None:
|
||||
print(f"Error reading {image_path}")
|
||||
return
|
||||
|
||||
# 2. Resize
|
||||
# Use target_size from config if available, default to 224x224
|
||||
img_resized = cv2.resize(img, target_size)
|
||||
|
||||
# 3. Grayscale
|
||||
if len(img_resized.shape) == 3:
|
||||
gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY)
|
||||
else:
|
||||
gray = img_resized
|
||||
|
||||
# 4. GaussianBlur (3-7)
|
||||
blurred = cv2.GaussianBlur(gray, blur_ksize, 0)
|
||||
|
||||
# 5. Adaptive Threshold (blockSize 11-51 odd, C 2-10)
|
||||
thresh = cv2.adaptiveThreshold(blurred, 255,
|
||||
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
||||
cv2.THRESH_BINARY_INV,
|
||||
adaptive_block_size,
|
||||
adaptive_C)
|
||||
|
||||
# 6. Morphological Gradient (kernel 3x3 - 7x7)
|
||||
kernel_grad = cv2.getStructuringElement(cv2.MORPH_RECT, (gradient_ksize, gradient_ksize))
|
||||
gradient = cv2.morphologyEx(thresh, cv2.MORPH_GRADIENT, kernel_grad)
|
||||
|
||||
# 7. Closing (Fill holes / Connect edges)
|
||||
kernel_close = cv2.getStructuringElement(cv2.MORPH_RECT, (closing_ksize, closing_ksize))
|
||||
closed = cv2.morphologyEx(gradient, cv2.MORPH_CLOSE, kernel_close)
|
||||
|
||||
# 8. Connected Components Filter (Remove small components)
|
||||
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(closed, connectivity=8)
|
||||
|
||||
# Create a mask with only large components
|
||||
filtered_components_mask = np.zeros_like(closed)
|
||||
for i in range(1, num_labels): # Skip background (0)
|
||||
area = stats[i, cv2.CC_STAT_AREA]
|
||||
if area >= min_area:
|
||||
filtered_components_mask[labels == i] = 255
|
||||
|
||||
# 9. FindContours (on the filtered components)
|
||||
contours, _ = cv2.findContours(filtered_components_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
# Create a filled mask from contours
|
||||
final_mask = np.zeros_like(closed)
|
||||
|
||||
for cnt in contours:
|
||||
# We can also apply aspect ratio/solidity filters here if needed,
|
||||
# but user specifically asked for "Connected Components Filter -> FindContours"
|
||||
# Assuming CC filter is the main filter now.
|
||||
cv2.drawContours(final_mask, [cnt], -1, 255, -1) # Fill contour
|
||||
|
||||
# 10. Apply Mask to Original Image (Combine)
|
||||
masked_img = cv2.bitwise_and(img_resized, img_resized, mask=final_mask)
|
||||
|
||||
# 11. Save result
|
||||
cv2.imwrite(save_path, masked_img)
|
||||
|
||||
def main():
|
||||
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Pytorch/
|
||||
|
||||
# Construct paths
|
||||
dataset_path = os.path.abspath(os.path.join(base_dir, "..", "Dataset", "Training"))
|
||||
|
||||
if not os.path.exists(dataset_path):
|
||||
training_path = os.path.abspath(os.path.join(base_dir, "..", "Dataset", "Training_Crop"))
|
||||
if os.path.exists(training_path):
|
||||
dataset_path = training_path
|
||||
print(f"Dataset/Training_Crop not found. Using {dataset_path}")
|
||||
else:
|
||||
print(f"Error: Neither Training_Crop nor Training directories found at {dataset_path} or {training_path}")
|
||||
return
|
||||
|
||||
# Output directory for the new strategy (Strategy 3)
|
||||
save_root = os.path.abspath(os.path.join(base_dir, "..", "Dataset", "Masked_Training_Crop_Strategy3"))
|
||||
|
||||
print(f"Base Directory: {base_dir}")
|
||||
print(f"Dataset Path: {dataset_path}")
|
||||
print(f"Save Path: {save_root}")
|
||||
|
||||
if not os.path.exists(save_root):
|
||||
os.makedirs(save_root)
|
||||
print(f"Created save directory: {save_root}")
|
||||
|
||||
labels = Loading_Config.get("Training_Labels", [])
|
||||
if not labels:
|
||||
labels = ["stomach_cancer_Crop", "Normal_Crop", "Have_Question_Crop"]
|
||||
|
||||
image_size = Training_Config.get("Image_Size", 224)
|
||||
|
||||
for label in labels:
|
||||
label_dir = os.path.join(dataset_path, label)
|
||||
save_label_dir = os.path.join(save_root, label)
|
||||
|
||||
if not os.path.exists(save_label_dir):
|
||||
os.makedirs(save_label_dir)
|
||||
|
||||
if not os.path.exists(label_dir):
|
||||
print(f"Skipping {label}: Directory not found at {label_dir}")
|
||||
continue
|
||||
|
||||
# Find images
|
||||
types = ('*.jpg', '*.png', '*.jpeg', '*.bmp')
|
||||
images = []
|
||||
for t in types:
|
||||
images.extend(glob(os.path.join(label_dir, t)))
|
||||
|
||||
print(f"Processing {label}: {len(images)} images")
|
||||
|
||||
for img_path in tqdm(images):
|
||||
fname = os.path.basename(img_path)
|
||||
save_path = os.path.join(save_label_dir, fname)
|
||||
|
||||
# Execute pipeline
|
||||
preprocess_image(img_path, save_path,
|
||||
target_size=(image_size, image_size),
|
||||
blur_ksize=(5, 5),
|
||||
adaptive_block_size=31,
|
||||
adaptive_C=6,
|
||||
gradient_ksize=3,
|
||||
closing_ksize=5,
|
||||
min_area=100)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 83 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 71 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 84 KiB |
|
After Width: | Height: | Size: 71 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 65 KiB |
|
After Width: | Height: | Size: 87 KiB |
|
After Width: | Height: | Size: 76 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 77 KiB |
|
After Width: | Height: | Size: 83 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 75 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 76 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 91 KiB |
|
After Width: | Height: | Size: 70 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 64 KiB |
|
After Width: | Height: | Size: 77 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 66 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 85 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 86 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 68 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 58 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 76 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 59 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 92 KiB |
|
After Width: | Height: | Size: 69 KiB |
|
After Width: | Height: | Size: 71 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 77 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 79 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 71 KiB |