-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorsegmentation_HomeAssignment.m
More file actions
57 lines (45 loc) · 1.88 KB
/
colorsegmentation_HomeAssignment.m
File metadata and controls
57 lines (45 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
% Main script to process the cancer images
clear all;
close all;
% Load images
image1 = imread('Images/cancer_image_1.png');
image2 = imread('Images/cancer_image_2.png');
% Generate binary masks using color-based segmentation
mask1 = ColorBasedSegmentation(image1);
mask2 = ColorBasedSegmentation(image2);
% Display results for first image
figure(1);
subplot(1,3,1); imshow(image1); title('Tissue Image 1');
subplot(1,3,2); imshow(mask1); title('Cancer Binary Mask 1');
% Display results for second image
figure(2);
subplot(1,3,1); imshow(image2); title('Tissue Image 2');
subplot(1,3,2); imshow(mask2); title('Cancer Binary Mask 2');
% Save masks
imwrite(mask1, 'Images/Co_cancer_mask_1.png');
imwrite(mask2, 'Images/Co_cancer_mask_2.png');
function [binaryMask] = ColorBasedSegmentation(inputImage)
% seperate channels
hsvImage = rgb2hsv(inputImage);
h = hsvImage(:,:,1);
s = hsvImage(:,:,2);
v = hsvImage(:,:,3);
% Otsu's method to compute a threshold on the hue and saturation channels.
hueThresh = graythresh(h);
satThresh = graythresh(s);
% cancer areas tend to have lower hue values (more purple)
cancerMask_hsv = (h < hueThresh) & (s > satThresh) & (v < 0.8);
% Cancerous regions tend to have a high density of cell boundaries (Edge Detector).
grayImage = rgb2gray(inputImage);
edgeImage = edge(grayImage, 'Sobel');
se_edge = strel('disk', 2);
dilatedEdges = imdilate(edgeImage, se_edge);
% combinee the color mask with the edge infor.
enhancedMask = cancerMask_hsv | dilatedEdges;
%refine with morphological operations.
binaryMask = bwareaopen(enhancedMask, 200);
binaryMask = imfill(binaryMask, 'holes');
se = strel('disk', 3);
binaryMask = imclose(binaryMask, se);
binaryMask = imopen(binaryMask, strel('disk', 1));
end