Image segmentation OWT-UCM Hierarchical Segmentation algorithm code application and extraction of hierarchical segmentation results

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

A Contour Detection and Hierarchical Image Segmentation paper in Berkeley proposed a new image segmentation algorithm. This article will briefly introduce the application of the code corresponding to the algorithm. 

The paper is introduced in this blog
Berkeley Resources , this page includes links to databases and code downloads. 
write picture description here 
Download the source code, this code needs to run under MATLAB in Linux or Mac. The most important thing is that the code requires 5G memory to run, so when the system is less than 5G, there is a high probability that it will not run normally. I have successfully run it under Ubuntu16.04 and matalbr2014a.

1. gcc downgrade

The gcc version that comes with Ubuntu 16.04 is higher than the gcc version supported by MATLAB 2014, so it needs to be downgraded to 4.7. If the version of gcc that comes with your Linux system matches the version of gcc supported by your installed matlab, there is no need to downgrade or upgrade.

sudo apt-get install –y gcc-4.7
sudo apt-get install –y g++-4.7
cd /usr/bin
sudo rm gcc
sudo ln –s gcc-4.7 gcc
sudo rm g++
sudo ln –s g++-4.7 g++
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Enter gcc –version in the terminal to see if the downgrade is successful 

write picture description here

2. Prepare

This part refers to BSR_source/BSR/grouping/source/README_linux. By default, matlab has been successfully installed and mex has been configured.

Make sure your system has the required image library installed

$ sudo apt-get install libjpeg-dev
$ sudo apt-get install libpng-dev
  • 1
  • 2
  • 3

Force matlab to load the system's latest library instead of its own older version

$ export LD_PRELOAD=/lib/x86_64-linux-gnu/libc.so.6:/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/lib/x86_64-linux-gnu/libgcc_s.so.1
  • 1

3. Compile

Edit the following line in the file BSR_source/BAR/grouping/source/gpb_src/Rules.make to point to the directory where matlab is installed

MATLAB_PATH := /usr/local/MATLAB/R2014a/ #Set your own matlab installation path

Matlab mex file compilation settings, matlab architecture 
32-bit system set the following statement 
MATLAB_ARCH := glnx86 
MEX_EXTN := mexglx 
64-bit system set the following statement

MATLAB_ARCH := glnxa64 
MEX_EXTN := mexa64

In the terminal, cd into the /BSR/grouping/source folder and execute the command

$ source build.sh
  • 1

This command will compile all the source code, if the compilation is successful, a situation similar to the following figure will appear in the terminal: 
write picture description here

4. Run

There are three demo files under grouping, but there is a high chance that an error will be reported when running directly. The basic steps of the OWT-UCM algorithm are gPb-OWT-UCM. For the downloaded code, we only need to call the most important function im2ucm and make appropriate modifications. I put two images under ~/Data/VOC2012/test/ for testing. 
write picture description here

imgDir='~/Data/VOC2012/test/';   %存放图片的文件夹
outDir='~/Data/VOC2012/UCMImages/'   %存放结果的文件夹
D=dir(fullfile(imgDir,'*.jpg'));
tic
for i=1:numel(D)
    outFile=[outDir D(i).name(1:end-4) '.bmp'];    %输出结果图片的文件名,保存为bmp格式
    imgFile=[imgDir D(i).name];                %输入图片的文件名
    im2ucm(imgFile,outFile);                   %调用im2ucm函数
end

toc
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

! Tip: It is best to save the output results as bmp, in order to extract the hierarchical segmentation results later.

At the same time, it is necessary to modify part of the code in the im2ucm.m file and modify the save method to adapt to the bmp format.

function [ucm2] = im2ucm(imgFile, outFile)

gPb_orient = globalPb(imgFile, outFile);
ucm2 = contours2ucm(gPb_orient, 'doubleSize');
% save(outFile,'ucm2');
imwrite(ucm2,outFile,'bmp');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

If the operation is successful, something similar will be displayed on the matlab command line. 
write picture description here

The UCM results are as follows 
write picture description here

Extract the hierarchical segmentation results of UCM, and attach the code as follows, which can be run on Windows.

str = 'F:\Data\VOC2012\test\';   %UCM结果存放路径
%ids.txt保存的是图片id方便后面建立文件夹
mIDfile = importdata('F:\Data\VOC2012\ids.txt');  
for i=1:2    %目前只有2个图片,所以长度为2
    str1=strcat(str,strcat(mIDfile(i),'.bmp'));
    str1=char(str1)
    img = imread(str1);
    idx = unique(img);   %得到UCM结果图中的pixel value的集合
    maxL = length(idx);
     mkdir(['D:\Data\VOC2012\UCM\' ...
        char(mIDfile(i))]);
    for j=1:maxL-1    %提取分层结果并保存
        im = uint8(img>idx(j)); 
        [pointy,pointx] = find(im==0);
         [pointy1,pointx1] = find(im==1);
        im(sub2ind(size(im),pointy,pointx)) = 255;
        im(sub2ind(size(im),pointy1,pointx1)) = 0;

        imwrite(im, ['D:\Data\VOC2012\UCM\' ...
        char(mIDfile(i)) '\' num2str(j) '.bmp'],'BMP');
    end
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

The result for 2007_000032 is as follows 
write picture description here

For the above case, the content of ids.txt is the number of the two pictures 
write picture description here

For the ids that generate pictures in batches, the following techniques are provided. 
First, enter the dos window of windows, and cd to the folder where the generated pictures are stored. 
Then enter dir/s/on/b>F:/1.txt to save the path of the named image to F:/1.txt. Of course, the save path of txt can be set arbitrarily. 
write picture description here

write picture description here

You can use Notepad++ to replace all 'F:\Data\VOC2012\test\' and '.bmp' with empty, so only the picture number is left. 
write picture description here

Guess you like

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