source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Features/OldBatchVersion/gen_Sup_new.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 11.6 KB
Line 
1% *  This code was used in the following articles:
2% *  [1] Learning 3-D Scene Structure from a Single Still Image,
3% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
4% *      In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
5% *      (best paper)
6% *  [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
7% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
8% *      In ICCV workshop on Virtual Representations and Modeling
9% *      of Large-scale environments (VRML), 2007.
10% *  [3] 3-D Depth Reconstruction from a Single Still Image,
11% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
12% *      International Journal of Computer Vision (IJCV), Aug 2007.
13% *  [6] Learning Depth from Single Monocular Images,
14% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
15% *      In Neural Information Processing Systems (NIPS) 18, 2005.
16% *
17% *  These articles are available at:
18% *  http://make3d.stanford.edu/publications
19% *
20% *  We request that you cite the papers [1], [3] and [6] in any of
21% *  your reports that uses this code.
22% *  Further, if you use the code in image3dstiching/ (multiple image version),
23% *  then please cite [2].
24% * 
25% *  If you use the code in third_party/, then PLEASE CITE and follow the
26% *  LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
27% *
28% *  Finally, this code is for non-commercial use only.  For further
29% *  information and to obtain a copy of the license, see
30% *
31% *  http://make3d.stanford.edu/publications/code
32% *
33% *  Also, the software distributed under the License is distributed on an
34% * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
35% *  express or implied.   See the License for the specific language governing
36% *  permissions and limitations under the License.
37% *
38% */
39function []=gen_Sup_new(sigm,k,min,SelectSegmentationPara);
40% this function generate superpixel using default parameter
41% but can also change to manually input parameter
42
43%%% Jeff's Comments
44% The global variable filename specifies a vector of names.  For
45% each file specified, read in the jpg and write out a ppm of size
46% SegVertYSize x SegHoriXSize.  Send that image to the CMU
47% segmentation program with params 0.8*[sigm, k, min].  If
48% SelectSegmenationPara is true, then display the results and ask
49% the user to enter new sigm, k, and min; repeat until user is happy.
50%
51% Resize both the image and the CMU output to PopUpVertY x
52% PopUpHoriX and write them to the scratch/ppm folder.
53%
54% Take the unresized CMU output and call suprgb2ind, converting it
55% from an image to a matrix of superpixel indicies.  save this
56% index image to scratch/data/MedSeg/ and a low res (VertYNuDepth x
57% HoriXNuDepth) version to scratch/data/.
58%%%%
59
60
61% default parameter
62if nargin < 4
63    SelectSegmentationPara = 0; % if SelectSegmentationPara == 1, enable the parameter interation with user.
64end
65
66% declaim global variable
67global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
68    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
69    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize PopUpVertY PopUpHoriX;
70
71scale =[0.8 1.6 5]; % use different scale to generate small(0.8) middle(1.6) 5(large) scale of superpixel
72
73% generate superpixel of each image
74
75NuPics = size(filename,2);
76for i = 1:NuPics
77%for i = 1:10
78%    i 
79    for j = 1:1%3% number of scale of superpixel
80
81       
82%         sigm_new =
83        % load image and process it to Hi Medi and Low Resolution
84        Img = imread([GeneralDataFolder '/' ImgFolder '/' filename{i} '.jpg']); % Readin the high resolution image
85        [VertYSizeHiREs HoriXSizeHiREs dummy]= size(Img);% find the dimension size of the Hi Resolution image
86        clear dummy;
87        % Loadin the GroundTruth data to know the depthMap size
88%         depthfile = strrep(filename{i},'img','depth'); % the depth filename(without .file extension) associate with the *jpg file
89%         load([GeneralDataFolder '/depthMap/' depthfile '.mat']);
90%         [VertYSizeLowREs HoriXSizeLowREs]= size(depthMap);% find the dimension size of the depth data
91        % in the new laser data we have scatter depthmap so use a
92        % predecided LowRes
93        VertYSizeLowREs = VertYNuDepth;
94        HoriXSizeLowREs = HoriXNuDepth;
95       
96        % using a fixed range of median size image [SegVertYSize SegHoriXSize ]
97        %  to generate superpixel to reduce computation
98        if VertYSizeHiREs*HoriXSizeHiREs > SegVertYSize*SegHoriXSize
99%             Ratio = [VertYSizeHiREs HoriXSizeHiREs]./[SegVertYSize SegHoriXSize]
100%              Ratio = [1 1]
101%             [VertYSizeHiREs HoriXSizeHiREs ]./floor(Ratio)
102%             pause
103            Img = imresize(Img,[SegVertYSize+1 SegHoriXSize+1 ],'nearest'); % Downsample high resolution image to a fixed median size image
104            %Img = imresize(Img,[VertYSizeHiREs HoriXSizeHiREs ]./floor(Ratio),'nearest'); % Downsample high resolution image to a range of median size image
105            imwrite(Img,[ScratchDataFolder '/ppm/' filename{i} '.ppm'],'ppm');% store median Resolution image to PPM format to feed in CMU C++ function
106        else
107            imwrite(Img,[ScratchDataFolder '/ppm/' filename{i} '.ppm'],'ppm');% store median Resolution image to PPM format to feed in CMU C++ function
108        end   
109       
110        % choose superpixel of the images
111        % default segmentation parameter
112        ok = 0; % ok ==1 means accept the segmentation
113        while 1
114            % call segment function writen in C++ from MIT
115            disp([LocalFolder '/../third_party/Superpixels/segment ' num2str(sigm*scale(j)) ' ' num2str(k*scale(j)) ...
116                ' ' num2str(min*scale(j)) ' ' ScratchDataFolder '/ppm/' filename{i} '.ppm' ' ' ...
117                ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale(j)) '_' ...
118                num2str(k*scale(j)) '_' num2str(min*scale(j)) '.ppm']);
119            system([LocalFolder '/../third_party/Superpixels/segment ' num2str(sigm*scale(j)) ' ' num2str(k*scale(j)) ...
120                ' ' num2str(min*scale(j)) ' ' ScratchDataFolder '/ppm/' filename{i} '.ppm' ' ' ...
121                ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale(j)) '_' ...
122                num2str(k*scale(j)) '_' num2str(min*scale(j)) '.ppm']);
123%            system([LocalFolder '/../third_party/Superpixels/segment ' num2str(sigm*scale(j)) ' ' num2str(k*scale(j)) ...
124%                ' ' num2str(min*scale(j)) ' ' ScratchDataFolder '/ppm/' filename{i} '.ppm' ' ' ...
125%                ScratchDataFolder '/ppm/' filename{i} '.ppm']);
126
127            MediResImgSuperpixel = imread([ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale(j)) '_' num2str(k*scale(j)) '_' num2str(min*scale(j)) '.ppm']); % Readin the high resolution image
128            MediResImgSuperpixel = MediResImgSuperpixel(1:(end-1),1:(end-1),1:3);
129            figure(1); image(MediResImgSuperpixel); % show the superpixel in Medi Resolution
130                imwrite(MediResImgSuperpixel,[ScratchDataFolder '/ppm/' 'test' int2str(i) '.jpg'],'jpg');
131   
132            % check if need to select segmentation parameter
133            if SelectSegmentationPara==1;
134                ok = input('Is the segmentation of image OK');% input new segmentation parameter
135            else   
136                ok =1 ;% accept default segmentation parameter
137            end
138   
139            % finish segmentation clean up the ppm folder.
140            if ok==1;
141                delete([ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale(j)) '_' num2str(k*scale(j)) '_' num2str(min*scale(j)) '.ppm']);
142%                delete([ScratchDataFolder '/ppm/' filename{i} '.ppm']);
143               
144                % generating [PopUpVertY, PopUpHoriX] (default 800 x 600)
145                % .ppm superpixel and image for photopopup software.
146                newfilename = strrep(filename{i},'.','')
147                SupPopup = imresize(MediResImgSuperpixel,[PopUpVertY, PopUpHoriX],'nearest');
148                imwrite(SupPopup,[ScratchDataFolder '/ppm/' newfilename '.ppm'],'ppm');
149                ImgPopup = imresize(Img,[PopUpVertY, PopUpHoriX],'nearest');
150                imwrite(ImgPopup,[ScratchDataFolder '/ppm/' newfilename '.jpg'],'jpg');
151%            return;   
152                break;
153            end
154            sigm = input('type sigm of segmentation');
155            k = input('type k of segmentation');
156            min = input('type min of segmentation');
157           
158        end
159% ============used to test no residual superpixel================
160%    end;
161%end;   
162%return;
163% ===============================================================
164
165        % index superpixel
166        [MediResImgIndexSuperpixelSepTemp dummy]= suprgb2ind(MediResImgSuperpixel); clear dummy;
167        LowResImgIndexSuperpixelSep{i,j} = imresize(MediResImgIndexSuperpixelSepTemp,...
168            [VertYSizeLowREs HoriXSizeLowREs],'nearest'); %Downsample to size size as prediected depth map
169       
170        % merage all small point in higher scale segmentation
171        if j ~= 1
172           LowResImgIndexSuperpixelSep{i,j} = premergAllsuperpixel(LowResImgIndexSuperpixelSep{i,j});
173        end
174        if j == 1;
175           MediResImgIndexSuperpixelSep = MediResImgIndexSuperpixelSepTemp;
176        end
177        % refining superpixel
178        % superpixel segmentation LowResImgSeperatedSuperpixel
179        %LowResImgsuperpixel = imresize(MediResImgSuperpixel,[VertYSizeLowREs HoriXSizeLowREs],'nearest'); %Downsample high resolution image to the same pixel size of GroundTruth data
180        %[LowResImgIndexSuperpixel  LowResImgIndexSuperpixel_list]= suprgb2ind(LowResImgsuperpixel);
181
182        % comment: cmu's superpixel might be connected. use premergsuperpixel to
183        % deal with nonconnected superpixels and very small superpixels
184        %[LowResImgIndexSuperpixelSepTemp]=premergsuperpixel(LowResImgIndexSuperpixel); % hard work 1min
185       
186        % reorder the index number of the LowResImgIndexSuperpixelSep
187        %[LowResImgIndexSuperpixelSep{i,j}  LowResImgIndexSuperpixelSep_list]= ordersup(LowResImgIndexSuperpixelSepTemp);
188       
189        % show superpixel
190        figure(2);
191        imagesc(LowResImgIndexSuperpixelSep{i,j});
192        newmap = rand(max(max(LowResImgIndexSuperpixelSep{i,j})),3);
193        colormap(newmap);
194       
195        % process the MediResImgSuperpixel to have the same number of
196        % LowResImgIndexSuperpixelSep
197%         if j==1
198%             tic
199%             [MediResImgIndexSuperpixel dummy]= suprgb2ind(MediResImgSuperpixel); clear dummy;
200%             MediResImgIndexSuperpixelSep = imresize(LowResImgIndexSuperpixelSep{i,1},size(MediResImgIndexSuperpixel),'nearest');
201%             NuSupMedi = max(max(MediResImgIndexSuperpixel));
202%             LowToMediResImgIndexSuperpixel = zeros(size(MediResImgIndexSuperpixel));
203%             for k = 1:NuSupMedi
204%                 mask = MediResImgIndexSuperpixel==k;
205%                 LowToMediResImgIndexSuperpixel(mask) = analysesupinpatch(MediResImgIndexSuperpixelSep(mask));
206% %                 [list_sup] = analysesupinpatch(MediResImgIndexSuperpixelSep(mask));
207% %                 [I C] = max(list_sup(2,:));
208% %                 LowToMediResImgIndexSuperpixel(mask) = list_sup(1,C);
209%             end
210%             LowToMediResImgIndexSuperpixelSep{i} =...
211%             premergAllsuperpixel(LowToMediResImgIndexSuperpixel);
212%             toc
213%         end   
214% ===============
215    end   
216save([ScratchDataFolder '/data/MedSeg/MediResImgIndexSuperpixelSep' num2str(i) '.mat'], 'MediResImgIndexSuperpixelSep');
217end   
218% =============
219
220% save result for later application
221save([ScratchDataFolder '/data/LowResImgIndexSuperpixelSep.mat'], 'LowResImgIndexSuperpixelSep');
222
223return;
Note: See TracBrowser for help on using the repository browser.