Application of OWT-UCM Hierarchical Segmentation Code in Image Segmentation and Extraction of Hierarchical Segmentation Results

Posted by oscar2 on Thu, 13 Jun 2019 20:26:37 +0200

A Contour Detection and Hierarchical Image Segmentation paper in Berkeley proposes a new image segmentation algorithm. This paper will briefly introduce the application of the corresponding code of the algorithm.
This article Blog This paper is introduced.
Berkeley Resources The page includes database and code download links.

Download the source code, which needs to run under MATLAB in Linux or Mac. The most important thing is that the code requires 5G of memory when it runs, so when the system is less than 5G, there is a high probability that it will not work properly. I have successfully operated under Ubuntu 16.04 and matalbr 2014.

1. GCC downgrade

The gcc version of Ubuntu 16.04 is higher than the gcc version supported by MATLAB 2014, so it needs to be downgraded to 4.7. If the gcc version of Linux system matches the gcc version supported by matlab installed by you, 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++

Enter gcc - version at the terminal to see if the downgrade is successful

2. Preparation

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

Ensure the image library needed for system installation

$ sudo apt-get install libjpeg-dev
$ sudo apt-get install libpng-dev

Force matlab to load the latest Library of the system, not its own old 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

3. Compilation

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 up 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 settings

MATLAB_ARCH := glnxa64
MEX_EXTN := mexa64

In the terminal, cd enters the / BSR/grouping/source folder and executes commands

$ source build.sh

This command compiles all source code, and if compiled successfully, the following situation will occur at the terminal:

4. Operation

There are three demo files under grouping, but there is a high probability of errors if you run them directly. The basic step of OWT-UCM algorithm is gPb-OWT-UCM. For downloaded code, we only need to call the most important function im2ucm and make appropriate modifications. I tested two images in ~/Data/VOC2012/test/.

imgDir='~/Data/VOC2012/test/';   %A folder for storing pictures
outDir='~/Data/VOC2012/UCMImages/'   %A folder for storing results
D=dir(fullfile(imgDir,'*.jpg'));
tic
for i=1:numel(D)
    outFile=[outDir D(i).name(1:end-4) '.bmp'];    %The file name of the output image is saved as bmp format
    imgFile=[imgDir D(i).name];                %File Name of Input Picture
    im2ucm(imgFile,outFile);                   %call im2ucm function
end

toc

! It is suggested that the output result should be saved as bmp, in order to extract the hierarchical segmentation result.

At the same time, it is necessary to modify part of the code in the im2ucm.m file and the way of saving 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');

If it runs successfully, it will display something like this on the matlab command line

The UCM results are as follows

Extract the results of UCM's hierarchical segmentation, attach the code as follows, the code can run in windows.

str = 'F:\Data\VOC2012\test\';   %UCM Result Storage Path
%ids.txt Save pictures. id Make it easy to create folders later
mIDfile = importdata('F:\Data\VOC2012\ids.txt');  
for i=1:2    %At present, there are only two pictures, so the length is 2.
    str1=strcat(str,strcat(mIDfile(i),'.bmp'));
    str1=char(str1)
    img = imread(str1);
    idx = unique(img);   %obtain UCM In the result graph pixel value A collection of
    maxL = length(idx);
     mkdir(['D:\Data\VOC2012\UCM\' ...
        char(mIDfile(i))]);
    for j=1:maxL-1    %Extraction and preservation of hierarchical results
        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

The results of 2007_000032 are as follows

In this case, the content of ids.txt is the number of two pictures.

For batch image generation ids, the following tips are provided
First, enter the dos window of windows and cd to the folder where the image is to be stored.
Then enter dir/s/on/b>F:/1.txt, which will save the path of the image to F:/1.txt, of course, the path of TXT can be set at will.

You can use Notepad++ to replace'F: Data VOC2012 test and'.bmp'with blank, leaving only the picture number.

Topics: MATLAB sudo Linux Ubuntu