0% found this document useful (0 votes)
51 views

DIP - Mini Project

The document provides instructions for a project on region-based image segmentation using MATLAB. It describes using the Image Segmenter app to segment images with different techniques like thresholding (global, manual, adaptive), graph cuts, clustering, and finding circles. Code examples are given for each segmentation method to generate a binary mask and masked output image. The objectives are to understand and test various region-based segmentation approaches.

Uploaded by

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

DIP - Mini Project

The document provides instructions for a project on region-based image segmentation using MATLAB. It describes using the Image Segmenter app to segment images with different techniques like thresholding (global, manual, adaptive), graph cuts, clustering, and finding circles. Code examples are given for each segmentation method to generate a binary mask and masked output image. The objectives are to understand and test various region-based segmentation approaches.

Uploaded by

Varsha Agwane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DEPARTMENT OF BIOMEDICAL ENGINEERING Experiment No.

Semester T.E. Semester VI – Biomedical Engineering


Subject Digital Image Processing
Subject Professor In-charge Prof. Geetha Narayanan
Laboratory GE Lab/Online

Student Name Rohit Masmar


Roll Number 21105A2001
Grade and Subject
Teacher’s Signature

Experiment PBL
Number
Experiment Project on region based segmentation used in Biomedical Image Processing.
Title
Resources / Hardware: Software: Matlab online
Apparatus IBM PC Compatible Computer System
Required

Objectives To understand region based segmentation using the Image Segmenter App that provides access to many
(Skill Set / different ways to segment an image.
Knowledge
Tested /
Imparted)

Theory of Performing segmentation using Image Segmenter can be an iterative process where we might try
Operation several of the segmentation options. Some segmentation techniques might work better with
certain types of images than others. After segmenting an image, we can save the binary mask.
We can also retrieve the code that Image Segmenter used to create the mask.

Segment Image Using Thresholding in Image Segmenter:


Program:
[Global Threshold]

function [BW,maskedImage] = segmentImage(X)


%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


X = imadjust(X);
% Threshold image with global threshold
BW = imbinarize(im2gray(X));

% Create masked image.


maskedImage = X;
maskedImage(~BW) = 0;
end

Manual Threshold:

function [BW,maskedImage] = segmentImage(X)


%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


X = imadjust(X);

% Threshold image with manual threshold


BW = im2gray(X) > 2.500000e-01;

% Create masked image.


maskedImage = X;
maskedImage(~BW) = 0;
end

Adaptive Threshold:
function [BW,maskedImage] = segmentImage(X)
%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


X = imadjust(X);

% Threshold image with adaptive threshold


BW = imbinarize(im2gray(X), 'adaptive', 'Sensitivity', 0.410000, 'ForegroundPolarity',
'bright');

% Create masked image.


maskedImage = X;
maskedImage(~BW) = 0;
end

Segment Image Using Graph Cut in Image Segmenter:

function [BW,maskedImage] = segmentImage(RGB)


%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(RGB) segments image RGB using
% auto-generated code from the Image Segmenter app. The final segmentation
% is returned in BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


RGB = imadjust(RGB);

% Convert RGB image into L*a*b* color space.


X = rgb2lab(RGB);

