benzlxs commited on
Commit
d4035c1
·
verified ·
1 Parent(s): cf080e3

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +0 -0
  2. SUN_source_code_v1/README.txt +54 -0
  3. SUN_source_code_v1/code/GeometricContext_dhoiem/.DS_Store +0 -0
  4. SUN_source_code_v1/code/GeometricContext_dhoiem/LICENSE.txt +15 -0
  5. SUN_source_code_v1/code/GeometricContext_dhoiem/README +127 -0
  6. SUN_source_code_v1/code/GeometricContext_dhoiem/data/classifiers_08_22_2005.mat +3 -0
  7. SUN_source_code_v1/code/GeometricContext_dhoiem/data/ijcvClassifier.mat +3 -0
  8. SUN_source_code_v1/code/GeometricContext_dhoiem/data/ijcvClassifier_indoor.mat +3 -0
  9. SUN_source_code_v1/code/GeometricContext_dhoiem/src/.DS_Store +0 -0
  10. SUN_source_code_v1/code/GeometricContext_dhoiem/src/APPtestDirectory.m +88 -0
  11. SUN_source_code_v1/code/GeometricContext_dhoiem/src/APPtestImage.m +138 -0
  12. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/test_boosted_dt_mc.m +42 -0
  13. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/test_boosted_kde_2c.m +12 -0
  14. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_dt_2c.m +90 -0
  15. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_dt_mc.m +154 -0
  16. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_kde_2c.m +112 -0
  17. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_stubs_mc.m +154 -0
  18. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/tree_getNewVersion.m +8 -0
  19. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/tree_getParameters.m +26 -0
  20. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.c +133 -0
  21. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.mexa64 +0 -0
  22. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.mexglx +0 -0
  23. SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.mexmaci +0 -0
  24. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPclassifierOutput2confidenceImages.m +56 -0
  25. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPestimateHorizon.m +79 -0
  26. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPestimateVp.m +228 -0
  27. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetImageFilters.m +29 -0
  28. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLabeledImage.m +120 -0
  29. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLabeledImage2.m +106 -0
  30. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLabeledImage2.m~ +96 -0
  31. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLargeConnectedEdges.m +173 -0
  32. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetPairwiseSuperpixelLikelihoods.m +20 -0
  33. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetRegionFeatures.m +204 -0
  34. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSegmentInds.m +8 -0
  35. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpData.m +106 -0
  36. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpInds.m +8 -0
  37. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpIndsOld.m +33 -0
  38. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpMeans.m +24 -0
  39. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpStats.m +78 -0
  40. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetVpFeatures.m +21 -0
  41. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPimages2superpixels.m +44 -0
  42. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPlines2vpFeatures.m +120 -0
  43. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPsp2regions.m +114 -0
  44. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPspInds2regionInds.m +23 -0
  45. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPtestImage.m~ +133 -0
  46. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPvp2horizon.m +112 -0
  47. SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPvp2regionFeatures.m +217 -0
  48. SUN_source_code_v1/code/GeometricContext_dhoiem/src/ijcv06/hsWheel.m +21 -0
  49. SUN_source_code_v1/code/GeometricContext_dhoiem/src/ijcv06/ijcvColorFigureScript.m +70 -0
  50. SUN_source_code_v1/code/GeometricContext_dhoiem/src/ijcv06/ijcvColorFigureScript.m~ +70 -0
.gitattributes CHANGED
The diff for this file is too large to render. See raw diff
 
SUN_source_code_v1/README.txt ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ This software is available for only non-commercial use. See the attached license in LICENSE.txt under code/scene_sun folder.
2
+
3
+ This is a post-CVPR version of the code and may produce slightly different (hopefully better) results than the performance reported in the following paper:
4
+
5
+ J. Xiao, J. Hays, K. Ehinger, A. Oliva, and A. Torralba
6
+ SUN Database: Large-scale Scene Recognition from Abbey to Zoo
7
+ Proceedings of 23rd IEEE Conference on Computer Vision and Pattern Recognition (CVPR2010)
8
+
9
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10
+
11
+ USAGE
12
+
13
+ There are two top level folders in this zip file: code and data. All source code goes to code folder, and all data goes to code folder. Our source code is located under the folder code/scene_sun. To ease the deployment of the software, we also include the libraries from other research groups in this package under different folders under code folder. Please read their respective compilation and installation instructions as well as software license in their respective folders. Folder data/vocabulary contain k-means clustered codebook for bag-of-word features. We don't provide codes to generate this codebook but it should be straightforward to compute it using our feature extraction code and k-mean in MATLAB toolbox. Although it is possible to put image data and intermediate results in a separate folder, we recommend you to put them inside data/dataset_name .
14
+
15
+ Step 1: Download and uncompress the image data to the folder data/scene_15class/image or data/scene_397class/image depends on which database you use.
16
+ You can download the 15 scene categories dataset from:
17
+ http://www-cvr.ai.uiuc.edu/ponce_grp/data/index.html#scenes
18
+ and the SUN 397 class dataset from
19
+ http://groups.csail.mit.edu/vision/SUN/
20
+ In the folder data/scene_15class and data/scene_397class, we have contained MAT file split10.mat for random training and testng data split that we used for the paper.
21
+
22
+ Step 2: You need to change the folder path in compute_feature.m and run_kernel_svm.m to select which database for you to use.
23
+
24
+ Step 3: Run compute_feature.m to compute the image features. You can use more than one MATLAB sessions to run this scripts at the same time parallelly.
25
+
26
+ Step 4: Run run_kernel_svm.m to train SVM and do evaluation.
27
+
28
+ Notice: This code is not optimized for speed. It may takes a long time to compute the result. Please use a fast computer and be patient. We suggest you run the code on the 15-scene dataset for debugging purpose to make sure this code works correctly first, before you run on the SUN 397 database. For the 15 scene database, if you run the code correctly, you should be able to get the following result report for the combined kernel approach.
29
+ combine kernel accuracy (normalized) = 88.4 <- this is slightly better than what we report in the CVPR paper.
30
+ combine kernel accuracy (unnormalized) = 79.2
31
+
32
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33
+
34
+ ENVIRONMENT
35
+
36
+ We only tested our code in CSAIL Debian Linux on 64bit Intel Machine with 32GB memory, and Matlab Version 7.7.0.471 (R2008b).
37
+ But it should be quite straightforward to port the code into Windows or Mac.
38
+
39
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
+
41
+ BUG REPORT
42
+
43
+ Please email to jxiao@csail.mit.edu for reporting any bug. We can only provide minimal support for academic researchers only.
44
+
45
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
+
47
+ REFERENCES
48
+
49
+ This code implements the following papers. Note that the implementation may not be exact.
50
+ Please cite this paper if you use the code in your research.
51
+
52
+ J. Xiao, J. Hays, K. Ehinger, A. Oliva, and A. Torralba
53
+ SUN Database: Large-scale Scene Recognition from Abbey to Zoo
54
+ Proceedings of 23rd IEEE Conference on Computer Vision and Pattern Recognition (CVPR2010)
SUN_source_code_v1/code/GeometricContext_dhoiem/.DS_Store ADDED
Binary file (12.3 kB). View file
 
SUN_source_code_v1/code/GeometricContext_dhoiem/LICENSE.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ License for Non-Commercial Use
2
+
3
+ If this software is redistributed, this license must be included. The term software includes any
4
+ source files, documentation, executables, models, and data.
5
+
6
+ This software is available for general use by academic or non-profit, or government-sponsored
7
+ researchers. It may also be used for evaluation purposes elsewhere. This license does not grant
8
+ the right to use this software or any derivation of it in a for-profit enterprise. For
9
+ commercial use, please contact the Tech Transfer Office at Carnegie Mellon University.
10
+
11
+ This software comes with no warranty or guarantee of any kind. By using this software, the user
12
+ accepts full liability.
13
+
14
+ This license is written by Derek Hoiem (C) 2010.
15
+
SUN_source_code_v1/code/GeometricContext_dhoiem/README ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
+ SOURCE README FOR AUTOMATIC PHOTO POPUP AND GEOMETRIC CONTEXT
3
+ Derek Hoiem (dhoiem@cs.cmu.edu)
4
+ 01/08/2010
5
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6
+
7
+
8
+ LICENSE
9
+
10
+ Copyright (C) 2007 Derek Hoiem, Carnegie Mellon University
11
+
12
+ This software is available for only non-commercial use. See the attached
13
+ license in LICENSE.txt.
14
+
15
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
+
18
+
19
+ REFERENCES
20
+
21
+ This code implements the following papers. Note that the implementation may not be exact.
22
+ Please cite one or more of these papers, depending on the use.
23
+
24
+ D. Hoiem, A.A. Efros, and M. Hebert, "Geometric Context from a Single Image", ICCV 2005.
25
+
26
+ D. Hoiem, A.A. Efros, and M. Hebert, "Recovering Surface Layout from an Image", IJCV, Vol. 75, No. 1,
27
+ October 2007.
28
+
29
+ D. Hoiem, A.A. Efros, and M. Hebert, "Automatic Photo Pop-up", ACM SIGGRAPH 2005.
30
+
31
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
32
+
33
+
34
+ Note about VERSIONS
35
+
36
+ This contains two versions of the "Automatic Photo Pop-up" and "Geometric
37
+ "Context" code. The first version is from the SIGGRAPH 2005 / ICCV 2005 papers
38
+ and involves the functions photoPopup and APPtestImage. The second version
39
+ is from the IJCV 2007 paper and involves the functions photoPopupIjcv and
40
+ ijcvTestImage. Note that these implementations may not be exact. In particular,
41
+ some changes to features were made to improve speed, which may have small effects
42
+ on accuracy. Further, the multiple segmentation algorithm is random, so different
43
+ runs will not produce identical results.
44
+
45
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
+
47
+
48
+ How to RUN:
49
+
50
+
51
+ First, appropriately replace the path for the segment command in im2superpixels.m.
52
+
53
+ Geometric Context version 1 (ICCV 2005):
54
+ [labels, conf_map, maps, pmaps] = APPtestImage(image, imsegs, vClassifier, hClassifier, segDensity)
55
+ image should be a double color image
56
+ imsegs is the superpixel structure (if empty, will be computed)
57
+ The remaining three arguments are stored in data/classifiers_08_22_2005.mat
58
+
59
+ [labels, conf_map, imsegs] = ...
60
+ APPtestDirectory(segDensity, vClassifier, hClassifier, imdir, imfn, varargin)
61
+ Processes a directory of images with given filenames. Optional last argument is where to
62
+ store displayed results.
63
+
64
+
65
+ Geometric Context version 2 (IJCV 2007):
66
+ [pg, data, imsegs] = ijcvTestImage(im, imsegs, classifiers, smaps, spdata, adjlist, edata);
67
+ The only required fields are im and classifiers.
68
+ Example usage:
69
+ load '../data/ijcvClassifier.mat'
70
+
71
+
72
+ classifiers=load('../data/ijcvClassifier_indoor.mat');
73
+ [pg, data, imsegs] = ijcvTestImage(im, [], classifiers);
74
+ [cimages, cnames] = pg2confidenceImages(imsegs, {pg});
75
+ % only keep ground pourous sky vertical [1 5 7 8]
76
+ geom_c_map = cimages{1}(:,:,[1 5 7 8]);
77
+
78
+
79
+
80
+ Classifiers trained on indoor and outdoor images are provided in the data directory.
81
+
82
+ [pg, smaps, imsegs] = ijcvTestImageList(fn, imsegs, classifiers, laboutdir, confoutdir)
83
+ Processes all images whose names are given in fn. As above, imsegs can be empty.
84
+
85
+
86
+ Automatic Photo Pop-up (SIGGRAPH 2005, IJCV 2007)
87
+ photoPopup(fnData, fnImage, extSuperpixelImage, outdir)
88
+ This is the 2005 version.
89
+ fnData is the filename containing the classifiers (data/classifiers_08_22_2005.mat).
90
+ If extSuperpixelImage is empty, it will be computed.
91
+
92
+ photoPopupIjcv(fnData, fnImage, extSuperpixelImage, outdir)
93
+ This is the IJCV 2007 version. See notes above.
94
+
95
+
96
+ Training:
97
+ For the IJCV version, see src/ijcv06/ijcvMultiSegScript.m
98
+ This function performs both training and evaluation. It requires some edits. It is currently
99
+ setup for cross-validation, but it should be straightforward to use separate train and test sets.
100
+ The Geometric Context Dataset (separate download) contains rand_indices which specifies the
101
+ train/test splits.
102
+
103
+
104
+ Other useful functions:
105
+ src/util/splitpg.m
106
+ src/util/pg2confidenceImages.m
107
+ src/util/pg2*
108
+ src/util/write*
109
+
110
+
111
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
+
114
+ NOTES:
115
+
116
+ Superpixels:
117
+ I use the segmentation code provided by Felzenszwalb and Huttenlocher
118
+ at people.cs.uchicago.edu/~pff/segment/ to create the superpixels in my
119
+ experiments. The first three arguments (sigma, k, min) that I use are
120
+ 0.8 100 100. I've included a pl script for segmenting a directory
121
+ that you may find useful. You can also use a different program to create
122
+ the superpixel image. That image should have a different RGB color for
123
+ each segment without drawn boundaries between segments.
124
+
125
+
126
+ (C) Derek Hoiem, Carnegie Mellon University, 2007
127
+
SUN_source_code_v1/code/GeometricContext_dhoiem/data/classifiers_08_22_2005.mat ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4e16bc17ce09bb357c0ffd8c4c47f90b31a5091e03595dbab5287ea222185649
3
+ size 231989
SUN_source_code_v1/code/GeometricContext_dhoiem/data/ijcvClassifier.mat ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ba29f94013c150396eedb80a5e0db656a96f754ca82fbbde02995979882d93b
3
+ size 460724
SUN_source_code_v1/code/GeometricContext_dhoiem/data/ijcvClassifier_indoor.mat ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3300ce7eba2e4bcd2e732babb3f139eb5d6c2e023c70c41a4cb9e2dc193a9ea4
3
+ size 198126
SUN_source_code_v1/code/GeometricContext_dhoiem/src/.DS_Store ADDED
Binary file (15.4 kB). View file
 
