www.gusucode.com > images 案例代码 matlab源码程序 > images/SegmentImageIntoThreeRegionsUsingColorInformationExample.m

    %% Segment Image into Three Regions Using Color Information
% 
%%
% Read image into the workspace and display it.

% Copyright 2015 The MathWorks, Inc.

RGB = imread('yellowlily.jpg'); 
imshow(RGB,'InitialMagnification', 50)
hold on
%%
% Creates scribbles for three regions. Note that you can specify the 
% scribbles interactively using tools such as |roipoly|, |imfreehand|, 
% |imrect|, |impoly|, and |imellipse|. Region 1 is the yellow flower.
% Region 2 is the green leaves. Region 3 is the background.
region1 = [350 700 425 120]; % [x y w h] format
BW1 = false(size(RGB,1),size(RGB,2));
BW1(region1(2):region1(2)+region1(4),region1(1):region1(1)+region1(3)) = true;
 
region2 = [800 1124 120 230];
BW2 = false(size(RGB,1),size(RGB,2));
BW2(region2(2):region2(2)+region2(4),region2(1):region2(1)+region2(3)) = true;

region3 = [20 1320 480 200; 1010 290 180 240]; 
BW3 = false(size(RGB,1),size(RGB,2));
BW3(region3(1,2):region3(1,2)+region3(1,4),region3(1,1):region3(1,1)+region3(1,3)) = true;
BW3(region3(2,2):region3(2,2)+region3(2,4),region3(2,1):region3(2,1)+region3(2,3)) = true;
%%  
% Display the seed regions.
visboundaries(BW1,'Color','r');
visboundaries(BW2,'Color','g');
visboundaries(BW3,'Color','b');
%%
% Segment the image.
[L,P] = imseggeodesic(RGB,BW1,BW2,BW3, 'AdaptiveChannelWeighting', true);
%%  
% Display results.
figure
imshow(label2rgb(L),'InitialMagnification', 50)
title('Segmented image with three regions')
  
figure
imshow(P(:,:,2),'InitialMagnification', 50)
title('Probability that a pixel belongs to region/label 2')