[37] | 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 | % */
|
---|
| 39 | function []=gen_TextSup(sigm,k,minV,NuPick,SelectSegmentationPara,BatchNu); |
---|
| 40 | % this function generate superpixel using default parameter |
---|
| 41 | % but can also change to manually input parameter |
---|
| 42 | |
---|
| 43 | BatchNu |
---|
| 44 | % default parameter |
---|
| 45 | if nargin < 5 |
---|
| 46 | SelectSegmentationPara = 0; |
---|
| 47 | end |
---|
| 48 | |
---|
| 49 | % declaim global variable |
---|
| 50 | global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory... |
---|
| 51 | ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default... |
---|
| 52 | Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize; |
---|
| 53 | |
---|
| 54 | scale_sigm =[ 1 1.6]; |
---|
| 55 | scale_k = [ 1.6 3]; |
---|
| 56 | scale_minV = [ 1 3]; |
---|
| 57 | |
---|
| 58 | % generate superpixel of each image |
---|
| 59 | %====================will be fixed in the future |
---|
| 60 | %load([GeneralDataFolder '/Pick.mat']); |
---|
| 61 | Pick= [1 10 11; |
---|
| 62 | 1 2 5; |
---|
| 63 | 1 3 7; |
---|
| 64 | 10 14 17; |
---|
| 65 | 12 15 13; |
---|
| 66 | 10 10 11]; |
---|
| 67 | %Pick = [10 14 17]; |
---|
| 68 | %randpick = randperm(6) |
---|
| 69 | % ================================ |
---|
| 70 | |
---|
| 71 | NuPics = size(filename,2); |
---|
| 72 | % =============== |
---|
| 73 | BatchSize = 10 |
---|
| 74 | batchImg = 1:BatchSize:NuPics; |
---|
| 75 | % ============== |
---|
| 76 | %for i = 1:NuPics |
---|
| 77 | for i = batchImg(BatchNu):min(batchImg(BatchNu)+BatchSize-1, NuPics) |
---|
| 78 | i |
---|
| 79 | % load([ScratchDataFolder '/data/LowResImgIndexSuperpixelSep.mat']); |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | % sigm_new = |
---|
| 85 | % load image and process it to Hi Medi and Low Resolution |
---|
| 86 | Img = imread([GeneralDataFolder '/' ImgFolder '/' filename{i} '.jpg']); % Readin the high resolution image |
---|
| 87 | [VertYSizeHiREs HoriXSizeHiREs dummy]= size(Img);% find the dimension size of the Hi Resolution image |
---|
| 88 | clear dummy; |
---|
| 89 | % Loadin the GroundTruth data to know the depthMap size |
---|
| 90 | % depthfile = strrep(filename{i},'img','depth'); % the depth filename(without .file extension) associate with the *jpg file |
---|
| 91 | % load([GeneralDataFolder '/depthMap/' depthfile '.mat']); |
---|
| 92 | % [VertYSizeLowREs HoriXSizeLowREs]= size(depthMap);% find the dimension size of the depth data |
---|
| 93 | |
---|
| 94 | % in the new laser data we have scatter depthmap so use a |
---|
| 95 | % predecided LowRes |
---|
| 96 | VertYSizeLowREs = VertYNuDepth; |
---|
| 97 | HoriXSizeLowREs = HoriXNuDepth; |
---|
| 98 | |
---|
| 99 | % using a median size image to generate superpixel to reduce computation |
---|
| 100 | % intensity (the median size has a upper threshould SegVertYSize SegHoriXSize) |
---|
| 101 | if VertYSizeHiREs*HoriXSizeHiREs > SegVertYSize*SegHoriXSize |
---|
| 102 | Img = imresize(Img,[SegVertYSize SegHoriXSize ],'nearest'); % Downsample high resolution image to a median size image |
---|
| 103 | %======================================== |
---|
| 104 | H = calculateFilterBanks_old(Img); |
---|
| 105 | H = permute(H,[3 1 2]); |
---|
| 106 | H = H(:,:); |
---|
| 107 | % H = H - repmat(min(H,[],2),[1 size(H,2)]); |
---|
| 108 | H = abs(H); |
---|
| 109 | H = H./repmat(max(H,[],2),[1 size(H,2)]); |
---|
| 110 | |
---|
| 111 | %======================================= |
---|
| 112 | % imwrite(Img,[ScratchDataFolder '/ppm/' filename{i} '.ppm'],'ppm');% store median Resolution image to PPM format to feed in CMU C++ function |
---|
| 113 | else |
---|
| 114 | %======================================== |
---|
| 115 | H = calculateFilterBanks_old(Img); |
---|
| 116 | H = permute(H,[3 1 2]); |
---|
| 117 | H = H(:,:); |
---|
| 118 | % H = H - repmat(min(H,[],2),[1 size(H,2)]); |
---|
| 119 | H = abs(H); |
---|
| 120 | H = H./repmat(max(H,[],2),[1 size(H,2)]); |
---|
| 121 | %======================================= |
---|
| 122 | |
---|
| 123 | % imwrite(Img,[ScratchDataFolder '/ppm/' filename{i} '.ppm'],'ppm');% store median Resolution image to PPM format to feed in CMU C++ function |
---|
| 124 | end |
---|
| 125 | %============================ |
---|
| 126 | [VertYImg HoriXImg dummy]= size(Img); |
---|
| 127 | for m=1:NuPick |
---|
| 128 | Pick(m,:) |
---|
| 129 | Img=H(Pick(m,:),:); |
---|
| 130 | Img=permute(Img,[2 3 1]); |
---|
| 131 | Img = reshape(Img,VertYImg,[],3); |
---|
| 132 | figure(3); image(Img); |
---|
| 133 | imwrite(Img,[ScratchDataFolder '/ppm/' filename{i} '_Text'... |
---|
| 134 | '.ppm'],'ppm');% store median Resolution image to PPM format to feed in CMU C++ function |
---|
| 135 | %================================= |
---|
| 136 | % choose superpixel of the images |
---|
| 137 | % default segmentation parameter |
---|
| 138 | for j = 1:2% number of scale of superpixel |
---|
| 139 | |
---|
| 140 | ok = 0; % ok ==1 means accept the segmentation |
---|
| 141 | while 1 |
---|
| 142 | % call segment function writen in C++ from MIT |
---|
| 143 | system([LocalFolder '/../third_party/Superpixels/segment ' num2str(sigm*scale_sigm(j)) ' ' num2str(k*scale_k(j)) ... |
---|
| 144 | ' ' num2str(minV*scale_minV(j)) ' ' ScratchDataFolder '/ppm/' filename{i} '_Text.ppm' ' ' ... |
---|
| 145 | ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale_sigm(j)) '_' ... |
---|
| 146 | num2str(k*scale_k(j)) '_' num2str(minV*scale_minV(j))... |
---|
| 147 | '_' num2str(Pick(m,1)) '_' num2str(Pick(m,2)) '_' num2str(Pick(m,3)) '.ppm']); |
---|
| 148 | MediResImgSuperpixel = imread([ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale_sigm(j)) '_' num2str(k*scale_k(j)) '_' num2str(minV*scale_minV(j)) '_' num2str(Pick(m,1)) '_' num2str(Pick(m,2)) '_' num2str(Pick(m,3)) '.ppm']); % Readin the high resolution image |
---|
| 149 | figure(1); image(MediResImgSuperpixel); % show the superpixel in Medi Resolution |
---|
| 150 | |
---|
| 151 | % check if need to select segmentation parameter |
---|
| 152 | if SelectSegmentationPara==1; |
---|
| 153 | ok = input('Is the segmentation of image OK');% input new segmentation parameter |
---|
| 154 | else |
---|
| 155 | ok =1 ;% accept default segmentation parameter |
---|
| 156 | end |
---|
| 157 | |
---|
| 158 | % finish segmentation clean up the ppm folder. |
---|
| 159 | if ok==1; |
---|
| 160 | delete([ScratchDataFolder '/ppm/' filename{i} '_' num2str(sigm*scale_sigm(j)) '_' num2str(k*scale_k(j)) '_' num2str(minV*scale_minV(j)) '_' num2str(Pick(m,1)) '_' num2str(Pick(m,2)) '_' num2str(Pick(m,3)) '.ppm']); |
---|
| 161 | |
---|
| 162 | break; |
---|
| 163 | end |
---|
| 164 | sigm = input('type sigm of segmentation'); |
---|
| 165 | k = input('type k of segmentation'); |
---|
| 166 | minV = input('type minV of segmentation'); |
---|
| 167 | |
---|
| 168 | end |
---|
| 169 | |
---|
| 170 | % index superpixel |
---|
| 171 | [MediResImgIndexSuperpixelSepTemp dummy]= suprgb2ind(MediResImgSuperpixel); clear dummy; |
---|
| 172 | TextLowResImgIndexSuperpixelSepTemp = imresize(MediResImgIndexSuperpixelSepTemp,[VertYSizeLowREs HoriXSizeLowREs],'nearest'); %Downsample high resolution image to the same pixel size of predict Depth data |
---|
| 173 | |
---|
| 174 | % merage the superpixel according to diff segmentation |
---|
| 175 | %NuSup = size(unique(LowResImgIndexSuperpixelSep),1); |
---|
| 176 | % LowSup = LowResImgIndexSuperpixelSep{i,1}; |
---|
| 177 | % Sup = zeros(size(LowSup)); |
---|
| 178 | % for l = (unique(LowSup))' |
---|
| 179 | % masksup = LowSup == l; |
---|
| 180 | % Index = analysesupinpatch(TextLowResImgIndexSuperpixelSepTemp(masksup)); |
---|
| 181 | % Sup(masksup)= Index; |
---|
| 182 | % end |
---|
| 183 | |
---|
| 184 | if j ~= 3 |
---|
| 185 | TextLowResImgIndexSuperpixelSep{i,m,j} = TextLowResImgIndexSuperpixelSepTemp; |
---|
| 186 | else |
---|
| 187 | % merage all small point in higher scale segmentation |
---|
| 188 | % if j ~= 1 |
---|
| 189 | TextLowResImgIndexSuperpixelSep{i,m,j} = premergAllsuperpixel(TextLowResImgIndexSuperpixelSepTemp); |
---|
| 190 | end |
---|
| 191 | %if j == 1; |
---|
| 192 | % MediResImgIndexSuperpixelSep{i} = MediResImgIndexSuperpixelSepTemp; |
---|
| 193 | %end |
---|
| 194 | % refining superpixel |
---|
| 195 | % superpixel segmentation LowResImgSeperatedSuperpixel |
---|
| 196 | %LowResImgsuperpixel = imresize(MediResImgSuperpixel,[VertYSizeLowREs HoriXSizeLowREs],'nearest'); %Downsample high resolution image to the same pixel size of GroundTruth data |
---|
| 197 | %[LowResImgIndexSuperpixel LowResImgIndexSuperpixel_list]= suprgb2ind(LowResImgsuperpixel); |
---|
| 198 | |
---|
| 199 | % comment: cmu's superpixel might be connected. use premergsuperpixel to |
---|
| 200 | % deal with nonconnected superpixels and very small superpixels |
---|
| 201 | %[LowResImgIndexSuperpixelSepTemp]=premergsuperpixel(LowResImgIndexSuperpixel); % hard work 1minV |
---|
| 202 | |
---|
| 203 | % reorder the index number of the LowResImgIndexSuperpixelSep |
---|
| 204 | %[LowResImgIndexSuperpixelSep{i,j} LowResImgIndexSuperpixelSep_list]= ordersup(LowResImgIndexSuperpixelSepTemp); |
---|
| 205 | |
---|
| 206 | % show superpixel |
---|
| 207 | figure(2); |
---|
| 208 | imagesc(TextLowResImgIndexSuperpixelSep{i,m,j}); |
---|
| 209 | newmap = rand(max(max(TextLowResImgIndexSuperpixelSep{i,m,j})),3); |
---|
| 210 | colormap(newmap); |
---|
| 211 | |
---|
| 212 | % process the MediResImgSuperpixel to have the same number of |
---|
| 213 | % LowResImgIndexSuperpixelSep |
---|
| 214 | % if j==1 |
---|
| 215 | % tic |
---|
| 216 | % [MediResImgIndexSuperpixel dummy]= suprgb2ind(MediResImgSuperpixel); clear dummy; |
---|
| 217 | % MediResImgIndexSuperpixelSep = imresize(LowResImgIndexSuperpixelSep{i,1},size(MediResImgIndexSuperpixel),'nearest'); |
---|
| 218 | % NuSupMedi = max(max(MediResImgIndexSuperpixel)); |
---|
| 219 | % LowToMediResImgIndexSuperpixel = zeros(size(MediResImgIndexSuperpixel)); |
---|
| 220 | % for k = 1:NuSupMedi |
---|
| 221 | % mask = MediResImgIndexSuperpixel==k; |
---|
| 222 | % LowToMediResImgIndexSuperpixel(mask) = analysesupinpatch(MediResImgIndexSuperpixelSep(mask)); |
---|
| 223 | % % [list_sup] = analysesupinpatch(MediResImgIndexSuperpixelSep(mask)); |
---|
| 224 | % % [I C] = max(list_sup(2,:)); |
---|
| 225 | % % LowToMediResImgIndexSuperpixel(mask) = list_sup(1,C); |
---|
| 226 | % end |
---|
| 227 | % LowToMediResImgIndexSuperpixelSep{i} =... |
---|
| 228 | % premergAllsuperpixel(LowToMediResImgIndexSuperpixel); |
---|
| 229 | % toc |
---|
| 230 | % end |
---|
| 231 | end |
---|
| 232 | end |
---|
| 233 | delete([ScratchDataFolder '/ppm/' filename{i} '_Text.ppm']); |
---|
| 234 | % save([ScratchDataFolder '/data/TextLowResImgIndexSuperpixelSepi' num2str(BatchNu) '.mat'], 'TextLowResImgIndexSuperpixelSep'); |
---|
| 235 | end |
---|
| 236 | |
---|
| 237 | % save result for later application |
---|
| 238 | save([ScratchDataFolder '/data/TextLowResImgIndexSuperpixelSepi' num2str(BatchNu) '.mat'], 'TextLowResImgIndexSuperpixelSep'); |
---|
| 239 | % save([ScratchDataFolder '/data/TextLowResImgIndexSuperpixelSep.mat'], 'TextLowResImgIndexSuperpixelSep'); |
---|
| 240 | %save([ScratchDataFolder '/data/MediResImgIndexSuperpixelSep.mat'], 'MediResImgIndexSuperpixelSep'); |
---|
| 241 | |
---|
| 242 | return; |
---|