0% found this document useful (0 votes)
11 views3 pages

Code

Uploaded by

danishnawaz398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Code

Uploaded by

danishnawaz398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Import necessary libraries

import cv2
import numpy as np
import [Link] as plt
from skimage import measure, morphology, exposure
from scipy import ndimage

# Install necessary library for file upload in Google Colab


try:
from [Link] import files
except ImportError:
pass

# Function to detect tumor and visualize results


def detect_and_visualize_tumor(image_path):

image = [Link](image_path, cv2.IMREAD_GRAYSCALE)

# Check if the image was loaded properly


if image is None:
print(f"Error: Unable to load the image {image_path}. Please check the
image path.")
return

print(f"Image {image_path} loaded successfully.")

# Display the original image


[Link]()
[Link](image, cmap='gray')
[Link]('Original Image')
[Link]('off')
[Link]()

# Apply median filter to the original image


image_filtered = [Link](image, 5)

# Display the filtered image


[Link]()
[Link](image_filtered, cmap='gray')
[Link]('Filtered Image')
[Link]('off')
[Link]()

# Enhance the filtered image using histogram equalization


image_enhanced = exposure.equalize_hist(image_filtered)

# Apply Gaussian blur to reduce noise


image_blurred = [Link](image_enhanced, (5, 5), 0)

# Edge detection using Canny


edges = [Link]((image_blurred * 255).astype(np.uint8), 100, 200)

# Display the enhanced image


[Link]()
[Link](image_blurred, cmap='gray')
[Link]('Enhanced Image')
[Link]('off')
[Link]()
# Display edge detection
[Link]()
[Link](edges, cmap='gray')
[Link]('Edge Detection')
[Link]('off')
[Link]()

# Tumor detection function using Otsu's thresholding


def detect_tumor(image):
# Apply Otsu's thresholding
_, bw = [Link]((image * 255).astype(np.uint8), 0, 255,
cv2.THRESH_BINARY + cv2.THRESH_OTSU)
label_img = [Link](bw)
props = [Link](label_img)

# Consider regions with solidity > 0.7 and significant area


high_dense_area = [prop for prop in props if [Link] > 0.7 and
[Link] > 100] # Adding area constraint
if not high_dense_area:
return None, None

max_area = max(high_dense_area, key=lambda x: [Link])


tumor_mask = label_img == max_area.label
tumor_mask = [Link](tumor_mask, [Link](5))

return tumor_mask, max_area.bbox

# Detect the tumor


tumor_mask, bbox = detect_tumor(image_blurred)
if tumor_mask is None:
print("No tumor detected.")
return

# Find contours around the tumor


contours, _ = [Link]((tumor_mask * 255).astype(np.uint8),
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours on the original image


image_with_contours = [Link](image, cv2.COLOR_GRAY2BGR)
[Link](image_with_contours, contours, -1, (0, 0, 255), 2)

# Display the original image with tumor contours


[Link]()
[Link](image_with_contours)
[Link]('Detected Tumor with Contours')
[Link]('off')
[Link]()

# Calculate and display the tumor area


tumor_area = [Link](tumor_mask)
print(f"Tumor Area (in pixels): {tumor_area}")

# Crop and display the tumor region


min_row, min_col, max_row, max_col = bbox
cropped_tumor = image[min_row:max_row, min_col:max_col]

[Link]()
[Link](cropped_tumor, cmap='gray')
[Link]('Cropped Tumor Region')
[Link]('off')
[Link]()

# Zoom into the cropped tumor region


zoom_factor = 2 # Define the zoom factor
zoomed_tumor = [Link](cropped_tumor, None, fx=zoom_factor, fy=zoom_factor,
interpolation=cv2.INTER_LINEAR)

# Display the zoomed tumor image in a separate figure


[Link]()
[Link](zoomed_tumor, cmap='gray')
[Link]('Zoomed Tumor Region')
[Link]('off')
[Link]()

# File upload in Google Colab


uploaded = [Link]()

# Process each uploaded image


for image_path in [Link]():
detect_and_visualize_tumor(image_path)

You might also like