转自:https://blog.youkuaiyun.com/skye1221/article/details/77511797
这篇主要讲如何获得最右边的结果,论文可以参考:
From Contours to Regions: An Empirical Evaluation
Contour Detection and Hierachical Image Segmentation
根据上图,我们可以得到一般的思路,给定原图和其对应的UCM以及某个参数因子,就可以得到我们想要的结果。大致步骤如下:
1、读取某一原图及其对应的UCM
2、设定thresholding
3、根据thresholding,得到某一level的UCM map
4、对该UCM map的所有regions设定label
5、构建一个三通道的RGB矩阵,每个通道都是thresholding对应level的UCM map
6、找到所有UCM map 的label在原图对应的二维坐标
7、在原图基础上,求regions的label对应坐标位置三个通道的像素值,然后求average
8、对RGB对应的三个通道的每个regions赋值相应的average
代码如下:
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
该代码中读取原图和UCM,是直接利用伯克利提供的源码BSR的example,可以做相应的修改。
直观图片数据如下:
原图
UCM
某一level的UCM map
没有边界轮廓的结果
有边界轮廓的结果