% Graph cut
foregroundInd = [3124781 3153509 3153517 3153525 3153549 3153557 3153565 3153573
3182262 3182278 3182286 3211006 3211030 3211038 3211046 3239632 3239648 3239663
3239679 3239743 3239759 3239790 3267932 3267940 3267948 3267956 3267964 3267980
3268004 3268019 3268035 3268051 3268083 3268091 3268099 3268115 3268123 3268138
3268146 3268154 3268162 3268170 3268178 3268202 3268234 3268249 3268257 3268265
3268273 3268281 3268289 3296685 3296693 3379382 3407864 3407872 3407880 3407888
3407896 3407904 3407912 3407936 3407983 3407999 3408007 3408023 3408047 3408063
3408078 3408086 3408142 3436609 3436617 3436625 3436633 3551690 3580466 3666787
3691971 3720723 3720731 3749508 3778284 3807052 3807060 3807068 3807076 3835828
3835836 3835844 3864620 3893381 3893397 3922165 3922173 3922181 3950941 3950957
4062478 4062493 4091278 4120070 4148854 4206430 4235222 4235230 4264014 4292806
4350390 4379190 4407982 4461998 4490782 4519582 4577190 4577198 4605998 4634798
4634806 4663606 4692406 4692414 4721222 4721230 4750030 4750038 4778838 4778846
4804046 4804054 4804062 4804070 4832870 4976893 5034501 5232581 5290197 5319020
5319028 5376636 5405460 5463092 5491908 5491915 5574747 5690019 5690026 5718834
5718850 5776474 5805290 5834106 5834114 5862922 5891738 5916953 5916969 5945777
5945785 6032288 6061112 6089912 6089920 6118728 6118736 6147560 6176367 6259239
6288047 6489742 6518542 6576197 6605005 6605013 6630221 6630229 6659029 6659045
6745485 6831908 6860724 6860732 6889532 6975972 7029988 7260467 7289267 7289275
7318091 7346899 7346907 7372123 7429762 7545002 7545010 7545018 7545026 7573834
7660329 7689160 7689168 7717984 7717992 7743200 7743208 7743216 7772032 7800832
7800840 7800848 7800856 7800863 7800871 7800879 7800895 7800903 7800927 7800951
7800959 ];
backgroundInd = [2040094 2040158 2040205 2068823 2068831 2069045 2069061 2069085
2069093 2069101 2069108 2069116 2069124 2069132 2069140 2069156 2069164 2069204
2069227 2069235 2069259 2069275 2069291 2069299 2069315 2069330 2097591 2184649
2184657 2184673 2237849 2353992 2354000 2381761 2382808 2382816 2411624 2496890
2496898 2554458 2554474 2579642 2608426 2694795 2723563 2781139 2867492 2979036
3007820 3122965 3151749 3238101 3295662 3295678 3349646 3378430 3407222 3464806
3551182 3637567 3720351 3806743 3893135 4148719 4206319 4292711 4548287 4634679
4749879 4832671 4919071 4947871 5005471 5063063 5091863 5120663 5174663 5318663
5462663 5574271 5603071 5916279 6315895 6344695 6459927 6546351 6629175 6686806
6802062 6830870 6830878 6830886 6859709 6859725 6888525 6888541 6888549 6917365
6946181 6946197 6946204 6975012 6975028 7000244 7029060 7057884 7057908 7086723
7086739 7115587 7115603 7115634 7115642 7115658 7144601 7144617 7144625 7144633
7144641 7144656 7144672 7144680 7144704 7144736 7144775 7144823 7144831 7144839
7144847 7173663 7173686 7260197 7289005 7289013 7289021 7289029 7289045 7289053
7289061 7289101 7289108 7317908 7317916 ];
L = superpixels(X,40600,'IsInputLab',true);

% Convert L*a*b* range to [0 1]


scaledX = prepLab(X);
BW = lazysnapping(scaledX,L,foregroundInd,backgroundInd);

% Create masked image.


maskedImage = RGB;
maskedImage(repmat(~BW,[1 1 3])) = 0;
end
function out = prepLab(in)

% Convert L*a*b* image to range [0,1]


out = in;
out(:,:,1) = in(:,:,1) / 100; % L range is [0 100].
out(:,:,2) = (in(:,:,2) + 86.1827) / 184.4170; % a* range is [-86.1827,98.2343].

Segment Image Using Auto Cluster in Image Segmenter:

function [BW,maskedImage] = segmentImage(X)


%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


X = imadjust(X);

% Auto clustering
s = rng;
rng('default');
L = imsegkmeans(single(X),2,'NumAttempts',2);
rng(s);
BW = L == 2;

% Fill holes
BW = imfill(BW, 'holes');

% Create masked image.

Segment Image Using Find Circles in Image Segmenter:

function [BW,maskedImage] = segmentImage(X)


%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


X = imadjust(X);

% Find circles
[centers,radii,~] = imfindcircles(X,[23
75],'ObjectPolarity','bright','Sensitivity',0.85);
BW = false(size(X,1),size(X,2));
[Xgrid,Ygrid] = meshgrid(1:size(BW,2),1:size(BW,1));
for n = 1:numel(radii)
BW = BW | (hypot(Xgrid-centers(n,1),Ygrid-centers(n,2)) <= radii(n));
end
Segment Image Using Local Graph Cut (Grabcut) in Image Segmenter:

function [BW,maskedImage] = segmentImage(RGB)


%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(RGB) segments image RGB using
% auto-generated code from the Image Segmenter app. The final segmentation
% is returned in BW, and a masked image is returned in MASKEDIMAGE.

% Auto-generated by imageSegmenter app on 30-Mar-2023


%----------------------------------------------------

% Adjust data to span data range.


RGB = imadjust(RGB);

% Convert RGB image into L*a*b* color space.


X = rgb2lab(RGB);

% Create empty mask


BW = false(size(X,1),size(X,2));

% Local graph cut


