Graph cut Segmentation(Simplest Implementation) | Digital Image Processing | MATLAB
ฝัง
- เผยแพร่เมื่อ 10 ก.พ. 2025
- Prerequisite:
-------------------
Interactive Image Segmentation In-depth Intuition
• Interactive Image Segm...
Code:
clc
clear all
close all
warning off
RGB=imread('yellowlily.jpg');
subplot(1,3,1);
imshow(RGB);
title('Original Image');
[BW,maskedImage] = segmentImage(RGB);
subplot(1,3,2);
imshow(BW);
title('Segmented Binary Image');
subplot(1,3,3);
imshow(maskedImage);
title('Segmented Color Image');
Function Code:
function [BW,maskedImage] = segmentImage(RGB)
X = rgb2lab(RGB);
% Graph cut
foregroundInd = [305810 362952 446521 446528 457927 461222 479174 483687 483691 484026 505286 526500 534598 559136 566925 573635 580283 580345 588334 591598 591601 609650 609712 630924 642279 660291 674912 701078 707541 736900 751656 761360 769442 769471 769491 769506 776130 790634 797346 808517 823458 834682 847942 870799 873808 885496 895059 900202 903477 909716 913291 921471 924792 924803 924810 928019 928032 928045 931349 936250 939520 954213 960311 968910 971768 983159 990130 996212 1007486 1007488 1007492 1007499 1007510 1010794 1011346 1014009 1014153 1019038 1022169 1022242 1022267 1022284 1025646 1036862 1047241 1051556 1052128 1058641 1061359 1061880 1061894 1066756 1070000 1076269 1079322 1079777 1087755 1087777 1087921 1091068 1094017 1094350 1097503 1097521 1097700 1105448 1105661 1105669 1109053 1111976 1112375 1117069 1123397 1123582 1129914 1130280 1135210 1141434 1141438 1141522 1144587 1149587 1149600 1149886 1156097 1162519 1162927 1167510 1167550 1170876 1174025 1177619 1180451 1188684 1192079 1195121 1195283 1195567 1195571 1206525 1209865 1217938 1221439 1221674 1224533 1239329 1239635 1242407 1242467 1246152 1250616 1257093 1263663 1263849 1271781 1275045 1275537 1278318 1283234 1283688 1286487 1289901 1304627 1316328 1322852 1329056 1333992 1352232 1362007 1366581 1366612 1376373 1376714 1384533 1384553 1394327 1394334 1399525 1427306 1438679 1463210 1468044 1503935 1504010 1528406 1533386 1551248 1569195 1569284 1583883 1593759 1596939 1616528 1623122 1629595 1637777 1637790 1637799 ];
backgroundInd = [23312 26611 52732 73862 89490 89492 92750 99286 103244 103256 103269 103271 103276 103313 104171 106489 109808 117879 121279 125407 135942 136820 139106 145577 163921 168879 168913 172201 172241 178186 186978 192929 193546 207546 208304 208677 210994 223081 225536 228751 229645 241097 244375 247880 255817 262582 265624 265852 265855 274017 277066 285239 285443 288652 290754 297275 298308 305612 345628 353788 363580 371744 375015 381536 386434 386607 392962 400211 404386 419074 421267 424540 446818 471959 514495 533926 537084 543612 547106 558333 567586 569812 597461 623662 652934 662781 667661 701410 721584 742720 752004 760626 772125 817284 822615 829296 845255 923801 931524 941716 941853 971123 971273 995603 1006982 1021717 1042935 1062084 1065739 1069129 1078841 1093534 1103119 1140864 1145316 1195955 1224096 1233444 1238975 1251618 1256774 1277950 1289569 1295935 1315685 1316714 1316887 1333469 1346698 1351385 1361217 1377260 1384236 1406436 1409762 1409765 1413000 1413020 1419989 1423191 1427697 1429941 1434682 1444425 1449378 1450534 1452642 1459164 1468519 1471710 1475016 1475153 1477278 1486413 1491735 1495232 1504392 1506658 1509922 1513186 1514140 1514184 1519036 1522231 1522300 1525608 1527641 1528825 1540183 1540243 1540289 1546762 1558179 1558226 1558357 1560288 1566297 1566339 1569596 1584280 1584296 1586402 1587513 1587540 1597305 1605465 1608929 1611993 1615780 1619049 1623421 1658467 1661740 1666610 1667448 1673180 1679732 1687781 1690952 1702613 1703332 1703545 1708953 1739216 1749990 1760607 1772051 1775102 1786515 1789198 1793245 1796513 1815873 1837074 1851742 1854533 1858250 1869649 1876157 1887557 1893744 1894072 1908744 1911997 1916623 1923417 1929936 1934608 1938091 1941347 1941351 1944420 1944608 1949493 1955868 1956012 1959265 1962516 1962518 1967312 1967328 1967341 1967352 1967374 1967390 1967403 ];
L = superpixels(X,10088,'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:3) = (in(:,:,2:3) + 100) / 200; % a* and b* range is [-100,100].
end
Learn Complete Image Processing & Computer Vision using MATLAB:
• Digital Image Processi...
#DigitalImageProcessing #MATLAB #ComputerVision #ImageSegmentation
Thank you sooo much sir❤❤❤
You are welcome @zahraarab7! Happy Learning
hi sir. how can i save the image individually? i want to save the original image as separate file, the segmented image as separate file and masked image as separate file
Hello WAN MUHAMAD BAQIR BIN MAHDI / UPM , you can use imwrite function to write any image , for details you can refer this documentation link -- in.mathworks.com/help/matlab/ref/imwrite.html Hope this will be helfpul! Happy Learning
@@KnowledgeAmplifier1 thank you so much sir
Sir in the line [BW,maskedImage]= segmentImage(RGB);.I'm getting error stating that unrecognised functions or variable ' segmentImage' how can I rectify this error sir
Hello sukesh venkatesh, have you created the function as explained in the video?
thank you
You're welcome KINGRAYZ! Happy Learning :-)
sir my problem is to cut image ( a bunch of leaf image) which has same background (white color) how can I create by just a code to directly cut the image?
hello sir, i have problem at image segmenter windows. When I want to load the image from the workspace but there is no variable exists and the filter only shows intensity (M-by-N). i already followed ur coding.
thankyou in advance
Many thanks very helpful
Welcome Alaster McDonach
. Happy Coding :-)
Request sir, detect people smoked
really required.great idea.mr mukarji please look into this
Noted, I will try to implement this
Yes sir , I will look into this
@@KnowledgeAmplifier1 you can I am sure
motivating really...
Thank You sir :-)
May I know how to contact you? On Insta, email?