Segmentation obtained by thresholding the UCM at certain level

Reprinted from: https://blog.csdn.net/skye1221/article/details/77511797

write picture description here

This article mainly talks about how to obtain the result on the far right. The paper can refer to: 
From Contours to Regions: An Empirical Evaluation 
Contour Detection and Hierachical Image Segmentation

According to the above figure, we can get the general idea, given the original image and its corresponding UCM and a certain parameter factor, we can get the result we want. The general steps are as follows: 
1. Read an original image and its corresponding UCM 
2. Set thresholding 
3. Obtain a UCM map of a certain level according to the thresholding 
4. Set labels for all regions of the UCM map 
5. Build a Three-channel RGB matrix, each channel is the UCM map corresponding to the thresholding level 
6. Find the two-dimensional coordinates corresponding to the label of all UCM maps in the original image 
7. On the basis of the original image, find the coordinates of the regions corresponding to the three channels. Pixel value, and then find the average 
8. Assign the corresponding average to each region of the three channels corresponding to RGB

code show as below:

load('data/101087_ucm2.mat','ucm2');
image=imread('data/101087.jpg');
%convert ucm to the size of the original image
ucm = ucm2(3:2:end, 3:2:end);

%get the boundaries of segmentation at scale k in range [0 1]
k = 0.4;
bdry = (ucm >= k);

%get superpixels at scale k without boundaries:
labels2 = bwlabel(ucm2 <= k);
labels = labels2(2:2:end, 2:2:end);

% figure;imshow('data/101087.jpg');
% figure;imshow(ucm);
% figure;imshow(bdry);
% change format
temp=mat2gray(bdry);
temp=uint8(temp*255);
%set a three channels matrix to save result,all the channel are
%identical,etc,temp. The three channel,as we all know, reprents RGB
RGB(:,:,1)=temp;
RGB(:,:,2)=temp;
RGB(:,:,3)=temp;
% the number of different label values in labels matrix
label=unique(labels);
for i=1:length(label)
    [x,y]=find(labels==i); % find the location where labels equal i
    [m,n]=size([x,y]); %m is the numberi of piexl node of the region
    sum=zeros(3,1); % make a 3x1 matrix to save the sum value
    for j=1:m  % calculate the sum
        sum(1,1)=sum(1,1)+double(image(x(j),y(j),1));
        sum(2,1)=sum(2,1)+double(image(x(j),y(j),2));
        sum(3,1)=sum(3,1)+double(image(x(j),y(j),3));
    end
    %calculate the average
    average1=round(sum(1,1)/m);
    average2=round(sum(2,1)/m);
    average3=round(sum(3,1)/m);
    for k=1:m
        % set each channel 
        RGB(x(k),y(k),1)=uint8(average1);
        RGB(x(k),y(k),2)=uint8(average2);
        RGB(x(k),y(k),3)=uint8(average3);
    end
end
%if you want show the contour,you should need the following code
[x2,y2]=find(temp==255);
[m2,n2]=size([x2,y2]);
for i=1:m2
    %set contour for each channel
    RGB(x2(i),y2(i),1)=255;
    RGB(x2(i),y2(i),2)=255;
    RGB(x2(i),y2(i),3)=255;
end
  • 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

The original image and UCM are read in this code, which is an example of the source code BSR provided by Berkeley directly, which can be modified accordingly. 
The intuitive picture data is as follows:

original image 
write picture description here

UCM  
write picture description here 
map of a certain level of UCM 
write picture description here

Results without boundary contours 
write picture description here

Results with bounding contours 
write picture description here



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648447&siteId=291194637