xPos = [363.2489 3244.6586 3244.6586 363.2489 ];
yPos = [275.7775 275.7775 2164.1300 2164.1300 ];
m = size(BW, 1);
n = size(BW, 2);
ROI = poly2mask(xPos,yPos,m,n);
foregroundInd = [1003181 1003197 1003202 1003207 1003212 1003222 1003228 1003238
1003243 1003258 1014836 1014846 1014851 1014969 1014974 1026505 1026721 1040506
1040511 1040516 1040758 1040768 1040773 1052124 1052129 1052134 1052140 1052145
1052155 1052160 1052176 1052181 1052469 1063789 1063794 1063799 1064154 1064159
1075453 1075839 1075844 1098793 1098803 1099215 1110467 1110473 1110735 1110740
1110745 1110750 1110756 1110900 1124483 1124751 1124916 1136158 1136472 1136596
1136601 1147838 1148121 1148126 1148157 1148276 1159806 1159848 1159853 1159940
1159945 1171486 1171492 1171538 1171615 1182868 1183172 1183177 1183223 1183233
1183239 1183244 1183249 1183254 1183259 1183264 1183269 1183275 1183280 1183285
1183290 1194857 1208564 1208873 1231924 1232233 1232238 1243604 1255608 1255613
1255619 1255624 1266969 1278649 1278989 1278994 1292665 1293020 1293025 1293041
1293046 1293056 1293072 1293082 1293097 1293108 1293113 1293118 1293123 1293128
1293133 1293139 1293144 1293154 1293159 1339395 1351559 1363327 1363332 1377348
1388935 1388940 1388945 1388951 1389023 1389028 1400646 1400656 1400661 1400667
1400672 1400677 1400682 1400687 1400692 1400697 1400713 1400723 1400728 1400734
1400739 1423491 1424088 1484804 1484809 1496463 1508117 1508128 1519736 1519741
1519746 1519751 1519761 1519767 1519772 1519777 1519782 1533747 1557101 1568781
1580431 1580436 1580441 1580446 1580451 1580456 3491906 3587682 3613378 3625058
3636738 3648418 3697479 3744205 3769906 3793266 3877372 3900737 3924097 3938113
3949793 3961478 3984838 4080619 4129681 4153041 4178737 4274513 4286193 4405323
4431019 4489419 4526790 4550150 4825778 4863154 4886509 4898189 4935565 4970605
5115437 5150477 5570941 5753139 5776494 5799854 5811534 5848910 5883950 5944686
6017102 6112878 6185294 6292750 6304430 6330126 6341806 6425902 6437582 6449262
6472622 6509993 6556707 6666489 6713199 6762245 6773919 6785594 6797269 6808944
6846310 6846315 6881339 6893019 7026151 7075202 7121911 7145266 7159282 7194312
7217672 7243363 7278393 7290067 7301742 7301747 7313422 7327428 7327433 7339103
7350778 7350783 7362458 7374132 7374138 7397462 7397467 7397472 ];
backgroundInd = [5233276 5294012 5305692 5317372 5702853 5847706 5882751 5894431
5906111 5920132 6027614 6050984 6074349 6088370 6100055 6111741 6135111 6184203
6195888 6207578 6219269 6244985 6268371 6280061 6280082 6280087 6291782 6291788
6291803 6291813 6303498 6303504 6303509 6303514 6303519 6303524 ];
L = superpixels(X,41027,'IsInputLab',true);
% Convert L*a*b* range to [0 1]
scaledX = prepLab(X);
BW = BW | grabcut(scaledX,L,ROI,foregroundInd,backgroundInd);

% Local graph cut


xPos = [882.9317 913.8040 913.8040 882.9317 ];
yPos = [641.0991 641.0991 1135.0551 1135.0551 ];
m = size(BW, 1);
n = size(BW, 2);
ROI = poly2mask(xPos,yPos,m,n);
foregroundInd = [];
backgroundInd = [];
L = superpixels(X,41027,'IsInputLab',true);

% Convert L*a*b* range to [0 1]


scaledX = prepLab(X);
BW = BW | grabcut(scaledX,L,ROI,foregroundInd,backgroundInd);

% Create masked image.


maskedImage = RGB;
maskedImage(repmat(~BW,[1 1 3])) = 0;
end

function out = prepLab(in)

% Convert L*a*b* image to range [0,1]


out = in;
out(:,:,1) = in(:,:,1) / 100; % L range is [0 100].
out(:,:,2) = (in(:,:,2) + 86.1827) / 184.4170; % a* range is [-86.1827,98.2343].
out(:,:,3) = (in(:,:,3) + 107.8602) / 202.3382; % b* range is [-107.8602,94.4780].

end

Segment Image Using Thresholding in Image Segmenter:


Outputs
Segment Image Using Graph Cut in Image Segmenter:
Segment Image Using Auto Cluster in Image Segmenter:
After filling holes:

Segment Image Using Find Circles in Image Segmenter:


Segment Image Using Local Graph Cut (Grabcut) in Image Segmenter:
Segment Image by Drawing Regions Using Image Segmenter:
Conclusion
We have seen that segmentation consists in dividing an image into several homogeneous
regions. The homogeneity of a region is based on color, texture, contours, etc.The methods of
segmentation are very diverse, and we have seen a few of them.

You might also like