SUN_source_code_v1/code/GeometricContext_dhoiem/src/APPtestDirectory.m ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [labels, conf_map, imsegs] = ...
2
+ APPtestDirectory(segDensity, vClassifier, hClassifier, ...
3
+ imdir, imfn, varargin)
4
+ % [labels, conf_map] = APPtestDirectory(segDensity, vClassifier,
5
+ % hClassifier, imdir, imsegs, varargin)
6
+ %
7
+ % Gets the geometry for each image with superpixels given by imsegs.
8
+ %
9
+ % Input:
10
+ % segDensity: structure giving probability of 2 sp having same label
11
+ % vClassifier: segment classifier for ground/vert/sky
12
+ % hClassifier: segment classifier for subclassses of vert
13
+ % imdir: the image directory
14
+ % imfn: the image filenames
15
+ % varargin{1} (optional): the output directory for displaying results
16
+ % Output:
17
+ % labels: structure containing labeling results for each image
18
+ % conf_map: the likelihoods for each sp for each class for each image
19
+ %
20
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
21
+ % Current Version: 1.0 09/30/2005
22
+
23
+ DO_PARALLEL = 0; % for running multiple parallel processes on directory
24
+
25
+ if length(varargin) > 0
26
+ outdir = varargin{1};
27
+ end
28
+
29
+ for f = 1:length(imfn)
30
+
31
+ fn = imfn{f};
32
+ bn = strtok(fn, '.');
33
+
34
+ if ~DO_PARALLEL || ~exist(outdir) || ~exist([outdir '/' bn '.c.mat'])
35
+
36
+ if ~exist(outdir, 'file')
37
+ mkdir(outdir);
38
+ end
39
+
40
+ if DO_PARALLEL % to mark as being processed
41
+ touch([outdir '/' bn '.c.mat']);
42
+ end
43
+
44
+ image = im2double(imread([imdir, '/', fn]));
45
+
46
+ tmp = im2superpixels(image);
47
+ tmp.imname = fn;
48
+ imsegs(f) = tmp;
49
+
50
+ if size(image, 3) == 3
51
+
52
+ disp(['processing image ' fn]);
53
+
54
+ [labels(f), conf_map(f), maps{f}, pmaps(f)] = ...
55
+ APPtestImage(image, imsegs(f), vClassifier, hClassifier, segDensity);
56
+ % for generating context
57
+ [cimages, cnames] = APPclassifierOutput2confidenceImages(imsegs(f), conf_map(f));
58
+
59
+ if length(varargin) > 0
60
+ outdir = varargin{1};
61
+
62
+ limage = APPgetLabeledImage(image, imsegs(f), labels(f).vert_labels, labels(f).vert_conf, ...
63
+ labels(f).horz_labels, labels(f).horz_conf);
64
+ imwrite(limage, [outdir, '/', bn, '.l.jpg']);
65
+ imwrite(image, [outdir, '/', fn]);
66
+
67
+ % for generating context
68
+ glabels = labels(f);
69
+ gconf_map = conf_map(f);
70
+ gmaps = maps{f};
71
+ gpmaps = pmaps(f);
72
+ save([outdir '/' bn '.c.mat'], 'glabels', 'gconf_map', 'cimages', 'cnames', 'gmaps', 'gpmaps', 'imsegs');
73
+
74
+ % make a vrml file
75
+ APPwriteVrmlModel(imdir, imsegs(f), labels(f), outdir);
76
+ end
77
+
78
+ end
79
+
80
+ drawnow;
81
+
82
+ end
83
+
84
+ end
85
+
86
+ for f = 1:length(imfn)
87
+ labels(f).imname = imsegs(f).imname;
88
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/APPtestImage.m ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [labels, conf_map, maps, pmaps] = APPtestImage(image, imsegs, ...
2
+ vClassifier, hClassifier, segDensity)
3
+ % [labels, conf_map, maps, pmaps] = APPtestImage(image, imsegs,
4
+ % vClassifier, hClassifier, segDensity)
5
+ %
6
+ % Gets the geometry for a single image.
7
+ %
8
+ % Input:
9
+ % image: the image to process
10
+ % imsegs: the sp structure
11
+ % vClassifier: region classifier for main classes
12
+ % hClassifier: region classifier for vertical subclasses
13
+ % segDensity: density for superpixel clustering
14
+ % Output:
15
+ % labels: structure containing labeling results for each image
16
+ % conf_map: the likelihoods for each sp for each class for each image
17
+ % maps: the segmentation region maps (from superpixels to regions)
18
+ % pmaps: probabilities for each region
19
+ %
20
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
21
+ % Current Version: 1.0 09/30/2005
22
+
23
+
24
+ % mix class must be the last class in each classifier
25
+
26
+ if isempty(imsegs)
27
+ imsegs = im2superpixels(image);
28
+ end
29
+
30
+ %exclude mix class
31
+ nvclasses = length(vClassifier.names)-1;
32
+ nhclasses = length(hClassifier.names)-1;
33
+
34
+ [doog_filters, texton_data] = APPgetImageFilters;
35
+
36
+ imsegs.seginds = APPgetSpInds(imsegs);
37
+
38
+ % compute features
39
+ spdata = APPgetSpData(image, doog_filters, texton_data.tim, imsegs);
40
+ [spdata.hue, spdata.sat, tmp] = rgb2hsv(image);
41
+
42
+
43
+ imsize = size(image);
44
+ minEdgeLen = sqrt(imsize(1)^2+imsize(2)^2)*0.02;
45
+ [vpdata.lines, vpdata.spinfo] = ...
46
+ APPgetLargeConnectedEdges(rgb2gray(image), minEdgeLen, imsegs);
47
+ [vpdata.v, vpdata.vars, vpdata.p, vpdata.hpos] = ...
48
+ APPestimateVp(vpdata.lines, imsize(1:2), 0);
49
+
50
+ % create multiple partitions
51
+ num_partitions = [3 4 5 7 9 11 15 20 25];
52
+ num_maps = length(num_partitions);
53
+ maps = APPsp2regions(segDensity, spdata, num_partitions);
54
+
55
+ nsegs = imsegs.nseg;
56
+ pV = zeros(nsegs, nvclasses);
57
+ pH = zeros(nsegs, nhclasses);
58
+
59
+ % compute the probability of each block having each possible label
60
+ for m = 1:num_maps
61
+
62
+ currMap = maps(:, m);
63
+
64
+ %cluster_image = get_cluster_overlay_s2(rgb2gray(image), currMap, imsegs.segimage);
65
+ %imwrite(cluster_image, ['../results/tmp/alley09.c.' num2str(max(currMap)) '.jpg'], 'Quality', 80);
66
+
67
+ currNSegments = max(currMap(:));
68
+
69
+ regionFeatures = APPgetRegionFeatures(image, imsegs, currMap, (1:currNSegments), spdata, vpdata);
70
+
71
+ nregions = size(regionFeatures, 1);
72
+
73
+ % get probability of vertical classes P(y|x) for each region
74
+ tmpV = test_boosted_dt_mc(vClassifier, regionFeatures);
75
+ tmpV = 1 ./ (1+exp(-tmpV));
76
+
77
+ % get probability of horizontal classes P(y|x) for each region
78
+ tmpH = test_boosted_dt_mc(hClassifier, regionFeatures);
79
+ tmpH = 1 ./ (1+exp(-tmpH));
80
+
81
+ % normalize probabilities so that each is P(label|~mixed)P(~mixed)
82
+ % normalize probabilities and sum over maps
83
+ for r = 1:nregions
84
+ indices = find(currMap == r);
85
+ tmpV(r, 1:end-1) = tmpV(r, 1:end-1) / sum(tmpV(r, 1:end-1));
86
+ tmpV(r, 1:end-1) = tmpV(r, 1:end-1) * (1-tmpV(r, end));
87
+ for c = 1:nvclasses
88
+ pV(indices, c) = pV(indices, c) + tmpV(r, c);%*pYv(c, r);
89
+ end
90
+ tmpH(r, 1:end-1) = tmpH(r, 1:end-1) / sum(tmpH(r, 1:end-1));
91
+ tmpH(r, 1:end-1) = tmpH(r, 1:end-1) * (1-tmpH(r, end));
92
+ for c = 1:nhclasses
93
+ pH(indices, c) = pH(indices, c) + tmpH(r, c);%*pYh(c, r);
94
+ end
95
+
96
+ end
97
+
98
+ pmaps.v{m} = tmpV;
99
+ pmaps.h{m} = tmpH;
100
+
101
+ end
102
+
103
+ % re-normalize weighted vote from classifiers
104
+ for s = 1:size(pV, 1)
105
+ pV(s, :) = pV(s, :) / sum(pV(s, :));
106
+ end
107
+ for s = 1:size(pH, 1)
108
+ pH(s, :) = pH(s, :) / sum(pH(s, :));
109
+ end
110
+
111
+ conf_map.vmap = pV;
112
+ conf_map.hmap = pH;
113
+ conf_map.vnames = vClassifier.names(1:end-1);
114
+ conf_map.hnames = hClassifier.names(1:end-1);
115
+
116
+
117
+ % get label for each block with confidence
118
+ % total_labels = char(zeros(num_blocks, 7));
119
+ labels.vert_labels = cell(nsegs, 1);
120
+ labels.vert_conf = zeros(nsegs, 1);
121
+ labels.horz_labels = cell(nsegs, 1);
122
+ labels.horz_conf = zeros(nsegs, 1);
123
+ for s = 1:nsegs
124
+ [labels.vert_conf(s), c] = max(pV(s, :));
125
+ labels.vert_labels(s) = vClassifier.names(c);
126
+ [labels.horz_conf(s), c] = max(pH(s, :));
127
+ labels.horz_labels(s) = hClassifier.names(c);
128
+ end
129
+
130
+ [tmp, vlabels] = max(pV, [], 2);
131
+ [tmp, hlabels] = max(pH, [], 2);
132
+
133
+ if 0
134
+ [hy, estType] = geometry2horizon(imsegs, vlabels, hlabels, vpdata);
135
+
136
+ labels.hy = 1-hy;
137
+ labels.hestType = estType;
138
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/test_boosted_dt_mc.m ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function confidences = test_boosted_dt_mc(classifier, features)
2
+ % confidences = test_boosted_dt_mc(classifier, features)
3
+ %
4
+ % Returns a log likelihod ratio for each class in the classifier
5
+ %
6
+ % Input:
7
+ % classifier: boosted decision tree classifier
8
+ % features: classifier features (ndata, nvariables)
9
+ % Output:
10
+ % confidences(ndata, nclasses):
11
+ % P(class=k|features) \propto 1./(1+exp(-confidences(k)))
12
+
13
+ npred = classifier.wcs(1).dt.npred;
14
+ if size(features, 2)~=npred
15
+ error('Incorrect number of attributes')
16
+ end
17
+
18
+ wcs = classifier.wcs;
19
+ nclasses = size(wcs, 2);
20
+
21
+ ntrees = size(wcs, 1);
22
+
23
+ confidences = zeros(size(features, 1), nclasses);
24
+ for c = 1:nclasses
25
+ for t = 1:ntrees
26
+ if ~isempty(wcs(t,c).dt)
27
+ if 1
28
+ dt = wcs(t,c).dt;
29
+ [var, cut, children, catsplit] = tree_getParameters(dt);
30
+ nodes = treevalc(int32(var), cut, int32(children(:, 1)), ...
31
+ int32(children(:, 2)), catsplit(:, 1), features');
32
+ %disp(num2str(nodes));
33
+ else
34
+ [class_indices, nodes, classes] = treeval(wcs(t, c).dt, features);
35
+ end
36
+ confidences(:, c) = confidences(:, c) + wcs(t, c).confidences(nodes);
37
+ end
38
+ end
39
+ confidences(:, c) = confidences(:, c) + classifier.h0(c);
40
+ end
41
+
42
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/test_boosted_kde_2c.m ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function p = test_boosted_kde_2c(density, x, data)
2
+ % used to evaluate the likelihood of a point from a kernel density estimate
3
+ % density is the density function
4
+ % x are the points at which f is defined (assumed to be equally spaced)
5
+ % data are the data points to be evaluated
6
+ % p is the value of f(xi) where x(xi) is the closest value in x to y
7
+
8
+ n = length(x);
9
+ wx = x(2)-x(1);
10
+ indices = round((data- x(1))/wx)+1;
11
+ indices = min(max(indices, 1), n);
12
+ p = density(indices);
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_dt_2c.m ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function classifier = train_boosted_dt_2c(features, cat_features, ...
2
+ labels, num_iterations, nodespertree, stopval, w)
3
+ % classifier = train_boosted_dt_2c(features, cat_features, ...
4
+ % labels, num_iterations, nodespertree, stopval, w)
5
+ %
6
+ % Trains a two-class classifier based on boosted decision trees. Boosting done by the
7
+ % logistic regression version of Adaboost (Adaboost.L - Collins, Schapire,
8
+ % Singer 2002). At each iteration, a set of decision trees is created, with
9
+ % confidences equal to 1/2*ln(P+/P-), according to the
10
+ % weighted distribution. Weights are assigned as
11
+ % w(i,j) = 1 / (1+exp(sum{t in iterations}[yij*ht(xi, j)])).
12
+ % features(ndata, nfeatures)
13
+ % cat_features - discrete-valued output feature indices (could be [])
14
+ % labels - {-1, 1}
15
+ % num_iterations - the number of trees to create
16
+
17
+
18
+ num_data = length(labels);
19
+
20
+ cl = [-1 1];
21
+ y = labels;
22
+
23
+ if ~exist('stopval', 'var') || isempty(stopval)
24
+ stopval = 0;
25
+ end
26
+
27
+ if ~exist('w', 'var') || isempty(w)
28
+ w = ones(num_data, 1);
29
+ end
30
+ w = w/sum(w);
31
+
32
+ classifier.h0 = 0;
33
+
34
+ % for i = 1:2
35
+ % indices = find(y==cl(i));
36
+ % count = numel(indices);
37
+ % if cl(i)==1
38
+ % classifier.h0 = log(count / (num_data-count));
39
+ % end
40
+ % w(indices) = 1 / count/2;
41
+ % end
42
+
43
+ data_confidences = zeros(num_data, 1);
44
+ aveconf = [];
45
+
46
+ for t = 1:num_iterations
47
+ % learn decision tree based on weighted distribution
48
+ dt = treefitw(features, y, w, 1/num_data/2, 'catidx', cat_features, 'method', 'classification', 'maxnodes', nodespertree*4);
49
+ [tmp, level] = min(abs(dt.ntermnodes-nodespertree));
50
+ dt = treeprune(dt, 'level', level-1);
51
+ % assign partition confidences
52
+ pi = (strcmp(dt.classname{1},'1')) + (2*strcmp(dt.classname{2},'1'));
53
+ ni = (strcmp(dt.classname{1},'-1')) + (2*strcmp(dt.classname{2},'-1'));
54
+ classprob = dt.classprob;
55
+ confidences = 1/2*(log(classprob(:, pi)) - log(classprob(:, ni)));
56
+
57
+ % assign weights
58
+ [class_indices, nodes, classes] = treeval(dt, features);
59
+ data_confidences = data_confidences + confidences(nodes);
60
+ w = 1 ./ (1+exp(y.*data_confidences));
61
+ w = w / sum(w);
62
+
63
+ pconf = mean(1./(1+exp(-data_confidences(y==1))));
64
+ nconf = mean(1./(1+exp(-data_confidences(y==-1))));
65
+
66
+ disp(['c: ' num2str(mean(1 ./ (1+exp(-y.*data_confidences)))) ' e: ' ...
67
+ num2str(mean(y.*data_confidences < 0)) ' c_p: ' num2str(pconf) ' c_n: ' num2str(nconf)]);
68
+
69
+ classifier.wcs(t,1).dt = dt;
70
+ classifier.wcs(t,1).confidences = confidences;
71
+ %pause(0.1);
72
+
73
+ aveconf(t) = mean(1 ./ (1+exp(-y.*data_confidences)));
74
+ if t>10 && (aveconf(t)-aveconf(t-10) < stopval)
75
+ disp(num2str(aveconf))
76
+ disp(['Stopping after ' num2str(t) ' trees'])
77
+ break;
78
+ end
79
+
80
+ end
81
+
82
+ disp(['mean conf = ' num2str(mean(1 ./ (1+exp(-y.*data_confidences))))]);
83
+ disp(['training error: ' num2str(mean(y.*data_confidences < 0))]);
84
+
85
+
86
+
87
+
88
+
89
+
90
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_dt_mc.m ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function classifier = train_boosted_dt_mc(features, cat_features, labels, ...
2
+ num_iterations, num_nodes, stopval, init_weights, varargin)
3
+ %
4
+ %classifier = train_boosted_dt_mc(features, cat_features, labels, ...
5
+ % num_iterations, num_nodes, stopval, init_weights, varargin)
6
+ %
7
+ % Train a classifier based on boosted decision trees. Boosting done by the
8
+ % logistic regression version of Adaboost (Adaboost.L - Collins, Schapire,
9
+ % Singer 2002). At each
10
+ % iteration, a set of decision trees is created for each class, with
11
+ % confidences equal to 1/2*ln(P+/P-) for that class, according to the
12
+ % weighted distribution. Final classification is based on the largest
13
+ % confidence label (possibly incorporating a prior as h0(c) =
14
+ % 1/2*ln(Pc/(1-Pc)). Weights are assigned as
15
+ % w(i,j) = 1 / (1+exp(sum{t in iterations}[yij*ht(xi, j)])).
16
+
17
+ if length(varargin) == 1 % class names supplied
18
+ gn = varargin{1};
19
+ gid = zeros(size(labels));
20
+ for c = 1:length(gn)
21
+ ind = find(strcmp(labels, gn{c}));
22
+ gid(ind) = c;
23
+ if ~isempty(init_weights)
24
+ disp([gn{c} ': ' num2str(sum(init_weights(ind)))]);
25
+ else
26
+ disp([gn{c} ': ' num2str(length(ind))]);
27
+ end
28
+ end
29
+ ind = find(gid==0);
30
+ gid(ind) = [];
31
+ labels(ind) = [];
32
+ features(ind, :) = [];
33
+ else
34
+ [gid, gn] = grp2idx(labels);
35
+ end
36
+
37
+ if ~exist('stopval', 'var') || isempty(stopval)
38
+ stopval = 0;
39
+ end
40
+ if ~exist('init_weights', 'var')
41
+ init_weights = [];
42
+ end
43
+
44
+ classifier.names = gn;
45
+
46
+ num_classes = length(gn);
47
+ num_data = length(gid);
48
+
49
+ if isempty(init_weights)
50
+ init_weights = ones(num_data, 1)/num_data;
51
+ else
52
+ init_weights = init_weights / sum(init_weights);
53
+ end
54
+
55
+ % if no examples from a class are present, create one dummy example for
56
+ % that class with very small weight
57
+ for c = 1:numel(gn)
58
+ if ~any(gid==c)
59
+ disp(['warning: no examples from class ' gn(c)])
60
+ gid(end+1) = c;
61
+ features(end+1, :) = zeros(size(features(end, 1)));
62
+ num_data = num_data + 1;
63
+ init_weights(end+1) = min(init_weights)/2;
64
+ end
65
+ end
66
+
67
+ all_conf = zeros(num_data, num_classes);
68
+ for c = 1:num_classes
69
+
70
+ disp(['class: ' num2str(gn{c})]);
71
+ y = (gid == c)*2-1;
72
+ cl = [-1 1];
73
+ nc = 2;
74
+ w = zeros(num_data, 1);
75
+ cw = zeros(num_classes, 1);
76
+ for i = 1:2
77
+ indices = find(y==cl(i));
78
+ %count = sum(init_weights(indices));
79
+ %w(indices) = init_weights(indices) / count / 2;
80
+ w(indices) = init_weights(indices);
81
+
82
+ if cl(i)==1
83
+ %classifier.h0(c) = log(count / (1-count));
84
+ classifier.h0(c) = 0;
85
+ end
86
+
87
+ end
88
+
89
+ data_confidences = zeros(num_data, 1);
90
+ aveconf = [];
91
+
92
+ for t = 1:num_iterations
93
+ % learn decision tree based on weighted distribution
94
+ dt = treefitw(features, y, w, 1/num_data/2, 'catidx', cat_features, 'method', 'classification', 'maxnodes', num_nodes*4);
95
+ [tmp, level] = min(abs(dt.ntermnodes-num_nodes));
96
+ dt = treeprune(dt, 'level', level-1);
97
+
98
+ % assign partition confidences
99
+ pi = (strcmp(dt.classname{1},'1')) + (2*strcmp(dt.classname{2},'1'));
100
+ ni = (strcmp(dt.classname{1},'-1')) + (2*strcmp(dt.classname{2},'-1'));
101
+
102
+ classprob = dt.classprob;
103
+ confidences = 1/2*(log(classprob(:, pi)) - log(classprob(:, ni)));
104
+
105
+ % assign weights
106
+ [class_indices, nodes, classes] = treeval(dt, features);
107
+ data_confidences = data_confidences + confidences(nodes);
108
+
109
+ w = 1 ./ (1+exp(y.*data_confidences));
110
+ w = w / sum(w);
111
+
112
+ %disp(['c: ' num2str(mean(1 ./ (1+exp(-y.*data_confidences)))) ' e: ' num2str(mean(y.*data_confidences < 0)) ' w: ' num2str(max(w))]);
113
+
114
+ classifier.wcs(t, c).dt = dt;
115
+ classifier.wcs(t, c).confidences = confidences;
116
+
117
+
118
+ aveconf(t) = mean(1 ./ (1+exp(-y.*data_confidences)));
119
+ if t>10 && (aveconf(t)-aveconf(t-10) < stopval)
120
+ disp(num2str(aveconf))
121
+ disp(['Stopping after ' num2str(t) ' trees'])
122
+ break;
123
+ end
124
+
125
+ end
126
+
127
+ finalconf = 1 ./ (1+exp(-y.*data_confidences));
128
+ finalerr = (y.*data_confidences < 0);
129
+ disp(['confidence:: mean: ' num2str(mean(finalconf)) ...
130
+ ' pos: ' num2str(mean(finalconf(y==1))) ...
131
+ ' neg: ' num2str(mean(finalconf(y~=1)))]);
132
+ disp(['training error:: mean: ' num2str(mean(finalerr)) ...
133
+ ' pos: ' num2str(mean(finalerr(y==1))) ...
134
+ ' neg: ' num2str(mean(finalerr(y~=1)))]);
135
+ all_conf(:, c) = data_confidences+classifier.h0(c);
136
+
137
+ end
138
+
139
+ % compute and display training error
140
+ [tmp, assigned_label] = max(all_conf, [], 2);
141
+ conf_matrix = zeros(num_classes, num_classes);
142
+ for c = 1:num_classes
143
+ indices = find(gid==c);
144
+ for c2 = 1:num_classes
145
+ conf_matrix(c, c2) = mean(assigned_label(indices)==c2);
146
+ end
147
+ disp([gn{c} ' error: ' num2str(mean(assigned_label(indices)~=c))]);
148
+ end
149
+ disp('Confusion Matrix: ');
150
+ disp(num2str(conf_matrix));
151
+ disp(['total error: ' num2str(mean(assigned_label~=gid))]);
152
+
153
+
154
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_kde_2c.m ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function density = train_boosted_kde_2c(data, labels, ranges, num_iter)
2
+ % Try to learn ln(P(x1, x2 | +)/P(x1, x2 | -), where + indicates that a pair of points,
3
+ % x, are in the same cluster and - indicates that the pair are in different
4
+ % clusters. Use boosting to estimate the parameters of the density in a
5
+ % naive structure.
6
+ % density(num_features).{x, log_ratio}
7
+
8
+
9
+ ndata = numel(labels);
10
+ nfeatures = size(data, 2);
11
+
12
+ if ~exist('ranges') || isempty(ranges)
13
+ ranges = cell(nfeatures, 1);
14
+ end
15
+
16
+ for f = 1:numel(ranges)
17
+ if isempty(ranges{f})
18
+ ranges{f} = 'unbounded';
19
+ end
20
+ end
21
+
22
+ pos_indices = find(labels==1);
23
+ neg_indices = find(labels==-1);
24
+ npos = length(pos_indices);
25
+ nneg = length(neg_indices);
26
+ %weights = 1/num_data*ones(num_data, 1);
27
+ % weights = zeros(ndata, 1);
28
+ % weights(pos_indices) = 1/2/npos;
29
+ % weights(neg_indices) = 1/2/nneg;
30
+ weights = repmat(1/ndata, [ndata 1]);
31
+ pos_data = data(pos_indices, :);
32
+ neg_data = data(neg_indices, :);
33
+
34
+ kernel_width = zeros(nfeatures, 1);
35
+
36
+ % get optimal kernel width and xrange
37
+ % this window parameter is recommended by Silverman(1986) for non-normal plots
38
+ for f = 1:nfeatures
39
+
40
+ y = data(:, f);
41
+ s = std(y);
42
+ sorty = sort(y);
43
+ n = length(y);
44
+ iq = sorty(round(3/4*n))-sorty(round(n/4));
45
+ density(f).kernelwidth = 0.9*min(s, iq/1.34)*(1/n)^(1/5);
46
+ %disp(num2str(density(f).kernelwidth))
47
+ [tmp, density(f).x] = ksdensity(y, 'weights', weights, ...
48
+ 'width', density(f).kernelwidth, 'support', ranges{f});
49
+ density(f).log_ratio = zeros(size(density(f).x));
50
+ end
51
+
52
+ total_confidences = zeros(ndata, 1);
53
+ for m = 1:num_iter
54
+ pos_weights = weights(pos_indices);
55
+ neg_weights = weights(neg_indices);
56
+ tmp_confidences = zeros(ndata, 1);
57
+ % update densities
58
+ for f = 1:nfeatures
59
+ pos_f = ksdensity(pos_data(:, f), density(f).x, 'weights', pos_weights, ...
60
+ 'width', density(f).kernelwidth, 'support', ranges{f});
61
+ pos_f = pos_f + 1/(ndata/2);
62
+ pos_f = pos_f / sum(pos_f) * sum(pos_weights);
63
+ neg_f = ksdensity(neg_data(:, f), density(f).x, 'weights', neg_weights, ...
64
+ 'width', density(f).kernelwidth, 'support', ranges{f});
65
+ neg_f = neg_f + 1/(ndata/2);
66
+ neg_f = neg_f / sum(neg_f) * sum(neg_weights);
67
+
68
+ tmp_ratio = (log(pos_f)-log(neg_f));
69
+ %density(f).log_ratio = density(f).log_ratio + tmp_ratio;
70
+ curr_ratio(f).log_ratio = tmp_ratio;
71
+ tmp_confidences = tmp_confidences + ...
72
+ test_boosted_kde_2c(tmp_ratio, density(f).x, data(:, f))';
73
+ end
74
+
75
+ % get alpha parameter for confidence
76
+ alpha = fminbnd(@compute_expected_confidence, 0.001, 2.0, [], labels, tmp_confidences, weights);
77
+ %disp(['alpha = ' num2str(alpha)]);
78
+ for f = 1:nfeatures
79
+ density(f).log_ratio = density(f).log_ratio + alpha*curr_ratio(f).log_ratio;
80
+ end
81
+
82
+ weights = weights .* exp(-alpha*tmp_confidences.*labels);
83
+ sumw = sum(weights);
84
+ %disp(['sum w = ' num2str(sumw)]);
85
+ weights = weights / sumw;
86
+
87
+ total_confidences = total_confidences + alpha*tmp_confidences;
88
+
89
+ disp(['training error: n_err = ' num2str(mean(total_confidences(neg_indices)>=0)) ' p_err = ' ...
90
+ num2str(mean(total_confidences(pos_indices)<0))]);
91
+ if 0
92
+ [f1, x] = ksdensity(total_confidences(pos_indices));
93
+ f2 = ksdensity(total_confidences(neg_indices), x);
94
+ figure(1), plot(x, f1, 'b', x, f2, 'r');
95
+ pause(0.5);
96
+ end
97
+
98
+ end
99
+
100
+
101
+
102
+
103
+ function expected_confidence = compute_expected_confidence(alpha, labels, confidences, weights)
104
+ % used to choose alpha
105
+ % alpha is chosen so that the expected confidence:
106
+ % E(weight*exp(-alpha*label*confidence)*label*confidence) = 0
107
+ % Note: Absolute value used so that minimization results in best confidence
108
+ % confidence. Normalization is unnecesary for finding alpha, though.
109
+ new_weights = exp(-alpha*labels.*confidences).*weights;
110
+ expected_confidence = sum(new_weights);
111
+ %expected_confidence = abs(sum(new_weights.*labels.*confidences)/sum(new_weights));
112
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/train_boosted_stubs_mc.m ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function classifier = train_boosted_dt_mc(features, cat_features, labels, ...
2
+ num_iterations, num_nodes, stopval, init_weights, varargin)
3
+ %
4
+ %classifier = train_boosted_dt_mc(features, cat_features, labels, ...
5
+ % num_iterations, num_nodes, stopval, init_weights, varargin)
6
+ %
7
+ % Train a classifier based on boosted decision trees. Boosting done by the
8
+ % logistic regression version of Adaboost (Adaboost.L - Collins, Schapire,
9
+ % Singer 2002). At each
10
+ % iteration, a set of decision trees is created for each class, with
11
+ % confidences equal to 1/2*ln(P+/P-) for that class, according to the
12
+ % weighted distribution. Final classification is based on the largest
13
+ % confidence label (possibly incorporating a prior as h0(c) =
14
+ % 1/2*ln(Pc/(1-Pc)). Weights are assigned as
15
+ % w(i,j) = 1 / (1+exp(sum{t in iterations}[yij*ht(xi, j)])).
16
+
17
+ if length(varargin) == 1 % class names supplied
18
+ gn = varargin{1};
19
+ gid = zeros(size(labels));
20
+ for c = 1:length(gn)
21
+ ind = find(strcmp(labels, gn{c}));
22
+ gid(ind) = c;
23
+ if ~isempty(init_weights)
24
+ disp([gn{c} ': ' num2str(sum(init_weights(ind)))]);
25
+ else
26
+ disp([gn{c} ': ' num2str(length(ind))]);
27
+ end
28
+ end
29
+ ind = find(gid==0);
30
+ gid(ind) = [];
31
+ labels(ind) = [];
32
+ features(ind, :) = [];
33
+ else
34
+ [gid, gn] = grp2idx(labels);
35
+ end
36
+
37
+ if ~exist('stopval', 'var') || isempty(stopval)
38
+ stopval = 0;
39
+ end
40
+ if ~exist('init_weights', 'var')
41
+ init_weights = [];
42
+ end
43
+
44
+ classifier.names = gn;
45
+
46
+ num_classes = length(gn);
47
+ num_data = length(gid);
48
+
49
+ if isempty(init_weights)
50
+ init_weights = ones(num_data, 1)/num_data;
51
+ else
52
+ init_weights = init_weights / sum(init_weights);
53
+ end
54
+
55
+ % if no examples from a class are present, create one dummy example for
56
+ % that class with very small weight
57
+ for c = 1:numel(gn)
58
+ if ~any(gid==c)
59
+ disp(['warning: no examples from class ' gn(c)])
60
+ gid(end+1) = c;
61
+ features(end+1, :) = zeros(size(features(end, 1)));
62
+ num_data = num_data + 1;
63
+ init_weights(end+1) = min(init_weights)/2;
64
+ end
65
+ end
66
+
67
+ all_conf = zeros(num_data, num_classes);
68
+ for c = 1:num_classes
69
+
70
+ disp(['class: ' num2str(gn{c})]);
71
+ y = (gid == c)*2-1;
72
+ cl = [-1 1];
73
+ nc = 2;
74
+ w = zeros(num_data, 1);
75
+ cw = zeros(num_classes, 1);
76
+ for i = 1:2
77
+ indices = find(y==cl(i));
78
+ %count = sum(init_weights(indices));
79
+ %w(indices) = init_weights(indices) / count / 2;
80
+ w(indices) = init_weights(indices);
81
+
82
+ if cl(i)==1
83
+ %classifier.h0(c) = log(count / (1-count));
84
+ classifier.h0(c) = 0;
85
+ end
86
+
87
+ end
88
+
89
+ data_confidences = zeros(num_data, 1);
90
+ aveconf = [];
91
+
92
+ for t = 1:num_iterations
93
+ % learn decision tree based on weighted distribution
94
+ dt = treefitw(features, y, w, 1/num_data/2, 'catidx', cat_features, 'method', 'classification', 'maxnodes', num_nodes*4);
95
+ [tmp, level] = min(abs(dt.ntermnodes-num_nodes));
96
+ dt = treeprune(dt, 'level', level-1);
97
+
98
+ % assign partition confidences
99
+ pi = (strcmp(dt.classname{1},'1')) + (2*strcmp(dt.classname{2},'1'));
100
+ ni = (strcmp(dt.classname{1},'-1')) + (2*strcmp(dt.classname{2},'-1'));
101
+
102
+ classprob = dt.classprob;
103
+ confidences = 1/2*(log(classprob(:, pi)) - log(classprob(:, ni)));
104
+
105
+ % assign weights
106
+ [class_indices, nodes, classes] = treeval(dt, features);
107
+ data_confidences = data_confidences + confidences(nodes);
108
+
109
+ w = 1 ./ (1+exp(y.*data_confidences));
110
+ w = w / sum(w);
111
+
112
+ disp(['c: ' num2str(mean(1 ./ (1+exp(-y.*data_confidences)))) ' e: ' num2str(mean(y.*data_confidences < 0)) ' w: ' num2str(max(w))]);
113
+
114
+ classifier.wcs(t, c).dt = dt;
115
+ classifier.wcs(t, c).confidences = confidences;
116
+
117
+
118
+ aveconf(t) = mean(1 ./ (1+exp(-y.*data_confidences)));
119
+ if t>10 && (aveconf(t)-aveconf(t-10) < stopval)
120
+ disp(num2str(aveconf))
121
+ disp(['Stopping after ' num2str(t) ' trees'])
122
+ break;
123
+ end
124
+
125
+ end
126
+
127
+ finalconf = 1 ./ (1+exp(-y.*data_confidences));
128
+ finalerr = (y.*data_confidences < 0);
129
+ disp(['confidence:: mean: ' num2str(mean(finalconf)) ...
130
+ ' pos: ' num2str(mean(finalconf(y==1))) ...
131
+ ' neg: ' num2str(mean(finalconf(y~=1)))]);
132
+ disp(['training error:: mean: ' num2str(mean(finalerr)) ...
133
+ ' pos: ' num2str(mean(finalerr(y==1))) ...
134
+ ' neg: ' num2str(mean(finalerr(y~=1)))]);
135
+ all_conf(:, c) = data_confidences+classifier.h0(c);
136
+
137
+ end
138
+
139
+ % compute and display training error
140
+ [tmp, assigned_label] = max(all_conf, [], 2);
141
+ conf_matrix = zeros(num_classes, num_classes);
142
+ for c = 1:num_classes
143
+ indices = find(gid==c);
144
+ for c2 = 1:num_classes
145
+ conf_matrix(c, c2) = mean(assigned_label(indices)==c2);
146
+ end
147
+ disp([gn{c} ' error: ' num2str(mean(assigned_label(indices)~=c))]);
148
+ end
149
+ disp('Confusion Matrix: ');
150
+ disp(num2str(conf_matrix));
151
+ disp(['total error: ' num2str(mean(assigned_label~=gid))]);
152
+
153
+
154
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/tree_getNewVersion.m ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ function dt = tree_getNewVersion(dt)
2
+
3
+ cut_old = dt.cut;
4
+ dt.cut = num2cell(dt.cut);
5
+ ind = find(dt.var<0);
6
+ for k = 1:numel(ind)
7
+ dt.cut{ind(k)} = dt.catsplit(cut_old(ind(k)), :);
8
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/tree_getParameters.m ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [var, cut, children, catsplit] = tree_getParameters(dt)
2
+
3
+ if iscell(dt.cut)
4
+ var = dt.var;
5
+ children = dt.children;
6
+
7
+ cut = zeros(size(dt.cut));
8
+ catind = var<0;
9
+ ncat = sum(catind);
10
+ catsplit = cell(ncat, 2);
11
+ ncat = 0;
12
+ for k = 1:numel(cut)
13
+ if var(k)>=0
14
+ cut(k) = dt.cut{k};
15
+ else
16
+ ncat = ncat+1;
17
+ cut(k) = ncat;
18
+ catsplit(ncat, :) = dt.cut{k};
19
+ end
20
+ end
21
+ else
22
+ var = dt.var;
23
+ cut = dt.cut;
24
+ children = dt.children;
25
+ catsplit = dt.catsplit;
26
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.c ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include "mex.h"
2
+ #include <stdlib.h>
3
+ #include <stdio.h>
4
+ /**
5
+ * Return the decision tree node corresponding to the given value set
6
+ *
7
+ * var[n]: the attribute ids for node n
8
+ * cut[n]: the threshold value for node n
9
+ * left_child[n]: the node id of the left child of node n, 0 if node n is terminal
10
+ * right_child[n]: the node id of the right child of node n, 0 if node n is terminal
11
+ * ncatsplit[c]: the number of values resulting in a left branch
12
+ * catsplit[c]: the values that would result in a left branch
13
+ * attributes: the attribute (variable) values for each feature
14
+ **/
15
+ void
16
+ treevalc(int* var, double* cut, int* left_child, int* right_child,
17
+ int* ncatsplit, double** catsplit,
18
+ double* attributes,
19
+ int* node_id) {
20
+
21
+ int currnode = 0;
22
+
23
+ int nextnode;
24
+ int currvar;
25
+ double currval;
26
+ int cid, v;
27
+ int numvals;
28
+ double* vals;
29
+
30
+ /* printf("init nodes: %d %d \n", left_child[currnode], right_child[currnode]); */
31
+
32
+ /* until reached terminal node */
33
+ while ((left_child[currnode] != 0) && (right_child[currnode] != 0)) {
34
+
35
+ /*printf("currnode: %d\n", currnode);*/
36
+
37
+ nextnode = -1;
38
+
39
+ currvar = abs(var[currnode])-1;
40
+ currval = attributes[currvar];
41
+
42
+ /* decision based on thresholded float value */
43
+ if (var[currnode] > 0) {
44
+
45
+ /*printf("currvar: %d\n", currvar);*/
46
+
47
+ /* branch left */
48
+ if (currval < cut[currnode]) {
49
+ nextnode = left_child[currnode];
50
+ }
51
+ /* branch right */
52
+ else {
53
+ nextnode = right_child[currnode];
54
+ }
55
+ }
56
+ /* decision based on discrete value */
57
+ else {
58
+ numvals = ncatsplit[(int)cut[currnode]-1];
59
+ vals = catsplit[(int)cut[currnode]-1];
60
+ for (v = 0; v < numvals; v++) {
61
+ if (currval == vals[v]) {
62
+ nextnode = left_child[currnode];
63
+ break;
64
+ }
65
+ }
66
+ if (nextnode == -1) {
67
+ nextnode = right_child[currnode];
68
+ }
69
+ }
70
+
71
+ currnode = nextnode-1;
72
+ /* printf("curr node: %d \n", currnode);*/
73
+ }
74
+
75
+ *node_id = currnode+1;
76
+
77
+ }
78
+
79
+ /**
80
+ * plhs = {var, cut, left_child, right_child, catsplit(cell array), attributes(numatt, numdata)}
81
+ *
82
+ */
83
+ void mexFunction(int nlhs, mxArray *plhs[],
84
+ int nrhs, const mxArray *prhs[])
85
+ {
86
+
87
+ if (nrhs != 6) {
88
+ printf("Error: wrong number of input arguments: %d.\n", nlhs);
89
+ printf("Syntax: node_ids = treevalc(var, cut, left_child, right_child, catsplit, attributes)\n");
90
+ }
91
+
92
+ int* var = (int*)mxGetPr(prhs[0]);
93
+ double* cut = mxGetPr(prhs[1]);
94
+ int* left_child = (int*)mxGetPr(prhs[2]);
95
+ int* right_child = (int*)mxGetPr(prhs[3]);
96
+ /* get catsplit variables */
97
+ int nsplits = mxGetNumberOfElements(prhs[4]);
98
+ int* ncatsplit = malloc(sizeof(int) * nsplits);
99
+ double** catsplit = malloc(sizeof(double*) * nsplits);
100
+
101
+ int n = 0;
102
+ for (n = 0; n < nsplits; n++) {
103
+ mxArray* catsplit_cell_mx = mxGetCell(prhs[4], n);
104
+ if (catsplit_cell_mx == 0) {
105
+ printf("null cell");
106
+ }
107
+ ncatsplit[n] = mxGetNumberOfElements(catsplit_cell_mx);
108
+ catsplit[n] = (double*)mxGetPr(catsplit_cell_mx);
109
+ }
110
+
111
+ int numatt = mxGetM(prhs[5]);
112
+ int numdata = mxGetN(prhs[5]);
113
+
114
+ /* printf("num data = %d num att = %d\n", numdata, numatt);*/
115
+
116
+ double* all_attributes = mxGetPr(prhs[5]);
117
+
118
+ plhs[0] = mxCreateDoubleMatrix(numdata, 1, mxREAL);
119
+ double* node_ids = mxGetPr(plhs[0]);
120
+
121
+
122
+ int tmp_id;
123
+ for (n = 0; n < numdata; n++) {
124
+ treevalc(var, cut, left_child, right_child, ncatsplit, catsplit,
125
+ &all_attributes[numatt*n], &tmp_id);
126
+ node_ids[n] = (double)(tmp_id);
127
+ /* printf("final node id: %d\n", tmp_id); */
128
+ }
129
+
130
+ free(catsplit);
131
+ free(ncatsplit);
132
+
133
+ }
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.mexa64 ADDED
Binary file (9.3 kB). View file
 
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.mexglx ADDED
Binary file (8.92 kB). View file
 
SUN_source_code_v1/code/GeometricContext_dhoiem/src/boosting/treevalc.mexmaci ADDED
Binary file (9.22 kB). View file
 
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPclassifierOutput2confidenceImages.m ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [cimages, cnames] = APPclassifierOutput2confidenceImages(imsegs, conf_maps)
2
+ % Computes confidence maps from results of APPtestImage
3
+ %
4
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
5
+ % Current Version: 1.0 09/30/2005
6
+
7
+ nvclasses = length(conf_maps(1).vnames);
8
+ nhclasses = length(conf_maps(1).hnames);
9
+ nclasses = nvclasses+nhclasses;
10
+ for v = 1:nvclasses
11
+ cnames{v} = ['v' conf_maps(1).vnames{v}];
12
+ end
13
+ for h = 1:nhclasses
14
+ cnames{nvclasses+h} = ['h' conf_maps(1).hnames{h}];
15
+ end
16
+
17
+
18
+ cimages = cell(length(imsegs), 1);
19
+
20
+ for f = 1:length(imsegs)
21
+
22
+ for n = 1:nclasses
23
+ cim{n} = single(ones(imsegs(f).imsize)/nclasses);
24
+ end
25
+
26
+ % for s = 1:imsegs(f).nseg
27
+ %
28
+ % ind = find(imsegs(f).segimage==s);
29
+ %
30
+ % for v = 1:nvclasses
31
+ % cim{v}(ind) = single(conf_maps(f).vmap(s, v));
32
+ % end
33
+ % for h = 1:nhclasses
34
+ % cim{nvclasses+h}(ind) = single(conf_maps(f).hmap(s, h));
35
+ % end
36
+ %
37
+ % end
38
+
39
+ cimages{f} = single(zeros([imsegs(f).imsize nclasses]));
40
+
41
+ vind = find(strcmp(cnames, 'v090'));
42
+
43
+ for n = 1:nclasses
44
+ if n <= nvclasses
45
+ %cimages{f}(:, :, n) = cim{n};
46
+ tmpmap = conf_maps(f).vmap(:, n);
47
+ cimages{f}(:, :, n) = tmpmap(imsegs(f).segimage);
48
+ else
49
+ % multiply P(vclass|X) by P(vertical|X)
50
+ tmpmap = conf_maps(f).hmap(:, n-nvclasses);
51
+ %cimages{f}(:, :, n) = cim{n}.*cim{vind};
52
+ cimages{f}(:, :, n) = tmpmap(imsegs(f).segimage).*cimages{f}(:, :, vind);
53
+ end
54
+ end
55
+ end
56
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPestimateHorizon.m ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function y_h = APPestimateHorizon(lines)
2
+ % Estimates horizon position of image
3
+ % lines should be normalized according to size of image
4
+ % need to multiply output by mean(imsize(1:2)) to get pixel value
5
+ %
6
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
7
+ % Current Version: 1.0 09/30/2005
8
+
9
+ max_angle = pi/8;
10
+
11
+ % lines(num_lines, [x1 x2 y1 y2 angle r])
12
+ % points(num_points, [y x])
13
+
14
+
15
+ a = lines(:, 5);
16
+ x1 = lines(:, 1);
17
+ y1 = lines(:, 3);
18
+ x2 = lines(:, 2);
19
+ y2 = lines(:, 4);
20
+ r = lines(:, 6);
21
+ cosa = cos(a);
22
+ sina= sin(a);
23
+ tana = tan(a);
24
+
25
+ max_dist = 1E2;
26
+
27
+ num_lines = size(lines, 1);
28
+ if num_lines <= 1
29
+ points = zeros(0, 2);
30
+ return;
31
+ end
32
+
33
+ num_pairs = num_lines*(num_lines-1)/2;
34
+
35
+ adist = pdist(a, 'cityblock');
36
+ %adist = mod(adist, pi/2) < max_angle;
37
+ adist = adist < max_angle;
38
+ adistM = squareform(adist);
39
+
40
+ points = zeros(num_pairs, 2);
41
+ count = 0;
42
+ for i = 1:num_lines
43
+ for j = i+1:num_lines
44
+ if adistM(i, j) & (r(i)~=r(j) | a(i)~=a(j))
45
+ count = count+1;
46
+ if a(i) == a(j) | a(i) == -a(j) % parallel but not colinear
47
+ points(count, 1:2) = ([x1(i) y1(i)]+[x1(j) y1(j)])/2 + [sina(i) cosa(i)]*max_dist;
48
+ else
49
+ m1 = tana(i);
50
+ m2 = tana(j);
51
+ points(count, 1) = (-y1(i)*m2+m1*y1(j)-m1*m2*x1(j)+m1*x1(i)*m2)/(m1-m2);
52
+ points(count, 2) = (y1(j)-y1(i)+m1*x1(i)-m2*x1(j))/(m1-m2);
53
+ if abs(points(count, 1))==Inf | abs(points(count, 2))==Inf
54
+ points(count, 1:2) = ([x1(i) y1(i)]+[x1(j) y1(j)])/2 + [sina(i) cosa(i)]*max_dist;
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ points = points(1:count, :);
63
+
64
+ %ind = find(abs(points(:, 2))<0.02);% & abs(points(:, 1)) < 0.75);
65
+ %tpoints = points(ind, :);
66
+
67
+ if size(points, 1) == 0
68
+ y_h = 0.5;
69
+ else
70
+ y_h = 0.5+fminbnd(@sum_distance, -2, 2, [], points(:, 1));
71
+ end
72
+
73
+
74
+
75
+
76
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78
+ function d = sum_distance(x, pts)
79
+ d = sum(sqrt(abs(x-pts)));
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPestimateVp.m ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [v2, sigma, p, hpos] = APPestimateVp(lines, imsize, DO_DISPLAY)
2
+ % [v2, sigma, p, hpos] = APPestimateVp(lines, imsize, DO_DISPLAY)
3
+ % Estimates the principal vanishing points, as in Video Compass [Kosecka
4
+ % 2002], except that more (or fewer) than 3 principal vps could be found
5
+ %
6
+ % Input:
7
+ % lines([x1 x2 y1 y2 angle r])
8
+ % imsize: size of image
9
+ % DO_DISPLAY (optional): whether to create display figures (default=0)
10
+ % Output:
11
+ % v2(nvp, [x y]) - the found vanishing points; last is outliers; vp are in
12
+ % units of pixels/imsize(1), with image upper-left at (0,0)
13
+ % sigma(nvp) - the variance for each vp
14
+ % p(nlines, nvp) - the confidence that each line belongs to each vp
15
+ % hpos - horizon position (0 is top of image)
16
+ %
17
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
18
+ % Current Version: 1.0 09/30/2005
19
+
20
+ if ~exist('DO_DISPLAY')
21
+ DO_DISPLAY = 0;
22
+ end
23
+
24
+ nlines = size(lines, 1);
25
+
26
+ x1 = [lines(:, [1 3]) ones(size(lines, 1), 1)];
27
+ x2 = [lines(:, [2 4]) ones(size(lines, 1), 1)];
28
+
29
+ % get plane normals for line segments
30
+ l = cross(x1, x2);
31
+ l = l ./ repmat(sqrt(sum(l.^2,2)), 1, 3);
32
+
33
+ % make theta range from 0 to pi (instead of -pi to pi)
34
+ nbins = 60;
35
+ theta = mod(lines(:,5), pi);
36
+
37
+ % get histogram of thetas
38
+ binwidth = pi/nbins;
39
+ bincenters = [(binwidth/2):binwidth:(pi-binwidth/2)];
40
+ hist_theta = hist(theta, bincenters);
41
+
42
+ % smooth histogram
43
+ for b = 1:nbins
44
+ hist_theta(b) = sum(hist_theta(mod([(b-1):(b+1)]-1, nbins)+1) .* [0.25 0.5 0.25]);
45
+ end
46
+
47
+ % compute curvature of histogram
48
+ s = 9;
49
+ C = zeros(1, nbins);
50
+ for b = 1:nbins
51
+ C(b) = hist_theta(b) - mean(hist_theta(mod([(b-(s-1)/2):(b+(s-1)/2)]-1, nbins)+1));
52
+ end
53
+
54
+
55
+ % find dominant peaks surrounded by zero crossings
56
+ zc_pos = find((C > 0) & ([C(end) C(1:end-1)]<0));
57
+ zc_neg = find((C < 0) & ([C(end) C(1:end-1)]>0));
58
+
59
+ ngroups = length(zc_pos);
60
+ bc = bincenters + pi/nbins/2;
61
+ if zc_neg(1) < zc_pos(1)
62
+ i1 = round((zc_pos(end)+zc_neg(end))/2);
63
+ i2 = round((zc_neg(1) + zc_pos(1))/2);
64
+ groups{1} = find((theta > bc(i1)) | (theta < bc(i2)));
65
+ for i = 2:ngroups
66
+ i1 = round((zc_pos(i-1)+zc_neg(i-1))/2);
67
+ i2 = round((zc_neg(i) + zc_pos(i))/2);
68
+ groups{i} = find((theta > bc(i1)) & (theta < bc(i2)));
69
+ end
70
+
71
+ else
72
+ for i = 1:ngroups
73
+ i1 = ceil((zc_pos(i)+zc_neg(max(i-1,1)))/2);
74
+ i2 = ceil((zc_neg(i) + zc_pos(min(i+1,ngroups)))/2);
75
+ groups{i} = find((theta > bc(i1)) & (theta < bc(i2)));
76
+ end
77
+ end
78
+
79
+ % remove groups with few segments
80
+ remove = [];
81
+ thresh = max(0.05*nlines, 5);
82
+ %thresh = 6;
83
+ for i = 1:ngroups
84
+ if length(groups{i}) < thresh
85
+ %disp(num2str([length(groups{i}) nlines]))
86
+ remove(end+1) = i;
87
+ end
88
+ end
89
+ groups(remove) = [];
90
+ ngroups = length(groups);
91
+
92
+
93
+ % initialize EM
94
+ sigma = ones(1, ngroups); % sigma = variance here
95
+ p = zeros(nlines, ngroups); % p = P(v | l) = P(l|v)P(v)/P(l)
96
+ for i = 1:ngroups
97
+ p(groups{i}, i) = 1;
98
+ end
99
+ %p(:, end) = 1-sum(p(:, 1:end-1), 2);
100
+ A = l;
101
+ v = [];
102
+ sigma = [];
103
+ for i = 1:ngroups
104
+ normp = p(:, i) / sum(p(:, i));
105
+ W = diag(normp);
106
+ [eigV, lambda] = eig(A'*W'*W*A);
107
+ [tmp, smallest] = min(diag(lambda));
108
+ v(1:3,i) = eigV(:, smallest);
109
+ sp = sort(normp, 'descend');
110
+ sp = sum(sp(1:min(length(sp), 2)));
111
+ sigma(i) = normp' * (l*v(:,i)).^2 / (1-sum(sp));
112
+ end
113
+
114
+ % create outlier groups
115
+ nadd = 3;
116
+ ngroups = ngroups + nadd;
117
+ sigma(end+(1:nadd)) = 0.2;
118
+ tmpv = [0 0 1]';
119
+ v(1:3, end+1) = tmpv / sqrt(sum(tmpv.^2));
120
+ tmpv = [1 0 1]';
121
+ v(1:3, end+1) = tmpv / sqrt(sum(tmpv.^2));
122
+ tmpv = [-1 0 1]';
123
+ v(1:3, end+1) = tmpv / sqrt(sum(tmpv.^2));
124
+ p(:, end+(1:nadd)) = 0;
125
+ pv = ones(1, ngroups);
126
+
127
+ oldv = v;
128
+
129
+ % do EM
130
+ for iter = 1:15
131
+ oldp = p;
132
+ pv = pv / sum(pv);
133
+ % p(l|v) = 1/sqrt(2*pi)/sigma*exp(-(l'v).^2/sigma^2/2)
134
+ S = repmat(sigma, nlines, 1) + 1E-10;
135
+ plv = exp(-(l*v).^2 ./ S / 2) ./ sqrt(S) + 1E-10;
136
+ p = plv .* repmat(pv, nlines, 1);
137
+ p = p ./ repmat(sum(p, 2), 1, ngroups);
138
+ pv = sum(p, 1);
139
+ nmembers = zeros(ngroups,1);
140
+ for i = 1:ngroups
141
+ normp = p(:, i) / sum(p(:, i));
142
+ W = diag(normp);
143
+ [eigV, lambda] = eig(A'*W'*W*A);
144
+ [tmp, smallest] = min(diag(lambda));
145
+ v(1:3,i) = eigV(:, smallest);
146
+ sp = sort(normp, 'descend');
147
+ sp = sum(sp(1:min(length(sp), 2)));
148
+ if (1-sum(sp)) > 0
149
+ sigma(i) = normp' * (l*v(:,i)).^2 / (1-sum(sp));
150
+ else
151
+ sigma(i) = Inf;
152
+ end
153
+ end
154
+
155
+ % remove duplicate groups
156
+ remove =[];
157
+ for i = 1:ngroups
158
+ for j = i+1:ngroups
159
+ if (v(1:3, i)'*v(1:3,j) > 0.995) || (pv(i)*nlines <= 3) || (sigma(i) > 10)
160
+ remove(end+1) = i;
161
+ break;
162
+ end
163
+ end
164
+ end
165
+ if length(remove)>0
166
+ p(:, remove) = [];
167
+ sigma(remove) = [];
168
+ v(:, remove) = [];
169
+ pv(remove) = [];
170
+ ngroups = size(p, 2);
171
+ end
172
+
173
+ % break when v converges
174
+ if all(size(v)==size(oldv)) && min(diag(oldv'*v)) > 0.999
175
+ break;
176
+ end
177
+ oldv = v;
178
+
179
+ end
180
+
181
+ % convert vanishing directions to [x y 1] form with (0,0) in upper-left of
182
+ % image
183
+ %A = inv([1/imsize(1) 0 -1/imsize(2)/2*imsize(1); 0 1/imsize(1) -1/imsize(1)/2*imsize(1); 0 0 1]);
184
+ %v2 = (A*v)';
185
+ v2 = v';
186
+ v2(:, 3) = v2(:, 3)+1E-4;
187
+ % sigma = sigma ./ (v2(:, 3).^2)';
188
+ v2 = v2 ./ repmat(v2(:, 3), 1, 3);
189
+ v2(:, 1) = v2(:, 1) + imsize(2)/imsize(1)/2;
190
+ v2(:, 2) = v2(:, 2) + 1/2;
191
+
192
+ if nargout > 3
193
+ hpos = APPvp2horizon(v2, sigma, p, imsize);
194
+ end
195
+ %v2 = v;
196
+
197
+ if DO_DISPLAY
198
+
199
+ figure(1), hold off, plot(bincenters, hist_theta, 'b');
200
+ figure(1), hold on, plot(bincenters, hist_theta, 'r');
201
+ drawnow;
202
+
203
+ edge_im = zeros(imsize);
204
+ lines_nnorm(:, [1 2]) = lines(:, [1 2])*imsize(1) + imsize(2)/2;
205
+ lines_nnorm(:, [3 4]) = lines(:, [3 4])*imsize(1) + imsize(1)/2;
206
+ %lines_nnorm = lines;
207
+ for i = 1:length(groups)
208
+ if length(groups{i})>0
209
+ edge_im = draw_line_image2(edge_im, lines_nnorm(groups{i}, 1:4)', i);
210
+ end
211
+ end
212
+ figure(2), imshow(255-label2rgb(edge_im));
213
+
214
+ % plot new line memberships
215
+ [tmp, bestv] = max(p, [], 2);
216
+ edge_im = zeros(imsize);
217
+ for i = 1:ngroups
218
+ groups{i} = find(bestv==i);
219
+ if length(groups{i})>0
220
+ edge_im = draw_line_image2(edge_im, lines_nnorm(groups{i}, 1:4)', i);
221
+ end
222
+ %disp(length(groups{i}))
223
+ end
224
+ figure(3), imshow(255-label2rgb(edge_im));
225
+ drawnow;
226
+ end
227
+
228
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetImageFilters.m ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [doog_filters, texton_data] = APPgetImageFilters;
2
+ % Gets filters for creating texture features
3
+
4
+ % create pair data
5
+ angle_step = 15;
6
+ filter_size = 5;
7
+ sigma = 1.5;
8
+ r = 0.25;
9
+
10
+ % create difference of oriented gaussian filters
11
+ num_angles=180/angle_step;
12
+ doog_filters = zeros(filter_size, filter_size, num_angles);
13
+ for (theta=0:angle_step:180-angle_step)
14
+ doog_filters(:, :, theta/angle_step+1)=createDoogFilter(sigma, r, theta, filter_size);
15
+ end
16
+
17
+ % load textons
18
+ if 0 % DWH no longer uses textons
19
+ texton_data = load('unitex_6_1_2_1.4_2_32.mat'); % from Berkeley database
20
+ ordered_textons = select_diverse_textons(texton_data.tsim, 12);
21
+ new_tims = cell(length(ordered_textons), 1);
22
+ for i = 1:length(new_tims)
23
+ new_tims{i} = texton_data.tim{ordered_textons(i)};
24
+ end
25
+ texton_data.tim = new_tims;
26
+ end
27
+
28
+ texton_data.tim = [];
29
+ %num_textons = length(texton_data.tim);
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLabeledImage.m ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function image = APPgetLabeledImage(image, imsegs, vLabels, vConf, hLabels, hConf)
2
+ % image = APPgetLabeledImage(image, imsegs, vLabels, vConf, hLabels, hConf)
3
+ %
4
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
5
+ % Current Version: 1.0 09/30/2005
6
+ %image = image / double(max(image(:)));
7
+
8
+ scale = size(image, 1) / size(vLabels, 1);
9
+
10
+ image = rgb2hsv(image);
11
+ image(:, :, 2) = 0.0*image(:, :, 2);
12
+ image = hsv2rgb(image);
13
+
14
+ drawn = zeros(size(vLabels));
15
+
16
+ rad = round(max(size(image))/30);
17
+ markW = ceil(rad/10);
18
+ right_arrow = struct('x', 0, 'y', 0, 'angle', 0, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
19
+ up_arrow = struct('x', 0, 'y', 0, 'angle', 90, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
20
+ up_left_arrow = struct('x', 0, 'y', 0, 'angle', 135, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
21
+ up_right_arrow = struct('x', 0, 'y', 0, 'angle', 45, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
22
+ left_arrow = struct('x', 0, 'y', 0, 'angle', 180, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
23
+ down_arrow = struct('x', 0, 'y', 0, 'angle', 270, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
24
+ toward_arrow = struct('x', 0, 'y', 0, 'angle', 250, 'radius', rad*2/3, 'head_length', rad/3, 'head_base_angle', 60);
25
+
26
+ nsegs = imsegs.nseg;
27
+ sinds = APPgetSpIndsOld((1:nsegs), imsegs);
28
+ for s = 1:nsegs
29
+
30
+ ind = sinds(s).inds;
31
+
32
+ sat = max(min(vConf(s), 1), 0.33);
33
+
34
+ val = 1;
35
+ mix_rat = 0.5;
36
+ arrows = [];
37
+ hue = 0;
38
+ if strcmp(vLabels{s}, 'sky')
39
+ hue = 177/255;
40
+ elseif strcmp(vLabels{s}, '000')
41
+ hue = 112/255;
42
+ % elseif strcmp(vLabels{s}, 'rnd') | strcmp(hLabels{s}, 'rnd')
43
+ % hue = 226/255;
44
+ elseif strcmp(vLabels{s}, '045')
45
+ %hue = 45/255;
46
+ hue = 0/255;
47
+ elseif strcmp(vLabels{s}, '090')
48
+ hue = 0/255;
49
+ %hue = 0/255;
50
+ elseif strcmp(vLabels{s}, 'dst')
51
+ %hue = 80/255;
52
+ %sat = sat/2;
53
+ hue = 0/255;
54
+ else
55
+ disp(vLabels{s})
56
+ sat = 0;
57
+ end
58
+ %if strcmp(hLabels{s}, 'rnd') & (strcmp(vLabels{s}, '090') | strcmp(vLabels{s}, '045'))
59
+ % hue = 0/255;
60
+ % sat = sat/2;
61
+ %end
62
+
63
+
64
+
65
+ width = size(image, 2);
66
+ height = size(image, 1);
67
+
68
+ % fill in region with color determined by label
69
+ colors = hsv2rgb([hue sat val]);
70
+ label_colors = colors;
71
+
72
+ colors = reshape(colors, [1 1 3]);
73
+
74
+ y_ind = mod(ind-1, height)+1;
75
+ x_ind = floor((ind-1)/height)+1;
76
+ for s = 1:length(ind)
77
+ image(y_ind(s), x_ind(s), (1:3)) = (1-mix_rat)*image(y_ind(s), x_ind(s), :) + mix_rat*colors;
78
+ end
79
+
80
+ end
81
+ if 1
82
+
83
+ for x = rad:round(rad*1.51):(width-rad)
84
+ for y = rad:round(rad*1.51):(height-rad)
85
+
86
+ hlabel = hLabels{imsegs.segimage(y, x)};
87
+ vlabel = vLabels{imsegs.segimage(y, x)};
88
+
89
+ intensity = 0;
90
+ label_colors = [1 1 1];
91
+
92
+ arrows = [];
93
+ if strcmp(vlabel, '090') | strcmp(vlabel, '045')
94
+ if strcmp(hlabel, '045')
95
+ arrows = left_arrow;
96
+ elseif strcmp(hlabel, '090')
97
+ arrows = up_arrow;
98
+ elseif strcmp(hlabel, '135')
99
+ arrows = right_arrow;
100
+ elseif strcmp(hlabel, 'por')
101
+ image = draw_circle_image(image, x, y, rad/2, 0, markW);
102
+ elseif strcmp(hlabel, 'sol')
103
+ image = draw_x_image(image, x, y, rad, 0, markW);
104
+ end
105
+
106
+ end
107
+
108
+ % draw arrows
109
+ for a = 1:length(arrows)
110
+ arrows(a).x = x;
111
+ arrows(a).y = y;
112
+ end
113
+
114
+ image = draw_arrow_image(image, arrows, 0, markW);
115
+
116
+ end
117
+
118
+ end
119
+ end
120
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLabeledImage2.m ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function image = APPgetLabeledImage2(image, imsegs, pv, ph)
2
+ % image = APPgetLabeledImage2(image, imsegs, pv, ph)
3
+ %
4
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2006
5
+ % Current Version: 2.0 04/22/2006
6
+ %image = image / double(max(image(:)));
7
+
8
+ %grayim = rgb2gray(image);
9
+ image = rgb2hsv(image);
10
+ image(:, :, 2) = 0.0*image(:, :, 2);
11
+ image = hsv2rgb(image);
12
+
13
+ rad = round(max(size(image))/20);
14
+ markW = ceil(rad/10);
15
+ right_arrow = struct('x', 0, 'y', 0, 'angle', 0, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
16
+ up_arrow = struct('x', 0, 'y', 0, 'angle', 90, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
17
+ up_left_arrow = struct('x', 0, 'y', 0, 'angle', 135, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
18
+ up_right_arrow = struct('x', 0, 'y', 0, 'angle', 45, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
19
+ left_arrow = struct('x', 0, 'y', 0, 'angle', 180, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
20
+ down_arrow = struct('x', 0, 'y', 0, 'angle', 270, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
21
+ toward_arrow = struct('x', 0, 'y', 0, 'angle', 250, 'radius', rad*2/3, 'head_length', rad/3, 'head_base_angle', 60);
22
+
23
+ vhue = [112 0 177]/255;
24
+
25
+ if ~isstruct(imsegs)
26
+ tmp = imsegs;
27
+ clear imsegs;
28
+ imsegs.segimage = tmp;
29
+ end
30
+
31
+ if ndims(pv)==2
32
+ [vconf, vlab] = max(pv, [], 2);
33
+ [hconf, hlab] = max(ph, [], 2);
34
+
35
+ vlabim = vlab(imsegs.segimage);
36
+ hlabim = hlab(imsegs.segimage);
37
+ hconfim = hconf(imsegs.segimage);
38
+ %vconf = max((vconf - 0.5)*2, 0);
39
+ %vconf = max(vconf, 1);
40
+
41
+ hueim = vhue(vlabim);
42
+ satim = vconf(imsegs.segimage);
43
+ valim = ones(size(hueim));
44
+ else
45
+ [satim, vlabim] = max(pv, [], 3);
46
+ [hconfim, hlabim] = max(ph, [], 3);
47
+ hueim = vhue(vlabim);
48
+ valim = ones(size(hueim));
49
+ end
50
+
51
+ % set color
52
+ image = image*0.5 + 0.5*hsv2rgb(hueim, satim, valim);
53
+
54
+ % draw boundaries of main classes
55
+ % [gx, gy] = gradient(vlabim);
56
+ % g = gx.^2 + gy.^2;
57
+ % g = conv2(g, ones(2), 'same');
58
+ % edgepix = find(g>0);
59
+ % npix = numel(hueim);
60
+ % for b = 1:3
61
+ % image((b-1)*npix+edgepix) = 0;
62
+ % end
63
+
64
+
65
+
66
+ if 1 % set do draw subclass marks
67
+
68
+ %hconfim = max((hconf(imsegs.segimage)-0.5)*2, 0);
69
+
70
+ height = size(image, 1); width = size(image, 2);
71
+
72
+ for x = rad:round(rad*1.51):(width-rad)
73
+ for y = rad:round(rad*1.51):(height-rad)
74
+
75
+ %val = 1-hconfim(y, x);
76
+ val = [0 0 0];
77
+
78
+ arrows = [];
79
+ if vlabim(y, x)==2 % vertical
80
+ if hlabim(y, x)==1 % left
81
+ arrows = left_arrow;
82
+ elseif hlabim(y, x)==2 % center
83
+ arrows = up_arrow;
84
+ elseif hlabim(y, x)==3 % right
85
+ arrows = right_arrow;
86
+ elseif hlabim(y, x)==4 % porous
87
+ image = draw_circle_image(image, x, y, rad/2, val, markW);
88
+ elseif hlabim(y, x)==5 % solid
89
+ image = draw_x_image(image, x, y, rad, val, markW);
90
+ end
91
+
92
+ end
93
+
94
+ % draw arrows
95
+ for a = 1:length(arrows)
96
+ arrows(a).x = x;
97
+ arrows(a).y = y;
98
+ end
99
+
100
+ image = draw_arrow_image(image, arrows, val, markW);
101
+
102
+ end
103
+
104
+ end
105
+ end
106
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLabeledImage2.m~ ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function image = APPgetLabeledImage2(image, imsegs, pv, ph)
2
+ % image = APPgetLabeledImage2(image, imsegs, vLabels, vConf, hLabels, hConf)
3
+ %
4
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
5
+ % Permission granted to non-commercial enterprises for
6
+ % modification/redistribution under GNU GPL.
7
+ % Current Version: 2.0 04/22/2006
8
+ %image = image / double(max(image(:)));
9
+
10
+ %grayim = rgb2gray(image);
11
+ image = rgb2hsv(image);
12
+ image(:, :, 2) = 0.0*image(:, :, 2);
13
+ image = hsv2rgb(image);
14
+
15
+ rad = round(max(size(image))/25);
16
+ markW = ceil(rad/10);
17
+ right_arrow = struct('x', 0, 'y', 0, 'angle', 0, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
18
+ up_arrow = struct('x', 0, 'y', 0, 'angle', 90, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
19
+ up_left_arrow = struct('x', 0, 'y', 0, 'angle', 135, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
20
+ up_right_arrow = struct('x', 0, 'y', 0, 'angle', 45, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
21
+ left_arrow = struct('x', rad/2, 'y', 0, 'angle', 180, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
22
+ down_arrow = struct('x', 0, 'y', -rad/2, 'angle', 270, 'radius', rad, 'head_length', rad/2, 'head_base_angle', 30);
23
+ toward_arrow = struct('x', 0, 'y', 0, 'angle', 250, 'radius', rad*2/3, 'head_length', rad/3, 'head_base_angle', 60);
24
+
25
+ vhue = [112 0 177]/255;
26
+
27
+ [vconf, vlab] = max(pv, [], 2);
28
+ [hconf, hlab] = max(ph, [], 2);
29
+
30
+ vlabim = vlab(imsegs.segimage);
31
+ hlabim = hlab(imsegs.segimage);
32
+
33
+ %vconf = max((vconf - 0.5)*2, 0);
34
+ %vconf = max(vconf, 1);
35
+
36
+ hueim = vhue(vlabim);
37
+ satim = vconf(imsegs.segimage);
38
+ valim = ones(size(hueim));
39
+
40
+ % set color
41
+ image = image*0.5 + 0.5*hsv2rgb(hueim, satim, valim);
42
+
43
+ % draw boundaries of main classes
44
+ % [gx, gy] = gradient(vlabim);
45
+ % g = gx.^2 + gy.^2;
46
+ % g = conv2(g, ones(2), 'same');
47
+ % edgepix = find(g>0);
48
+ % npix = numel(hueim);
49
+ % for b = 1:3
50
+ % image((b-1)*npix+edgepix) = 0;
51
+ % end
52
+
53
+
54
+
55
+ if 1 % set do draw subclass marks
56
+
57
+ %hconfim = max((hconf(imsegs.segimage)-0.5)*2, 0);
58
+ hconfim = hconf(imsegs.segimage);
59
+
60
+ [height, width] = size(imsegs.segimage);
61
+
62
+ for x = rad:round(rad*1.51):(width-rad)
63
+ for y = rad:round(rad*1.51):(height-rad)
64
+
65
+ %val = 1-hconfim(y, x);
66
+ val = 0;
67
+
68
+ arrows = [];
69
+ if vlabim(y, x)==2 % vertical
70
+ if hlabim(y, x)==1 % left
71
+ arrows = left_arrow;
72
+ elseif hlabim(y, x)==2 % center
73
+ arrows = up_arrow;
74
+ elseif hlabim(y, x)==3 % right
75
+ arrows = right_arrow;
76
+ elseif hlabim(y, x)==4 % porous
77
+ image = draw_circle_image(image, x, y, rad/2, val, markW);
78
+ elseif hlabim(y, x)==5 % solid
79
+ image = draw_x_image(image, x, y, rad, val, markW);
80
+ end
81
+
82
+ end
83
+
84
+ % draw arrows
85
+ for a = 1:length(arrows)
86
+ arrows(a).x = x;
87
+ arrows(a).y = y;
88
+ end
89
+
90
+ image = draw_arrow_image(image, arrows, val, markW);
91
+
92
+ end
93
+
94
+ end
95
+ end
96
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetLargeConnectedEdges.m ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [lines, spdata, edgeIm] = APPgetLargeConnectedEdges(grayIm, minLen, imsegs)
2
+ % [lines, spdata, edgeIm] = APPgetLargeConnectedEdges(grayIm, minLen,
3
+ % imsegs)
4
+ %
5
+ % Uses the method of Video Compass [Kosecka, et al 2002] to get long,
6
+ % straight edges.
7
+ %
8
+ % Input:
9
+ % grayIm: grayscale image to be analyzed
10
+ % minLen: minimum length in pixels for edge (suggested 0.025*diagonal)
11
+ % imsegs: superpixel image structure (used to store sp statistics)
12
+ % Output:
13
+ % lines: parameters for long, straight lines
14
+ % spdata: statistics for lines in each superpixel
15
+ % edgeIm (optional): for displaying lines found
16
+ %
17
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
18
+ % Current Version: 1.0 09/30/2005
19
+
20
+
21
+ [dX, dY] = gradient(conv2(grayIm, fspecial('gaussian', 7, 1.5), 'same'));
22
+
23
+ im_canny = edge(grayIm, 'canny');
24
+ % remove border edges
25
+ im_canny([1 2 end-1 end], :) = 0;
26
+ im_canny(:, [1 2 end-1 end]) = 0;
27
+ width = size(im_canny, 2);
28
+ height = size(im_canny, 1);
29
+
30
+ ind = find(im_canny > 0);
31
+
32
+ num_dir = 8;
33
+
34
+ dX_ind = dX(ind);
35
+ dY_ind = dY(ind);
36
+ a_ind = atan(dY_ind ./ (dX_ind+1E-10));
37
+
38
+ % a_ind ranges from 1 to num_dir with bin centered around pi/2
39
+ a_ind = ceil(mod(a_ind/pi*num_dir-0.5, num_dir));
40
+ %[g, gn] = grp2idx(a_ind);
41
+
42
+ % get the indices of edges in each direction
43
+ for i = 1:num_dir
44
+ direction(i).ind = ind(find(a_ind==i));
45
+ end
46
+
47
+
48
+ % remove edges that are too small and give all edges that have the same
49
+ % direction a unique id
50
+ % edges(height, width, [angle id])
51
+ if nargout>2
52
+ edgeIm = grayIm*0.75;
53
+ end
54
+ lines = zeros(2000, 6);
55
+
56
+ nspdata = length(imsegs.npixels);
57
+ spdata = repmat(struct('lines', zeros(5, 1), 'edge_count', 0), nspdata,1);
58
+ bcount = zeros(nspdata, 1);
59
+
60
+ used = zeros(size(im_canny));
61
+
62
+ line_count = 0;
63
+ for k = 1:num_dir
64
+
65
+ num_ind = 0;
66
+ for m = (k-1):k+1
67
+ num_ind = num_ind + sum(~used(direction(mod(m-1, num_dir)+1).ind));
68
+ end
69
+
70
+ ind = zeros(num_ind, 1);
71
+ dir_im = zeros(size(im_canny));
72
+
73
+ count = 0;
74
+ for m = (k-1):k+1
75
+ m2 = mod(m-1, num_dir)+1;
76
+ tind = direction(m2).ind(find(~used(direction(m2).ind)));
77
+ tmpcount = length(tind);
78
+ ind(count+1:count+tmpcount) = tind;
79
+ count = count + tmpcount;
80
+ end
81
+ dir_im(ind) = 1;
82
+
83
+ [tmpL, num_edges] = bwlabel(dir_im, 8);
84
+
85
+ % get the number of pixels in each edge
86
+ edge_size = zeros(num_edges, 1);
87
+ edges = repmat(struct('ind', zeros(200, 1)), num_edges, 1);
88
+ for i = 1:length(ind)
89
+ id = tmpL(ind(i));
90
+ edge_size(id) = edge_size(id) + 1;
91
+ edges(id).ind(edge_size(id)) = ind(i);
92
+ end
93
+ for i = 1:num_edges
94
+ edges(i).ind = edges(i).ind(1:edge_size(i));
95
+ end
96
+
97
+ % get the endpoints of the long edges and an image of the long edges
98
+ for id = 1:num_edges
99
+ if edge_size(id) > minLen
100
+
101
+ y = mod(edges(id).ind-1, height)+1;
102
+ x = floor((edges(id).ind-1) / height)+1;
103
+
104
+ %by = min(floor(y/block_size), ny-1);
105
+ %bx = min(floor(x/block_size), nx-1);
106
+
107
+ mean_x = mean(x);
108
+ mean_y = mean(y);
109
+ zmx = (x-mean_x);
110
+ zmy = (y-mean_y);
111
+ D = [sum(zmx.^2) sum(zmx.*zmy); sum(zmx.*zmy) sum(zmy.^2)];
112
+ [v, lambda] = eig(D);
113
+ theta = atan2(v(2, 2) , v(1, 2));
114
+ if lambda(1,1)>0
115
+ conf = lambda(2,2)/lambda(1,1);
116
+ else
117
+ conf = 100000;
118
+ end
119
+
120
+ %disp(conf)
121
+
122
+ if conf >= 400
123
+ line_count = line_count+1;
124
+
125
+ used(edges(id).ind) = 1;
126
+ bi = double(imsegs.segimage(edges(id).ind));
127
+ [g, gn] = grp2idx(bi);
128
+ for k = 1:length(bi)
129
+ if bi(k) > 0
130
+ spdata(bi(k)).edge_count = spdata(bi(k)).edge_count + 1;
131
+ end
132
+ end
133
+ for k = 1:length(gn)
134
+ tmpbi = str2num(gn{k});
135
+ if tmpbi>0
136
+ bcount(tmpbi) = bcount(tmpbi)+1;
137
+ spdata(tmpbi).lines(bcount(tmpbi)) = line_count;
138
+ end
139
+ end
140
+
141
+ %disp(num2str([lambda(1,1) lambda(2,2)]))
142
+ r = sqrt((max(x)-min(x))^2 + (max(y)-min(y))^2);
143
+ x1 = mean_x - cos(theta)*r/2;
144
+ x2 = mean_x + cos(theta)*r/2;
145
+ y1 = mean_y - sin(theta)*r/2;
146
+ y2 = mean_y + sin(theta)*r/2;
147
+
148
+ r = mean_x*cos(theta)+mean_y*sin(theta);
149
+ %tr = x1*cos(theta) + y1*sin(theta);
150
+ %disp(num2str([r tr]))
151
+
152
+ lines(line_count, 1:6) = [x1 x2 y1 y2 theta r];
153
+
154
+ end
155
+ end
156
+ end
157
+
158
+ end
159
+
160
+
161
+ if nargout>2
162
+ for i = 1:line_count
163
+ edgeIm = draw_line_image2(edgeIm, lines(i, 1:4)', i);
164
+ end
165
+ end
166
+
167
+ for k = 1:length(spdata)
168
+ spdata(k).lines = spdata(k).lines(1:bcount(k))';
169
+ end
170
+ lines = lines(1:line_count, :);
171
+
172
+ imsize = size(grayIm);
173
+ lines = normalizeLines(lines, imsize(1:2));
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetPairwiseSuperpixelLikelihoods.m ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function Z = APPgetPairwiseSuperpixelLikelihoods(features, density)
2
+ % Z = APPgetPairwiseSuperpixelLikelihoods(features, density)
3
+ % Gets the matrix of pairwise sp likelihoods
4
+
5
+ nElements = size(features, 1);
6
+ nFeatures = size(features, 2);
7
+ nDist = nElements*(nElements-1)/2;
8
+
9
+ % Y contains all pairwise likelihoods of being in same cluster
10
+ Y = zeros(nDist, 1);
11
+ count = 0;
12
+ for f = 1:nFeatures
13
+ tmpY = pdist(features(:, f), 'cityblock');
14
+ % minus used because log ratio is of P(x|+)/P(x|-), but we want inverse
15
+ %% NOW plus used for similarity
16
+ Y = Y + getKdeLikelihood(density(f).log_ratio, density(f).x, tmpY);
17
+ end
18
+
19
+ Z = squareform(Y');
20
+ Z = Z.*(ones(nElements, nElements)-eye(nElements));
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetRegionFeatures.m ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function regionFeatures = APPgetRegionFeatures(image, imsegs, currMap, ...
2
+ regionNums, spdata, vpdata)
3
+ % regionFeatures = APPgetRegionFeatures(image, imsegs, currMap, ...
4
+ % regionNums, spdata, vpdata)
5
+ % Computes features for segments given by currMap
6
+ %
7
+ % Input:
8
+ % image: image to be analyzed
9
+ % imsegs: sp structure
10
+ % currMap: maps sp to regions
11
+ % regionNums: indices of regions to be analyzed
12
+ % spdata: sp data structure from APPgetSpData
13
+ % vpdata: vanishing point data structure
14
+ % Output:
15
+ % regionFeatures(nRegions, nFeatures): region features
16
+ %
17
+ % Assuming there are 12 oriented filters and 12 texton filters the
18
+ % features are as follows:
19
+ % 1-12: mean abs oriented filter responses (ofr)
20
+ % 13: edginess - mean of all ofrs
21
+ % 14: index of largest ofr
22
+ % 15: dominance of largest response - max - median of ofr
23
+ % textons (would be 16-29) not often used
24
+ % 16-27: mean abs texton filter response (tfr)
25
+ % 28: index of largest tfr
26
+ % 29: dominance of largest tfr (max - med)
27
+ % 16-18: red, green, blue means
28
+ % 19-21: hue, saturation, value means (rgb2hsv)
29
+ % 22-23: y location, x location means
30
+ % 24-29: hue - histogram (5 bins), entropy
31
+ % 30-33: sat - histogram (3 bins), entropy
32
+ % 34-37: location - 10% and 90% (percentiles) y and x
33
+ % 38-39: edginess center y and x
34
+ % 40: number of super-pixels in segmentation
35
+ % 41: % of image area region takes up
36
+ % 42: number of sides in convex hull polygon
37
+ % 43: (num pixels) / (area of convex hull polygon)
38
+ % 44: whether the region is contiguous
39
+ % 45-60: vanishing point features (see vp2regionfeatures)
40
+ % 61-62: 10% and 90% y wrt horizon
41
+ % 63 : 0-> below horizon, 1-> straddles horizon, 2-> above horizon
42
+ % 64-82: older vanishing point features (see lines_to_vp_info3)
43
+ % discrete: 14 44 60 63
44
+ %
45
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
46
+ % Current Version: 1.0 09/30/2005
47
+
48
+ % WARNING: not using texton features
49
+
50
+ num_clusters = length(regionNums);
51
+
52
+ regionFeatures = zeros(num_clusters, 82);
53
+
54
+ num_angles = size(spdata.orientation,2);
55
+ num_textons = 0;
56
+
57
+ spfeatures = spdata.features; % superpixel features
58
+ pixelp = imsegs.npixels ./ sum(imsegs.npixels);
59
+ sporient = spdata.orientation;
60
+
61
+ height = size(image, 1);
62
+ width = size(image, 2);
63
+
64
+ hue = spdata.hue;
65
+ sat= spdata.sat;
66
+ yim = 1-repmat([(0:height-1)/(height-1)]', 1, width);
67
+ xim = repmat([(0:width-1)/(width-1)], height, 1);
68
+
69
+ cinds = APPspInds2regionInds(currMap, imsegs.seginds);
70
+ edgeim = spdata.edginess;
71
+
72
+ for c = 1:num_clusters
73
+
74
+ spind = find(currMap==regionNums(c));
75
+ cpixelp = pixelp(spind);
76
+ %cpixelp = cpixelp / sum(cpixelp); % line added 02/20/06
77
+
78
+ % features consisting of mean block responses
79
+
80
+ % mean orientation filter responses
81
+ rorient = zeros(num_angles, 1);
82
+ for a = 1:num_angles
83
+ regionFeatures(c, a) = sum(spfeatures(spind, a).*cpixelp);
84
+ rorient(a) = sum(sporient(spind, a).*cpixelp);
85
+ end
86
+ % mean edginess (overall response)
87
+ regionFeatures(c, num_angles+1) = sum(spfeatures(spind, num_angles+1).*cpixelp);
88
+ % most dominant filter response
89
+ [tmp, regionFeatures(c, num_angles+2)] = max(rorient);
90
+ % dominance of largest filter (max - median)
91
+ regionFeatures(c, num_angles+3) = max(rorient) - median(rorient);
92
+ % mean texton response
93
+ for t = 1:num_textons
94
+ regionFeatures(c, num_angles+3+t) = sum(spfeatures(spind, num_angles+t).*cpixelp);
95
+ end
96
+ nf = num_angles+3+num_textons;
97
+ if num_textons>0
98
+ % most dominant texton response
99
+ [tmp, regionFeatures(c, nf+1)] = max(regionFeatures(c, num_angles+3+(1:num_textons)));
100
+ % dominance of largest texton reponse
101
+ regionFeatures(c, nf+2) = regionFeatures(c, nf+1) - median(regionFeatures(c, num_angles+3+(1:num_textons)));
102
+ nf = nf +2;
103
+ end
104
+
105
+ % rgb means
106
+ for b = 1:3
107
+ regionFeatures(c, nf+b) = sum(spfeatures(spind, nf+2+b).*cpixelp);
108
+ end
109
+ % hsv
110
+ regionFeatures(c, nf+(4:6)) = rgb2hsv(regionFeatures(c, nf+(1:3)));
111
+
112
+ % y and x locs
113
+ regionFeatures(c, nf+7) = sum(spfeatures(spind, nf+7).*cpixelp);
114
+ regionFeatures(c, nf+8) = sum(spfeatures(spind, nf+8).*cpixelp);
115
+
116
+ % get the values that pertain to this region
117
+ rhue = hue(cinds{c});
118
+ rsat = sat(cinds{c});
119
+ rx = xim(cinds{c});
120
+ ry = yim(cinds{c});
121
+ redge = edgeim(cinds{c});
122
+ npix = length(cinds{c});
123
+
124
+ % hue and sat histograms
125
+ nf = nf+8;
126
+ hue_histogram = (hist(rhue, [0.1:0.2:0.9]) + 0.01)/(length(rhue)+0.05);
127
+ regionFeatures(c, nf+(1:5)) = hue_histogram / sum(hue_histogram);
128
+ regionFeatures(c, nf+6) = -1*sum(hue_histogram.*log(hue_histogram)) / log(length(hue_histogram));
129
+ sat_histogram = (hist(rsat, [0.167:0.333:0.833]) + 0.01)/(length(rsat)+0.03);
130
+ regionFeatures(c, nf+(7:9)) = sat_histogram / sum(sat_histogram);
131
+ regionFeatures(c, nf+10) = -1*sum(sat_histogram.*log(sat_histogram)) / log(length(sat_histogram));
132
+
133
+ % location - 10% and 90% percentiles of y and x
134
+ sorty = sort(ry);
135
+ regionFeatures(c, nf+11) = sorty(ceil(npix/10));
136
+ regionFeatures(c, nf+12) = sorty(ceil(9*npix/10));
137
+ sortx = sort(rx);
138
+ regionFeatures(c, nf+13) = sortx(ceil(npix/10));
139
+ regionFeatures(c, nf+14) = sortx(ceil(9*npix/10));
140
+
141
+ % center of edginess y and x
142
+ if regionFeatures(c, nf+11)==regionFeatures(c, nf+12)
143
+ regionFeatures(c, nf+15) = 0.5;
144
+ else
145
+ center_y = sum(redge.*ry) / sum(redge);
146
+ regionFeatures(c, nf+15) = sum((ry < center_y).*ry)/sum(ry);
147
+ end
148
+ if regionFeatures(c, nf+13)==regionFeatures(c, nf+14)
149
+ regionFeatures(c, nf+16) = 0.5;
150
+ else
151
+ center_x = sum(redge.*rx) / sum(redge);
152
+ regionFeatures(c, nf+16) = sum((rx < center_x).*rx)/sum(rx);
153
+ end
154
+
155
+ % num superpixels, % of image area
156
+ regionFeatures(c, nf+17) = length(spind);
157
+ regionFeatures(c, nf+18) = npix / width / height;
158
+
159
+ % polygon: num sides, area ratio
160
+ try
161
+ [polyi, polya] = convhull(rx, ry);
162
+ regionFeatures(c, nf+19) = length(polyi)-1;
163
+ regionFeatures(c, nf+20) = npix / (polya*width*height);
164
+ catch
165
+ regionFeatures(c, nf+(19:20)) = [4 0.75];
166
+ end
167
+
168
+ % whether contiguous
169
+ regionFeatures(c, nf+21) = 1;
170
+ for s = 1:length(spind)
171
+ isadj = imsegs.adjmat(setdiff(spind, spind(s)), spind(s));
172
+ if sum(isadj) == 0
173
+ regionFeatures(c, nf+21) = 0;
174
+ break;
175
+ end
176
+ end
177
+
178
+ % vanishing point features
179
+ nf = nf + 21;
180
+ region_center = [sorty(ceil(npix/2)) sortx(ceil(npix/2))];
181
+
182
+ rbounds = [sortx(1) sortx(end) 1-sorty(end) 1-sorty(1)];
183
+ regionFeatures(c, nf+(1:16)) = ...
184
+ APPvp2regionFeatures(spind, vpdata, region_center, rbounds, imsegs);
185
+
186
+ % y-location with respect to estimated horizon
187
+ if ~isnan(vpdata.hpos)
188
+ % location - 10% and 90% percentiles of y and x
189
+ regionFeatures(c, nf+17) = regionFeatures(c, 48) - (1-vpdata.hpos); % bottom 10 pct wrt horizon
190
+ regionFeatures(c, nf+18) = regionFeatures(c, 49) - (1-vpdata.hpos); % top 10 pct wrt horizon
191
+ % 1 -> completely under horizon, 2-> straddles horizon, 3-> completely above horizon
192
+ regionFeatures(c, nf+19) = (regionFeatures(c, nf+20)>0) + (regionFeatures(c, nf+21)>0) + 1;
193
+ else % horizon was not estimated with high confidence
194
+ regionFeatures(c, nf+(17:18)) = regionFeatures(c, [48:49])-0.5;
195
+ regionFeatures(c, nf+19) = 4; % signifies no data-estimated horizon
196
+ end
197
+
198
+ [h w] = size(image);
199
+ region_center(1) = region_center(1) - 0.5;
200
+ region_center(2) = region_center(2) - 0.5;
201
+ regionFeatures(c, nf+(20:38)) = ...
202
+ APPgetVpFeatures(vpdata.spinfo(spind), vpdata.lines, region_center, [h w]);
203
+
204
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSegmentInds.m ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ function sinds = APPgetSegmentInds(imsegs);
2
+ % Gets pixel indices for each superpixel in imsegs
3
+
4
+ ns = imsegs.nseg;
5
+ sinds = cell(ns, 1);
6
+ for s = 1:ns
7
+ sinds{s} = find(imsegs.segimage==s);
8
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpData.m ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function spdata = APPgetSpData(image, doogFilters, textonFilters, imsegs)
2
+ % spdata = APPgetSpData(image, doogFilters, textonFilters, imsegs)
3
+ %
4
+ % Gets the features and data corresponding to the superpixels given by
5
+ % imsegs.
6
+ % features:
7
+ % 1-12: mean absolute filter response of diff of oriented Gaussian filters
8
+ % 13: mean of 1-12
9
+ % 14: argmax of 1-12
10
+ % 15: max-median of 1-12
11
+ % possibly texton features (15+1:15+nTextons+2)
12
+ % 16-18: mean rgb values
13
+ % 19-21: hsv conversion from mean rgb values
14
+ % 22-23: mean x and y locations
15
+ %
16
+ % Input:
17
+ % image: rgb image to be analyzed
18
+ % doogFilters: filters for texture (could be empty)
19
+ % textonFilters: filters for texture (could be empty)
20
+ % imsegs: superpixel structure
21
+ % Output:
22
+ % spdata: structure for data corresponding to each superpixel
23
+ %
24
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
25
+ % Current Version: 1.0 09/30/2005
26
+
27
+ nAngles = size(doogFilters, 3);
28
+
29
+ % compute texture responses
30
+ grayim = rgb2gray(image);
31
+
32
+ %disp(num2str(size(image, 1)*size(image, 2)*nAngles /1024/1024*8))
33
+ orientationImages = zeros(size(image, 1), size(image, 2), nAngles);
34
+ % get and record the filter responses for each angle
35
+ for (i=1:nAngles)
36
+ orientationImages(:, :, i) = abs(conv2(grayim,doogFilters(:, :, i),'same')-grayim);
37
+ end
38
+ %spdata.orientationImages = orientationImages;
39
+
40
+ textonImages = compute_texton_response(grayim, textonFilters);
41
+ nTextons = numel(textonFilters);
42
+
43
+ [height, width, nb] = size(image);
44
+
45
+ % features: angle response peaks, mean angle response, angle with
46
+ % largest response, dominance of largest filter, mean abs texton
47
+ % repsonses, largest texton response, dominance of largest response,
48
+ % mean of rgb hsv x y
49
+ nfeatures = nAngles + 3 + nTextons + 6 + 2;
50
+ if nTextons > 0
51
+ nfeatures = nfeatures + 2;
52
+ end
53
+
54
+ % for each segmentation
55
+ for i = 1:length(imsegs)
56
+
57
+ nseg = imsegs(i).nseg;
58
+ spdata(i).npixels = imsegs(i).npixels;
59
+ spdata(i).adjmat = imsegs(i).adjmat;
60
+
61
+ features = zeros(nseg, nfeatures);
62
+
63
+ % oriented filters
64
+ maar = zeros(nseg, nAngles);
65
+ for a = 1:nAngles
66
+ maar(:, a) = APPgetSpMeans(imsegs(i), orientationImages(:, :, a));
67
+ end
68
+ features(:, 1:nAngles) = maar;
69
+ features(:, nAngles+1) = mean(maar, 2);
70
+ [maxval, features(:, nAngles+2)] = max(maar, [], 2);
71
+ features(:, nAngles+3) = maxval - median(maar, 2);
72
+ spdata(i).orientation = maar;
73
+ spdata(i).edginess = mean(orientationImages, 3);
74
+ clear orientationImages;
75
+
76
+ % textons
77
+ cf = nAngles+3;
78
+ for t = 1:nTextons
79
+ features(:, cf+t) = APPgetSpMeans(imsegs(i), textonImages(:, :, t));
80
+ end
81
+ cf = cf + nTextons;
82
+ if nTextons>0
83
+ [maxval, features(:, cf+1)] = max(features(:, cf+1:cf+nTextons), [], 2);
84
+ features(:, cf+2) = maxval - median(features(:, cf+1:cf+nTextons), 2);
85
+ clear textonImages;
86
+ cf = cf + 2;
87
+ end
88
+
89
+ % color
90
+ rgb = zeros(nseg, 3);
91
+ for b = 1:3
92
+ rgb(:, b) = APPgetSpMeans(imsegs(i), image(:, :, b));
93
+ end
94
+ features(:, cf + (1:3)) = rgb;
95
+ features(:, cf + (4:6)) = rgb2hsv(rgb);
96
+
97
+ % location
98
+ cf = cf + 6;
99
+ yim = 1-repmat(((0:height-1)/(height-1))', 1, width);
100
+ xim = repmat(((0:width-1)/(width-1)), height, 1);
101
+ features(:, cf+1) = APPgetSpMeans(imsegs(i), yim);
102
+ features(:, cf+2) = APPgetSpMeans(imsegs(i), xim);
103
+ spdata(i).features = features;
104
+ end
105
+
106
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpInds.m ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ function sinds = APPgetSpInds(imsegs);
2
+ % Gets pixel indices for each superpixel in imsegs
3
+
4
+ ns = imsegs.nseg;
5
+ sinds = cell(ns, 1);
6
+ for s = 1:ns
7
+ sinds{s} = find(imsegs.segimage==s);
8
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpIndsOld.m ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function cinds = APPgetSpIndsOld(map, imsegs);
2
+ % Get pixel indices for each superpixel
3
+
4
+ nc = max(map(:));
5
+ cinds(nc) = struct('inds', [], 'count', 0);
6
+
7
+ t1 = cputime;
8
+ for c = 1:nc
9
+ spind = find(map==c);
10
+ nind = sum(imsegs.npixels(spind));
11
+ cinds(c).inds = zeros(nind, 1);
12
+ cinds(c).count = 0;
13
+ end
14
+
15
+ t2 = cputime;
16
+ nseg = imsegs.nseg;
17
+ for k = 1:nseg
18
+ ind = find(imsegs.segimage==k);
19
+ c = map(k);
20
+ cinds(c).inds(cinds(c).count+1:cinds(c).count+length(ind)) = ind;
21
+ cinds(c).count = cinds(c).count + length(ind);
22
+ end
23
+
24
+ for c = 1:length(cinds)
25
+ cinds(c).inds = cinds(c).inds(1:cinds(c).count);
26
+ end
27
+ %segimage = imsegs.segimage;
28
+ %for k = 1:numel(segimage)
29
+ % c = map(segimage(k));
30
+ % cinds(c).count = cinds(c).count + 1;
31
+ % cinds(c).inds(cinds(c).count) = k;
32
+ %end
33
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpMeans.m ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function smeans = APPgetSpMeans(imseg, values)
2
+ % smeans = APPgetSpMeans(imseg, values)
3
+ %
4
+ % Computes the mean of values for each superpixel in imseg.
5
+ %
6
+ % imseg.{npixels(nseg), segimage, nseg} - the segmentation information
7
+ % values - a matrix of values
8
+ % smeans(nseg, 1) - the means of values for each segment
9
+
10
+ smeans = zeros(imseg.nseg, 1);
11
+ count = zeros(imseg.nseg, 1);
12
+ segimage = imseg.segimage;
13
+ nelements = numel(segimage);
14
+ for n = 1:nelements
15
+ s = segimage(n);
16
+ if s~=0
17
+ smeans(s) = smeans(s) + values(n);
18
+ count(s) = count(s) + 1;
19
+ end
20
+ end
21
+ smeans = smeans ./ (count+ (count==0));
22
+
23
+
24
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetSpStats.m ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function imsegs = APPgetSpStats(imsegs)
2
+ % imsegs = APPgetSpStats(imsegs)
3
+ % Gets basic information about the superpixels
4
+ %
5
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
6
+ % Current Version: 1.0 09/30/2005
7
+
8
+ for ii = 1:length(imsegs)
9
+
10
+ nseg = imsegs(ii).nseg;
11
+ segimage = imsegs(ii).segimage;
12
+
13
+ imh = size(segimage, 1);
14
+
15
+ adjmat = eye([nseg nseg], 'uint8');
16
+
17
+ % get adjacency
18
+ dx = uint8(segimage ~= segimage(:,[2:end end]));
19
+ dy = segimage ~= segimage([2:end end], :);
20
+
21
+ ind1 = find(dy);
22
+ ind2 = ind1 + 1;
23
+ s1 = segimage(ind1);
24
+ s2 = segimage(ind2);
25
+ adjmat(s1 + nseg*(s2-1)) = 1;
26
+ adjmat(s2 + nseg*(s1-1)) = 1;
27
+
28
+ ind3 = find(dx);
29
+ ind4 = ind3 + imh;
30
+ s3 = segimage(ind3);
31
+ s4 = segimage(ind4);
32
+ adjmat(s3 + nseg*(s4-1)) = 1;
33
+ adjmat(s4 + nseg*(s3-1)) = 1;
34
+
35
+
36
+ % slower code
37
+ % [height, width] = size(segimage);
38
+ %
39
+ % for y = 1:height-1
40
+ % for x = 1:width-1
41
+ % s1 = segimage(y, x);
42
+ % s2 = segimage(y+1, x);
43
+ % s3 = segimage(y, x+1);
44
+ % if s1 > 0
45
+ % npixels(s1) = npixels(s1) + 1;
46
+ % if s2 > 0
47
+ % adjmat(s1, s2) = 1;
48
+ % adjmat(s2, s1) = 1;
49
+ % end
50
+ % if s3 > 0
51
+ % adjmat(s1, s3) = 1;
52
+ % adjmat(s3, s1) = 1;
53
+ % end
54
+ % end
55
+ % end
56
+ % end
57
+ %
58
+ % x = width;
59
+ % for y = 1:height
60
+ % s1 = segimage(y, x);
61
+ % if s1 > 0
62
+ % npixels(s1) = npixels(s1) + 1;
63
+ % end
64
+ % end
65
+ %
66
+ % y = height;
67
+ % for x = 1:width-1
68
+ % s1 = segimage(y, x);
69
+ % if s1 > 0
70
+ % npixels(s1) = npixels(s1) + 1;
71
+ % end
72
+ % end
73
+
74
+ stats = regionprops(segimage, 'Area');
75
+ imsegs(ii).npixels = vertcat(stats(:).Area);
76
+ imsegs(ii).adjmat = logical(adjmat);
77
+
78
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPgetVpFeatures.m ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function features = APPgetVpFeatures(spinfo, lines, rCenter, imsize)
2
+ % Get the vanishing point features for superpixels defined by spinfo
3
+ % spinfo(nSp).{lines(nLinesInSp)}
4
+ % rCenter([y x]): region center wrt image center (-0.5 to 0.5)
5
+ %
6
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
7
+ % Current Version: 1.0 09/30/2005
8
+
9
+ features = zeros(1, 19);
10
+
11
+ num_blocks = length(spinfo);
12
+ used_lines = zeros(size(lines, 1), 1);
13
+ max_angle = pi/8;
14
+
15
+ for i = 1:num_blocks
16
+ used_lines(spinfo(i).lines) = 1;
17
+ end
18
+ lines = lines(find(used_lines>0), :);
19
+ num_lines = size(lines, 1);
20
+
21
+ features(:) = APPlines2vpFeatures(lines, max_angle);
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPimages2superpixels.m ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function imsegs = APPimages2superpixels(input_dir, ext, gt)
2
+ % imsegs = APPimages2superpixels(input_dir, ext, gt)
3
+ % Create the imsegs structure from a segmentation image
4
+ %
5
+ % INPUT:
6
+ % input_dir - The directory of segmentation images. Segments are denoted by
7
+ % different colors.
8
+ % ext - the extension of the segmentation image filenames
9
+ % gt - an existing imsegs structure (or [] if none exists)
10
+ % OUTPUT:
11
+ % imsegs - image segmentation data (and sometimes ground truth)
12
+ %
13
+ % Example: imsegs = APPimages2superpixels('../images', 'pnm', [])
14
+ %
15
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
16
+ % Current Version: 1.0 09/30/2005
17
+
18
+ if isempty(gt)
19
+ files = dir([input_dir '/*.' ext]);
20
+ for f = 1:length(files)
21
+ gt(f).image_name = files(f).name;
22
+ end
23
+ end
24
+
25
+
26
+ imsegs(length(gt)) = struct('imname', '', 'imsize', [0 0]);
27
+ for f = 1:length(gt)
28
+ imname = gt(f).image_name;
29
+ disp(imname)
30
+ basename = strtok(imname, '.');
31
+ im = imread([input_dir '/' basename '.' ext]);
32
+
33
+ im = double(im);
34
+
35
+ imsegs(f).imname = [strtok(imname,'.') '.jpg'];
36
+ imsegs(f).imsize = size(im);
37
+ imsegs(f).imsize = imsegs(f).imsize(1:2);
38
+ im = im(:, :, 1) + im(:, :, 2)*256 + im(:, :, 3)*256^2;
39
+ [gid, gn] = grp2idx(im);
40
+ imsegs(f).segimage = uint16(reshape(gid, imsegs(f).imsize));
41
+ imsegs(f).nseg = length(gn);
42
+
43
+ end
44
+ imsegs = APPgetSpStats(imsegs);
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPlines2vpFeatures.m ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function data = APPlines2vpFeatures(lines, max_angle)
2
+ % APPlines2vpFeatures(lines, max_angle)
3
+ % Computes vanishing point related features from straight lines
4
+ % data is currently 19 elements, line histograms not used
5
+ %
6
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
7
+ % Current Version: 1.0 09/30/2005
8
+
9
+ nbins=0;
10
+ data = zeros(nbins+19, 1);
11
+
12
+ nLines = size(lines, 1);
13
+
14
+ if nLines <= 1
15
+ data(nbins+(1:2)) = 0.5;
16
+ data(nbins+3) = 0;
17
+ data(nbins +(4:10)) = 0;
18
+ return;
19
+ end
20
+
21
+ a = lines(:, 5);
22
+ x1 = lines(:, 1);
23
+ y1 = lines(:, 3);
24
+ x2 = lines(:, 2);
25
+ y2 = lines(:, 4);
26
+ r = lines(:, 6);
27
+
28
+ slength = sqrt((x2-x1).^2 + (y2-y1).^2);
29
+
30
+ xc = (x1+x2)/2;
31
+ yc = (y1+y2)/2;
32
+
33
+ m = (y2-y1)./(x2-x1+1E-10);
34
+
35
+ a1 = atan(tan(a));
36
+
37
+
38
+ adist = pdist(a, 'cityblock');
39
+ %adist = mod(adist, pi/2) < max_angle;
40
+ adist = adist < max_angle;
41
+ adistM = squareform(adist);
42
+
43
+
44
+ % get |m1-m2|/|x1-x2|, where the direction of x is perpendicular to m
45
+ parabins = zeros(8, 1);
46
+ paracount = zeros(8, 1);
47
+ parabins2 = zeros(8, 1);
48
+ leftcount = 0.05;
49
+ rightcount = 0.05;
50
+ upcount = 0.05;
51
+ downcount = 0.05;
52
+ histx = [(-pi/2):(pi/8):(pi/2)]-pi/16;
53
+ for i = 1:nLines
54
+ bn = sum(a1(i) > histx);
55
+ if bn==9
56
+ bn = 1;
57
+ end
58
+
59
+ m1 = m(i); x1 = xc(i); y1 = yc(i);
60
+ %disp(num2str([x1 y1]))
61
+ isvert = abs(sin(a(i))) > abs(cos(a(i)));
62
+ ishorz = abs(cos(a(i))) > abs(sin(a(i)));
63
+ for j = i+1:nLines
64
+ if adistM(i, j)
65
+ sl = min(slength([i j]));
66
+
67
+ m2 = m(j); x2 = xc(j); y2 = yc(j);
68
+
69
+ if ~(m1==m2 & (y2-y1)==m1*(x2-x1)) % check for colinearity
70
+
71
+ xi = (y2-y1+m1*x1-m2*x2)/(m1-m2+1E-10);
72
+ yi = (-y1*m2+m1*y2-m1*m2*x2+m1*x1*m2)/(m1-m2+1E-10);
73
+
74
+ if sqrt(xi.^2+yi.^2) > 1 | m1==m2
75
+ parabins(bn) = parabins(bn) + sl;
76
+ end
77
+ paracount(bn) = paracount(bn) + sl;
78
+
79
+ if sqrt(xi.^2+yi.^2) > 3.5 | m1==m2
80
+ parabins2(bn) = parabins2(bn) + sl;
81
+ end
82
+
83
+ if isvert
84
+ if yi > (y1+y2)/2
85
+ upcount = upcount + sl;
86
+ else
87
+ downcount = downcount + sl;
88
+ end
89
+ else
90
+ if xi > (x1+x2)/2
91
+ rightcount = rightcount + sl;
92
+ else
93
+ leftcount = leftcount + sl;
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ ind = find(paracount>0);
102
+ parabins(ind) = parabins(ind) ./ paracount(ind);
103
+ parabins2(ind) = parabins2(ind) ./ paracount(ind);
104
+ %data(1:nbins) = angle_hist; % extent of different vanishing point directions
105
+ %data(nbins+1) = angle_entropy; % measure of how many different vanishing points there may be
106
+ data(1) = rightcount ./ (leftcount+rightcount); % differentiate between face right and face left
107
+ data(2) = upcount ./ (upcount+downcount); % differentiate between face up and face down
108
+ %data(3) = sum(slength); % total number of line pixels in region
109
+ data(3) = mean(adist); % percent of pairs of angles that are roughly parallel
110
+ data(4:11) = parabins; % measure of "parallelness" at different angles
111
+ data(12:19) = parabins2;
112
+
113
+ %disp(['bins: ' num2str(data(1:nbins)')]);
114
+ %disp(['rand: ' num2str(data(nbins+1:nbins+5)')])
115
+ %disp(['para: ' num2str(data(nbins+6:end)')])
116
+
117
+
118
+
119
+
120
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPsp2regions.m ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [maps, rscores, Z] = APPsp2regions(segDensity, spdata, nSegments)
2
+ % [maps, rscores, Z] = APPsp2regions(segDensity, spdata, nSegments)
3
+ % Clusters the superpixels into segments according to pairwise likelihoods
4
+ % given by segDensity
5
+ %
6
+ % Input:
7
+ % segDensity: pairwise sp likelihoods structure
8
+ % spdata: superpixel data and features structure
9
+ % nSegments: array containing the number of segments for each
10
+ % segmentation
11
+ % Ouput:
12
+ % maps(nSp, nMaps): randomly generated clusterings according
13
+ % to the distribution given by densities and features
14
+ % rscores: the average log likelihood for each region
15
+ % Z: the pairwise superpixel likelihood matrix
16
+ %
17
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
18
+ % Current Version: 1.0 09/30/2005
19
+
20
+ features = spdata.features;
21
+
22
+ nSp = size(features, 1);
23
+ nFeatures = size(features, 2);
24
+ nDist = nSp*(nSp-1)/2;
25
+ nMaps = length(nSegments);
26
+
27
+ Z = APPgetPairwiseSuperpixelLikelihoods(features, segDensity);
28
+
29
+ % convert Z to P(y1=y2 | |x1-x2|)
30
+ Z = log(1./(1+exp(-Z)));
31
+
32
+ maps = zeros(nSp, nMaps);
33
+ for m = 1:nMaps
34
+
35
+ num_clusters = nSegments(m);
36
+ if num_clusters > nSp
37
+ num_clusters = nSp;
38
+ end
39
+ % get random ordering of indices
40
+ pind = randperm(nSp);
41
+
42
+ % assign cluster ids to first num_clusters clusters
43
+ for i = 1:num_clusters
44
+ maps(pind(i), m) = i;
45
+ end
46
+
47
+ % assign remaining clusters
48
+ % go forwards ...
49
+ for i = num_clusters+1:length(pind)
50
+ % compute P(c(i) = k | x) for each k
51
+ f = zeros(num_clusters, 1);
52
+ likelihoods = Z(pind(1:i-1), pind(i));
53
+ for k = 1:num_clusters
54
+ sameinds = find(maps(pind(1:i-1), m)==k);
55
+ f(k) = mean(likelihoods(sameinds));
56
+ end
57
+
58
+ [tmp, k] = max(f);
59
+
60
+ % assign cluster and add log(P(ci = k | x, c))
61
+ maps(pind(i), m) = k;
62
+ %fact = repmat(-1, [i-1 1]);
63
+ %fact(find(maps(pind(1:i-1), m)==k))=1;
64
+
65
+ end
66
+
67
+ %... and go backwards now using all cluster assignments
68
+ for pp = 2:2
69
+ %for i = num_clusters+1:length(pind)
70
+ for i = 1:length(pind) % allow initial assignments to be reassigned
71
+ % compute P(c(i) = k | x) for each k
72
+ f = zeros(num_clusters, 1);
73
+ likelihoods = Z(:, pind(i));
74
+ for k = 1:num_clusters
75
+ sameinds = find(maps(:, m)==k);
76
+ if length(sameinds) > 0
77
+ f(k) = mean(likelihoods(sameinds));
78
+ end
79
+ end
80
+
81
+ [tmp, k] = max(f);
82
+
83
+ % assign cluster and add log(P(ci = k | x, c))
84
+ maps(pind(i), m) = k;
85
+ %fact = repmat(-1, [i-1 1]);
86
+ %fact(find(maps(pind(1:i-1), m)==k))=1;
87
+ end
88
+ end
89
+
90
+ currmap = maps(:, m);
91
+ nc = max(currmap);
92
+ for k = 1:nc
93
+ ninds = length(find(currmap==k));
94
+ while ninds == 0 & k < nc
95
+ inds = find(currmap>k);
96
+ currmap(inds) = currmap(inds)-1;
97
+ ninds = length(find(currmap==k));
98
+ nc = max(currmap);
99
+ end
100
+ end
101
+ maps(:, m) = currmap;
102
+
103
+ if nargout > 1
104
+ rscores{m} = zeros(nc, 1);
105
+ for k = 1:nc
106
+ ind = find(currmap==k);
107
+ npairs = length(ind)*(length(ind)-1);
108
+ % average of average log-likelihoods
109
+ rscores{m}(k) = 1./(1+exp(-sum(sum(Z(ind, ind)))/npairs));
110
+ end
111
+ end
112
+
113
+ end
114
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPspInds2regionInds.m ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function rinds = APPspInds2RegionInds(map, sinds)
2
+ % rinds = APPspInds2RegionInds(map, sinds)
3
+ % Gets the sp in each region
4
+ %
5
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
6
+ % Current Version: 1.0 09/30/2005
7
+
8
+ nr = max(map);
9
+ rinds = cell(nr, 1);
10
+ for r = 1:nr
11
+ count = 0;
12
+ rs = find(map==r);
13
+ for k = 1:length(rs)
14
+ count = count + length(sinds{rs(k)});
15
+ end
16
+ rinds{r} = zeros(count, 1);
17
+
18
+ count = 0;
19
+ for k = 1:length(rs)
20
+ rinds{r}(count+1:count+length(sinds{rs(k)})) = sinds{rs(k)};
21
+ count = count + length(sinds{rs(k)});
22
+ end
23
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPtestImage.m~ ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [labels, conf_map, maps, pmaps] = APPtestImage(image, imsegs, ...
2
+ vClassifier, hClassifier, segDensity)
3
+ % [labels, conf_map, maps, pmaps] = APPtestImage(image, imsegs,
4
+ % vClassifier, hClassifier, segDensity)
5
+ %
6
+ % Gets the geometry for a single image.
7
+ %
8
+ % Input:
9
+ % image: the image to process
10
+ % imsegs: the sp structure
11
+ % vClassifier: region classifier for main classes
12
+ % hClassifier: region classifier for vertical subclasses
13
+ % segDensity: density for superpixel clustering
14
+ % Output:
15
+ % labels: structure containing labeling results for each image
16
+ % conf_map: the likelihoods for each sp for each class for each image
17
+ % maps: the segmentation region maps (from superpixels to regions)
18
+ % pmaps: probabilities for each region
19
+ %
20
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
21
+ % Permission granted to non-commercial enterprises for
22
+ % modification/redistribution under GNU GPL.
23
+ % Current Version: 1.0 09/30/2005
24
+
25
+
26
+ % mix class must be the last class in each classifier
27
+
28
+ %exclude mix class
29
+ nvclasses = length(vClassifier.names)-1;
30
+ nhclasses = length(hClassifier.names)-1;
31
+
32
+ [doog_filters, texton_data] = APPgetImageFilters;
33
+
34
+ imsegs.seginds = APPgetSpInds(imsegs);
35
+
36
+ % compute features
37
+ spdata = APPgetSpData(image, doog_filters, texton_data.tim, imsegs);
38
+ [spdata.hue, spdata.sat, tmp] = rgb2hsv(image);
39
+
40
+
41
+ imsize = size(image);
42
+ minEdgeLen = sqrt(imsize(1)^2+imsize(2)^2)*0.02;
43
+ [vpdata.lines, vpdata.spinfo] = ...
44
+ APPgetLargeConnectedEdges(rgb2gray(image), minEdgeLen, imsegs);
45
+ [vpdata.v, vpdata.vars, vpdata.p, vpdata.hpos] = ...
46
+ APPestimateVp(vpdata.lines, imsize(1:2), 0);
47
+
48
+ % create multiple partitions
49
+ num_partitions = [3 4 5 7 9 11 15 20 25];
50
+ num_maps = length(num_partitions);
51
+ maps = APPsp2regions(segDensity, spdata, num_partitions);
52
+
53
+ nsegs = imsegs.nseg;
54
+ pV = zeros(nsegs, nvclasses);
55
+ pH = zeros(nsegs, nhclasses);
56
+
57
+ % compute the probability of each block having each possible label
58
+ for m = 1:num_maps
59
+
60
+ currMap = maps(:, m);
61
+
62
+ %cluster_image = get_cluster_overlay_s2(rgb2gray(image), currMap, imsegs.segimage);
63
+ %imwrite(cluster_image, ['../results/tmp/alley09.c.' num2str(max(currMap)) '.jpg'], 'Quality', 80);
64
+
65
+ currNSegments = max(currMap(:));
66
+
67
+ regionFeatures = APPgetRegionFeatures(image, imsegs, currMap, (1:currNSegments), spdata, vpdata);
68
+
69
+ nregions = size(regionFeatures, 1);
70
+
71
+ % get probability of vertical classes P(y|x) for each region
72
+ tmpV = test_boosted_dt_mc(vClassifier, regionFeatures);
73
+ tmpV = 1 ./ (1+exp(-tmpV));
74
+
75
+ % get probability of horizontal classes P(y|x) for each region
76
+ tmpH = test_boosted_dt_mc(hClassifier, regionFeatures);
77
+ tmpH = 1 ./ (1+exp(-tmpH));
78
+
79
+ % normalize probabilities so that each is P(label|~mixed)P(~mixed)
80
+ % normalize probabilities and sum over maps
81
+ for r = 1:nregions
82
+ indices = find(currMap == r);
83
+ tmpV(r, 1:end-1) = tmpV(r, 1:end-1) / sum(tmpV(r, 1:end-1));
84
+ tmpV(r, 1:end-1) = tmpV(r, 1:end-1) * (1-tmpV(r, end));
85
+ for c = 1:nvclasses
86
+ pV(indices, c) = pV(indices, c) + tmpV(r, c);%*pYv(c, r);
87
+ end
88
+ tmpH(r, 1:end-1) = tmpH(r, 1:end-1) / sum(tmpH(r, 1:end-1));
89
+ tmpH(r, 1:end-1) = tmpH(r, 1:end-1) * (1-tmpH(r, end));
90
+ for c = 1:nhclasses
91
+ pH(indices, c) = pH(indices, c) + tmpH(r, c);%*pYh(c, r);
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ % re-normalize weighted vote from classifiers
99
+ for s = 1:size(pV, 1)
100
+ pV(s, :) = pV(s, :) / sum(pV(s, :));
101
+ end
102
+ for s = 1:size(pH, 1)
103
+ pH(s, :) = pH(s, :) / sum(pH(s, :));
104
+ end
105
+
106
+ conf_map.vmap = pV;
107
+ conf_map.hmap = pH;
108
+ conf_map.vnames = vClassifier.names(1:end-1);
109
+ conf_map.hnames = hClassifier.names(1:end-1);
110
+
111
+
112
+ % get label for each block with confidence
113
+ % total_labels = char(zeros(num_blocks, 7));
114
+ labels.vert_labels = cell(nsegs, 1);
115
+ labels.vert_conf = zeros(nsegs, 1);
116
+ labels.horz_labels = cell(nsegs, 1);
117
+ labels.horz_conf = zeros(nsegs, 1);
118
+ for s = 1:nsegs
119
+ [labels.vert_conf(s), c] = max(pV(s, :));
120
+ labels.vert_labels(s) = vClassifier.names(c);
121
+ [labels.horz_conf(s), c] = max(pH(s, :));
122
+ labels.horz_labels(s) = hClassifier.names(c);
123
+ end
124
+
125
+ [tmp, vlabels] = max(pV, [], 2);
126
+ [tmp, hlabels] = max(pH, [], 2);
127
+
128
+ if 0
129
+ [hy, estType] = geometry2horizon(imsegs, vlabels, hlabels, vpdata);
130
+
131
+ labels.hy = 1-hy;
132
+ labels.hestType = estType;
133
+ end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPvp2horizon.m ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function [pos, tilt, conf, y, py] = APPvp2horizon(v, vars, p, imsize)
2
+ % [pos, tilt, conf, y, py] = APPvp2horizon(v, vars, p, imsize)
3
+ %
4
+ % Estimates the horizon position based on the vanishing points. Doesn't
5
+ % work well.
6
+ %
7
+ % Input:
8
+ % v - vanishing points from estimateVP
9
+ % vars - variances on vanishing point estimates
10
+ % p - memberships of lines in vp
11
+ % imsize - the size of the grayscale image
12
+ % Output:
13
+ % pos - y position (0 is top of image) of horizon, NaN if cannot be found
14
+ % tilt - the angle of the horizon line, NaN if not set
15
+ %
16
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
17
+ % Current Version: 1.0 09/30/2005
18
+
19
+ % find vanishing points that occur within image height (or close)
20
+ ind = find([v(:, 2) > -0.25 & v(:, 2) < 1.25]);
21
+
22
+ % do not consider vp with variance greater than varthresh
23
+ varthresh = 0.005;
24
+ ind(find(vars(ind) > varthresh)) = [];
25
+
26
+ % do not consider vp with fewer than 6 lines (expected)
27
+ memberthresh = 6;
28
+ nmembers = sum(p(:, ind), 1);
29
+ ind(find(nmembers < memberthresh)) = [];
30
+
31
+ conf = length(ind);
32
+
33
+ % if more than two vp are left, choose two with least variance
34
+ %[tmp, ordering] = sort(vars(ind), 'ascend');
35
+ %ind = ind(ordering(1:min(2, length(ordering))));
36
+
37
+ v = v(ind, :);
38
+ vars = vars(ind);
39
+
40
+ % compute horizon position (if possible) and tilt (if possible)
41
+ if length(ind)==0
42
+ pos = NaN;
43
+ tilt = NaN;
44
+ conf = NaN;
45
+ elseif length(ind)>0
46
+
47
+ % change variables so variance is along y axis only
48
+ v(:, 1) = v(:, 1) - 0.5*imsize(2)/imsize(1);
49
+ v(:, 2) = v(:, 2) - 0.5;
50
+ for i = 1:length(ind)
51
+ vars(i) = vars(i)*v(i,2)^2 / (v(i,1)^2+v(i,2)^2);
52
+ end
53
+ pos = sum(v(:, 2) ./ vars') / sum(1./vars');
54
+ % pos = mean(v(:, 2));
55
+
56
+ % pos = mean(v(:, 2));
57
+ % conf = std(v(:, 2)) * sqrt(length(ind) / (length(ind)-1));
58
+ % tilt = 0;
59
+
60
+ % disp(['vp: ' num2str(length(ind))])
61
+ % if length(ind)>2
62
+ % [p, stats] = robustfit(v(:,1),v(:,2));
63
+ % tilt = p(1);
64
+ % else
65
+ % [p, stats] = robustfit(v(:, 1), v(:, 2));
66
+ % tilt = p(1);
67
+ % end
68
+ % conf = stats.se(2);
69
+ % pos = p(2);
70
+ %[pos, conf] = polyval(p, 0, S);
71
+ %pos = p(2);
72
+ % conf = mu(2);
73
+ %tilt = p(1);
74
+ % disp(num2str([pos+0.5 tilt conf]));
75
+
76
+ % get distribution of horizon surrounding most likely position
77
+ if nargout > 3
78
+ y = [pos-0.25:0.005:pos+0.25];
79
+ py = zeros(size(y));
80
+ for i = 1:size(v, 1)
81
+ py = py + log(1 / sqrt(2*pi*vars(i))*exp(-1/2/vars(i)*(y-v(i,2)).^2));
82
+ end
83
+ py = exp(py);
84
+ py = py / sum(py);
85
+
86
+ y = y + 0.5;
87
+
88
+ % remove sampling points with very low probability
89
+ ind = find(py> (1E-4)/length(py));
90
+ y = y(ind);
91
+ py = py(ind);
92
+ py = py / sum(py);
93
+ end
94
+
95
+ pos = pos + 0.5;
96
+ %tilt = NaN;
97
+
98
+ %figure(1), plot(y, py, 'r')
99
+ end
100
+
101
+
102
+ % elseif length(ind)==1
103
+ % pos = 1-v(1, 2);
104
+ % tilt = NaN;
105
+ % end
106
+ % else % length(ind)==2
107
+ % midx = (imsize(2)/2/imsize(1));
108
+ % m = (v(1,2)-v(2,2)) / (v(1,1)-v(2,1));
109
+ % b = v(1,2) - m*v(1,1);
110
+ % pos = midx*m+b;
111
+ % tilt = atan(-m);
112
+ % end
SUN_source_code_v1/code/GeometricContext_dhoiem/src/geom/APPvp2regionFeatures.m ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function features = APPvp2regionFeatures(sinds, vpdata, rcenter, rbounds, imsegs)
2
+ % get the vanishing point features for a particular set of superpixels
3
+ % INPUT:
4
+ % sinds - the indices for the superpixels in the region
5
+ % vpdata.{v, vars, p, hpos, lines, spinfo} - statistics from estimateVP over entire image
6
+ % spinfo - tells which lines each superpixel contains
7
+ % lines - [x1 x2 y1 y2 theta r] endpoint and parameters for each line
8
+ % rcenter - center of the region [x y]; (0,0) is upper-left of image
9
+ % rbounds - bounds of region [minx maxx miny maxy]; (0,0) is upper-left of image
10
+ % imsegs - info about superpixelation
11
+ % OUTPUT:
12
+ % features
13
+ % 1 - 8 : line orientation histogram (normalized, weighted by length)
14
+ % 9 : entropy of line orientation histogram
15
+ % 10 : (num line pixels) / sqrt(area)
16
+ % 11 : (num line pixels with vertical vp membership) / sqrt(area)
17
+ % 12 : (num line pixels with horizontal vp membership) / sqrt(area)
18
+ % 13 : % of total line pixels with vertical membership
19
+ % 14 : x-pos of vp on horizon - center (0 if none)
20
+ % 15 : (y-pos of highest/lowest vertical vp - center) (0 if none)
21
+ % 16 : center wrt horizon vp {left, right, surrounding, far left/right, no vp on horizon}
22
+ %%%%%%%%%% 17-19 no longer used because ineffective
23
+ % 17-19 : same as 14-16 but with vp estimated only from region
24
+ % discrete features: 16
25
+ %
26
+ % Copyright(C) Derek Hoiem, Carnegie Mellon University, 2005
27
+ % Current Version: 1.0 09/30/2005
28
+
29
+ features = zeros(1, 16);
30
+
31
+ % maximum variance for a vp to be considered reliable
32
+ maxvpvar = 0.001;
33
+
34
+ % min distance to be considered "far"
35
+ minfardist = 2.5;
36
+
37
+ % max y-value for vp to be considered horizon candidate
38
+ maxhorzpos = 1.25;
39
+
40
+ % select relevant parts of data to this region
41
+ spinfo = vpdata.spinfo;
42
+ lines = vpdata.lines;
43
+ nseg = length(sinds);
44
+ spinfo = spinfo(sinds);
45
+ lineinds = union([spinfo(:).lines], []);
46
+ lines = lines(lineinds, :);
47
+
48
+ % arrange vp info
49
+ nvp = size(vpdata.v, 1);
50
+ imsize = imsegs.imsize;
51
+ isvalidvp = ones(size(vpdata.vars));
52
+ vpdata.p = vpdata.p(lineinds, :);
53
+ [linevpconf, linevp] = max(vpdata.p, [], 2);
54
+ %disp(num2str(size(lines, 1)))
55
+ %disp(num2str(sum(vpdata.p, 1)))
56
+ for i = 1:length(isvalidvp)
57
+ if isvalidvp(i) && sum(vpdata.p(:, i)) <= 3
58
+ isvalidvp(i) = 0;
59
+ end
60
+ end
61
+ %disp(num2str(isvalidvp))
62
+
63
+
64
+ % make vp relative to region center
65
+ vpdata.v = vpdata.v - repmat([rcenter 1], nvp, 1);
66
+ % vpdata.v(:, 1) = vpdata.v(:, 1) - imsize(2)/imsize(1)/2;
67
+ % vpdata.v(:, 2) = vpdata.v(:, 2) - 1/2;
68
+
69
+ % more line and area statistics
70
+ nlines = size(lines, 1);
71
+ nlinepixels = sum([spinfo(:).edge_count]);
72
+ linelength = sqrt((lines(:, 1)-lines(:,2)).^2 + (lines(:, 3)-lines(:,4)).^2);
73
+ rarea = sum(imsegs.npixels(sinds)) / prod(imsegs.imsize(1:2));
74
+
75
+ % orientation histogram and entropy
76
+ nbins = 8;
77
+ bw = pi / nbins;
78
+ %bincenters = [0 : bw : pi];
79
+ binedges = [bw/2 : bw : pi-bw/2];
80
+ lineangles = mod(lines(:,5), pi); % range from 0 to pi
81
+ bins = zeros(nlines, 1);
82
+ for k = 1:nlines
83
+ bins(k) = sum(lineangles(k) > binedges);
84
+ end
85
+ bins = bins + nbins*(bins==0);
86
+ for k = 1:nbins
87
+ linehist(k) = sum(linelength(find(bins==k)));
88
+ end
89
+ if sum(linehist)==0
90
+ features(1:8) = 0;
91
+ features(9) = 1;
92
+ else
93
+ linehist = linehist+1;
94
+ linehist = linehist / sum(linehist);
95
+ features(1:8) = linehist;
96
+ features(9) = -1 * sum(linehist .* log(linehist))/log(nbins);
97
+ end
98
+
99
+ % 10 : (num line pixels) / sqrt(area)
100
+ features(10) = sum(nlinepixels) / sqrt(rarea);
101
+
102
+ % % for vertical check that vanishing point has at least 75% confidence of
103
+ % % being greater than 75 degrees
104
+ %vertconf = 1 - normcdf(3.75, abs(vpdata.v(:, 2)'), sqrt(vpdata.vars));
105
+ vertvp = find((abs(vpdata.v(:, 2)) > minfardist) & (vpdata.vars < maxvpvar)' & isvalidvp');
106
+ horzvp = find((abs(vpdata.v(:, 2)) < maxhorzpos) & isvalidvp' & (vpdata.vars < maxvpvar)');
107
+
108
+ % 11 : (num line pixels with vertical vp membership) / sqrt(area)
109
+ features(11) = 0;
110
+ for i = 1:length(vertvp)
111
+ vertlineinds = find(linevp==vertvp(i));
112
+ features(11) = features(11) + sum(linelength(vertlineinds));
113
+ end
114
+ features(11) = features(11) / sqrt(rarea);
115
+
116
+ % 12 : (num line pixels with horizontal vp membership) / sqrt(area)
117
+ featuers(12) = 0;
118
+ for i = 1:length(horzvp)
119
+ horzlineinds = find(linevp==horzvp(i));
120
+ features(12) = features(12) + sum(linelength(horzlineinds));
121
+ end
122
+ features(12) = features(12) / sqrt(rarea);
123
+
124
+ % 13 : % of total line pixels with vertical membership
125
+ if linelength > 0
126
+ features(13) = features(11) / ( sum(linelength) / sqrt(rarea) );
127
+ end
128
+
129
+ % expected number of lines belonging to each vanishing point
130
+ vpcount = sum(vpdata.p, 1);
131
+
132
+ % 14 : x-pos of vp on horizon - center (0 if none)
133
+ [tmp, maxind] = max(vpcount(horzvp));
134
+ besthorzdist = vpdata.v(horzvp(maxind), 1);
135
+ if ~isempty(besthorzdist)
136
+ features(14) = besthorzdist;
137
+ end % else features(14) = 0
138
+
139
+ % 15 : abs(y-pos of highest/lowest vertical vp - center) (0 if none)
140
+ [tmp, maxind] = max(vpcount(vertvp));
141
+ bestvertdist = vpdata.v(vertvp(maxind), 2);
142
+ if ~isempty(bestvertdist)
143
+ features(15) = bestvertdist;
144
+ end % else features(15) = 0
145
+
146
+ % 16 : region pos wrt horizon vp {left, right, surrounding, far left/right, no vp on horizon}
147
+ tmppos = besthorzdist + rcenter(1); % return to image coordinates
148
+ if (isempty(besthorzdist)) % no vp on horizon
149
+ features(16) = 5;
150
+ elseif abs(besthorzdist) > minfardist % region far from vp (implies facing center)
151
+ features(16) = 4;
152
+ elseif rbounds(2) < tmppos % region left of vp (implies facing right)
153
+ features(16) = 1;
154
+ elseif rbounds(1) > tmppos % region right of vp (implies facing left)
155
+ features(16) = 2;
156
+ else % region surrounds vp (implies mixed)
157
+ features(16) = 3;
158
+ end
159
+
160
+
161
+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162
+ % % COMPUTE NEW VP USING REGION ONLY DATA FOR 17-19
163
+ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164
+ % if ~isempty(lines)
165
+ % [vpdata.v, vpdata.vars, vpdata.p] = estimateVP(lines, imsize);
166
+ % else
167
+ % vpdata.v = [0 0 1];
168
+ % vpdata.vars = [1];
169
+ % vpdata.p = [];
170
+ % end
171
+ %
172
+ % %vertconf = 1 - normcdf(3.75, abs(vpdata.v(:, 2)'), sqrt(vpdata.vars));
173
+ % vertvp = find((abs(vpdata.v(:, 2)) > minfardist) & (vpdata.vars < maxvpvar)');
174
+ % horzvp = find((abs(vpdata.v(:, 2)) < maxhorzpos) & (vpdata.vars < maxvpvar)');
175
+ % vpcount = sum(vpdata.p, 1);
176
+ %
177
+ % % 17 : x-pos of vp on horizon - center (0 if none)
178
+ % [tmp, maxind] = max(vpcount(horzvp));
179
+ % besthorzdist = vpdata.v(horzvp(maxind), 1);
180
+ % if ~isempty(besthorzdist)
181
+ % features(17) = besthorzdist;
182
+ % end % else features(17) = 0
183
+ %
184
+ % % 18 : abs(y-pos of highest/lowest vertical vp - center) (0 if none)
185
+ % [tmp, maxind] = max(vpcount(vertvp));
186
+ % bestvertdist = vpdata.v(vertvp(maxind), 2);
187
+ % if ~isempty(bestvertdist)
188
+ % features(18) = bestvertdist;
189
+ % end % else features(18) = 0
190
+ %
191
+ % % 19 : region pos wrt horizon vp {left, right, surrounding, far left/right, no vp on horizon}
192
+ % tmppos = besthorzdist + rcenter(1); % return to image coordinates
193
+ % if (isempty(besthorzdist)) % no vp on horizon
194
+ % features(19) = 5;
195
+ % elseif abs(besthorzdist) > minfardist % region far from vp (implies facing center)
196
+ % features(19) = 4;
197
+ % elseif rbounds(2) < tmppos % region left of vp (implies facing right)
198
+ % features(19) = 1;
199
+ % elseif rbounds(1) > tmppos % region right of vp (implies facing left)
200
+ % features(19) = 2;
201
+ % else % region surrounds vp (implies mixed)
202
+ % features(19) = 3;
203
+ % end
204
+
205
+ %disp(num2str(features(14:19)))
206
+
207
+ % % 17 : region pos wrt horizon {below, surrounding, above}
208
+ % tmpos = vpdata.hpos;
209
+ % if (tmppos > rbounds(3)) && (tmppos < rbounds(4)) % region surrounds horizon (implies vertical)
210
+ % features(17) = 2;
211
+ % elseif rbounds(3) < tmppos % region below horizon (implies ground or vertical)
212
+ % features(16) = 1;
213
+ % elseif rbounds(4) > tmppos % region above horizon (implies sky or vertical)
214
+ % features(16) = 2;
215
+ % end
216
+
217
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/ijcv06/hsWheel.m ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function im = hsWheel(hrange, srange, wrad)
2
+
3
+ im = zeros(2*wrad, 2*wrad);
4
+
5
+ for ty = 1:2*wrad
6
+ for tx = 1:2*wrad
7
+ y = ty-1/2 - wrad;
8
+ x = tx-1/2 - wrad;
9
+ r = sqrt(y^2 + x^2);
10
+ if r <= wrad
11
+ theta = atan2(x, y);
12
+ theta = theta + (theta<0)*2*theta;
13
+ [tmp, sind] = min(abs(srange-r/wrad));
14
+ [tmp, hind] = min(abs(hrange-theta/2/pi));
15
+ im(ty, tx) = (hind-1)*numel(hrange) + sind;
16
+ end
17
+ end
18
+ end
19
+
20
+
21
+
SUN_source_code_v1/code/GeometricContext_dhoiem/src/ijcv06/ijcvColorFigureScript.m ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % ijcvLocationFigureScript
2
+
3
+ outdir = '/IUS/vmr20/dhoiem/data/ijcv06/results';
4
+
5
+ nb = 50; % nbins
6
+
7
+ vimages = ones(nb, nb, 3);
8
+ himages = ones(nb, nb, 5);
9
+
10
+ for f = 1:numel(imsegs)
11
+
12
+ segimage = imsegs(f).segimage;
13
+ im = im2double(imread([imdir '/' imsegs(f).imname]));
14
+ [hue, sat, val] = rgb2hsv(im);
15
+ hue = max(ceil(hue*nb),1);
16
+ sat = max(ceil((1-sat)*nb),1);
17
+
18
+ vlab = imsegs(f).vert_labels(imsegs(f).segimage);
19
+ hlab = imsegs(f).horz_labels(imsegs(f).segimage);
20
+
21
+ for k = 1:numel(segimage)
22
+ if vlab(k)~=0
23
+ vimages(sat(k), hue(k), vlab(k)) = vimages(sat(k), hue(k), vlab(k)) + 1;
24
+ end
25
+ if hlab(k)~=0
26
+ himages(sat(k), hue(k), hlab(k)) = himages(sat(k), hue(k), hlab(k)) + 1;
27
+ end
28
+ end
29
+
30
+ if mod(f, 50)==0
31
+ disp(num2str(f))
32
+ end
33
+ end
34
+
35
+ probcolor = sum(vimages, 3);
36
+ probcolor = probcolor / sum(probcolor(:));
37
+ probcolor = probcolor / max(probcolor(:));
38
+
39
+
40
+ vimages = vimages ./ repmat(sum(vimages, 3), [1 1 3]);
41
+ himages = himages ./ repmat(sum(himages, 3), [1 1 5]);
42
+
43
+ hueim = repmat([1:nb]/nb, [nb 1]);
44
+ satim = repmat(1-[1:nb]'/nb, [1 nb]);
45
+
46
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, probcolor),1)), [outdir '/color_prob.jpg'], 'Quality', 100);
47
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, ones(nb,nb)),1)), [outdir '/color_full.jpg'], 'Quality', 100);
48
+ for v = 1:3
49
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, vimages(:, :, v)),1)), ...
50
+ [outdir '/vcol' num2str(v) '.jpg'], 'Quality', 100);
51
+ end
52
+
53
+ for h = 1:5
54
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, himages(:, :, h)),1)), ...
55
+ [outdir '/hcol' num2str(h) '.jpg'], 'Quality', 100);
56
+ end
57
+
58
+ % [tmp, bestv] = max(vimages, [], 3);
59
+ % [tmp, besth] = max(himages, [], 3);
60
+ % colimseg.nseg = 7;
61
+ % colimseg.segimage = (bestv==1) + (bestv==2).*(1+besth) + 7*(bestv==3);
62
+ % for k = 1:7
63
+ % colimseg.npixels(k) = sum(colimseg.segimage(:)==k);
64
+ % end
65
+ % lim = APPgetLabeledImage(ones(200,200, 3), colimseg, ...
66
+ % {'000', '090', '090', '090', '090', '090', 'sky'}, ones(7, 1), ...
67
+ % {'---', '045', '090', '135', 'por', 'sol', '---'}, ones(7, 1));
68
+ % imwrite(lim, [outdir '/labeledcol.jpg'], 'Quality', 100);
69
+ % lim2 = (lim>0).*vimages(:, :, [2 1 3]);
70
+ % imwrite(lim2, [outdir '/labeledcol.jpg'], 'Quality', 100);
SUN_source_code_v1/code/GeometricContext_dhoiem/src/ijcv06/ijcvColorFigureScript.m~ ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ % ijcvLocationFigureScript
2
+
3
+ outdir = '/IUS/vmr20/dhoiem/data/ijcv06/results';
4
+
5
+ nb = 50; % nbins
6
+
7
+ vimages = ones(nb, nb, 3);
8
+ himages = ones(nb, nb, 5);
9
+
10
+ for f = 1:numel(imsegs)
11
+
12
+ segimage = imsegs(f).segimage;
13
+ im = im2double(imread([imdir '/' imsegs(f).imname]));
14
+ [hue, sat, val] = rgb2hsv(im);
15
+ hue = max(ceil((1-hue)*nb),1);
16
+ sat = max(ceil(sat*nb),1);
17
+
18
+ vlab = imsegs(f).vert_labels(imsegs(f).segimage);
19
+ hlab = imsegs(f).horz_labels(imsegs(f).segimage);
20
+
21
+ for k = 1:numel(segimage)
22
+ if vlab(k)~=0
23
+ vimages(sat(k), hue(k), vlab(k)) = vimages(sat(k), hue(k), vlab(k)) + 1;
24
+ end
25
+ if hlab(k)~=0
26
+ himages(sat(k), hue(k), hlab(k)) = himages(sat(k), hue(k), hlab(k)) + 1;
27
+ end
28
+ end
29
+
30
+ if mod(f, 50)==0
31
+ disp(num2str(f))
32
+ end
33
+ end
34
+
35
+ probcolor = sum(vimages, 3);
36
+ probcolor = probcolor / sum(probcolor(:));
37
+ probcolor = probcolor / max(probcolor(:));
38
+
39
+
40
+ vimages = vimages ./ repmat(sum(vimages, 3), [1 1 3]);
41
+ himages = himages ./ repmat(sum(himages, 3), [1 1 5]);
42
+
43
+ hueim = repmat([1:nb]/nb, [nb 1]);
44
+ satim = repmat(1-[1:nb]'/nb, [1 nb]);
45
+
46
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, probcolor),1)), [outdir '/color_prob.jpg'], 'Quality', 100);
47
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, ones(nb,nb)),1)), [outdir '/color_full.jpg'], 'Quality', 100);
48
+ for v = 1:3
49
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, vimages(:, :, v)),1)), ...
50
+ [outdir '/vcol' num2str(v) '.jpg'], 'Quality', 100);
51
+ end
52
+
53
+ for h = 1:5
54
+ imwrite(hsv2rgb(imresize(cat(3, hueim, satim, himages(:, :, h)),1)), ...
55
+ [outdir '/hcol' num2str(h) '.jpg'], 'Quality', 100);
56
+ end
57
+
58
+ [tmp, bestv] = max(vimages, [], 3);
59
+ [tmp, besth] = max(himages, [], 3);
60
+ colimseg.nseg = 7;
61
+ colimseg.segimage = (bestv==1) + (bestv==2).*(1+besth) + 7*(bestv==3);
62
+ for k = 1:7
63
+ colimseg.npixels(k) = sum(colimseg.segimage(:)==k);
64
+ end
65
+ lim = APPgetLabeledImage(ones(200,200, 3), locimseg, ...
66
+ {'000', '090', '090', '090', '090', '090', 'sky'}, ones(7, 1), ...
67
+ {'---', '045', '090', '135', 'por', 'sol', '---'}, ones(7, 1));
68
+ imwrite(lim, [outdir '/labeledloc.jpg'], 'Quality', 100);
69
+ lim2 = (lim>0).*vimages(:, :, [2 1 3]);
70
+ imwrite(lim2, [outdir '/labeledloc2.jpg'], 'Quality', 100);