1 | % * This code was used in the following articles:
|
---|
2 | % * [1] Learning 3-D Scene Structure from a Single Still Image,
|
---|
3 | % * Ashutosh Saxena, Min Sun, Andrew Y. Ng,
|
---|
4 | % * In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
|
---|
5 | % * (best paper)
|
---|
6 | % * [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
|
---|
7 | % * Ashutosh Saxena, Min Sun, Andrew Y. Ng,
|
---|
8 | % * In ICCV workshop on Virtual Representations and Modeling
|
---|
9 | % * of Large-scale environments (VRML), 2007.
|
---|
10 | % * [3] 3-D Depth Reconstruction from a Single Still Image,
|
---|
11 | % * Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
|
---|
12 | % * International Journal of Computer Vision (IJCV), Aug 2007.
|
---|
13 | % * [6] Learning Depth from Single Monocular Images,
|
---|
14 | % * Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
|
---|
15 | % * In Neural Information Processing Systems (NIPS) 18, 2005.
|
---|
16 | % *
|
---|
17 | % * These articles are available at:
|
---|
18 | % * http://make3d.stanford.edu/publications
|
---|
19 | % *
|
---|
20 | % * We request that you cite the papers [1], [3] and [6] in any of
|
---|
21 | % * your reports that uses this code.
|
---|
22 | % * Further, if you use the code in image3dstiching/ (multiple image version),
|
---|
23 | % * then please cite [2].
|
---|
24 | % *
|
---|
25 | % * If you use the code in third_party/, then PLEASE CITE and follow the
|
---|
26 | % * LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
|
---|
27 | % *
|
---|
28 | % * Finally, this code is for non-commercial use only. For further
|
---|
29 | % * information and to obtain a copy of the license, see
|
---|
30 | % *
|
---|
31 | % * http://make3d.stanford.edu/publications/code
|
---|
32 | % *
|
---|
33 | % * Also, the software distributed under the License is distributed on an
|
---|
34 | % * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
---|
35 | % * express or implied. See the License for the specific language governing
|
---|
36 | % * permissions and limitations under the License.
|
---|
37 | % *
|
---|
38 | % */
|
---|
39 | function [NewBox] = RefineBox(Sup, Box, InScribeRatioThre); |
---|
40 | |
---|
41 | NumBox = size(Box,1); |
---|
42 | VOri = Box(:,3) - Box(:,1) + 1; |
---|
43 | HOri = Box(:,4) - Box(:,2) + 1; |
---|
44 | AreaOri = VOri.*HOri; |
---|
45 | NewBox = [zeros(size(Box,1),4)]; |
---|
46 | OriShapeFlag = 0; |
---|
47 | ShiftFlag = 1; |
---|
48 | NumScale = 100; |
---|
49 | for i = 1:NumBox |
---|
50 | |
---|
51 | BoxMask = zeros(size(Sup)); |
---|
52 | BoxMask(Box(i,1):Box(i,3), Box(i,2):Box(i,4)) = 1; |
---|
53 | BoxMask = logical( BoxMask); |
---|
54 | ElementInscribe = setdiff( sort( unique( Sup( BoxMask)) ), 0); |
---|
55 | ElementOutSide = setdiff( sort( unique( Sup( ~BoxMask)) ), 0); |
---|
56 | ElementInscribeHistC = sparse(1, max(Sup(:))); |
---|
57 | ElementOutSideHistC = ElementInscribeHistC; |
---|
58 | ElementInscribeHistC( ElementInscribe) = histc(Sup( BoxMask),ElementInscribe); |
---|
59 | ElementOutSideHistC( ElementOutSide) = histc(Sup( ~BoxMask),ElementOutSide); |
---|
60 | InscribeRatio = ElementInscribeHistC./( ElementInscribeHistC+ElementOutSideHistC); |
---|
61 | InscribeRatio( isnan( InscribeRatio)) = 0; |
---|
62 | SupIndRefined = find( InscribeRatio > InScribeRatioThre); |
---|
63 | SupShape = zeros(size( Sup)); |
---|
64 | for j = SupIndRefined |
---|
65 | SupShape( Sup == j) = InscribeRatio(j); |
---|
66 | end |
---|
67 | |
---|
68 | % decide new Box size |
---|
69 | SupShape(~BoxMask) = 0; |
---|
70 | Area = sum( SupShape(:) ~=0) |
---|
71 | Ratio = Area./AreaOri(i); |
---|
72 | |
---|
73 | % pick the Shape of the newBox |
---|
74 | if ~OriShapeFlag |
---|
75 | Vmax = VOri(i,1); |
---|
76 | Hmax = HOri(i,1); |
---|
77 | Vmin = Area/Hmax; |
---|
78 | count = 1; |
---|
79 | for Vtrial = linspace(Vmin,Vmax,NumScale) |
---|
80 | V(count) = max( round( Vtrial), 1); |
---|
81 | H(count) = max( round( Area/Vtrial), 1); |
---|
82 | if ShiftFlag |
---|
83 | h = ones(V(count),H(count)); |
---|
84 | res = filter2(h,SupShape(Box(i,1):Box(i,3), Box(i,2):Box(i,4)), 'valid'); |
---|
85 | [CCol ICol ] = max(res, [], 2); |
---|
86 | [C IRow ] = max(CCol); |
---|
87 | Shift(count,:) = [IRow ICol(IRow)] - 1; |
---|
88 | Score(count) = C; |
---|
89 | else |
---|
90 | Shift(count,:) = [VOri(i) HOri(i)] - [V(count) H(count)]; |
---|
91 | Shift(count,:) = round(Shift(count,:)/2) - 1; |
---|
92 | Score(count) = sum( sum( SupShape( (Box(i,1):(Box(i,1)+V(count)))+Shift(count,1), (Box(i,2):(Box(i,2)+H(count)))+Shift(count,2)))); |
---|
93 | end |
---|
94 | count = count + 1; |
---|
95 | end |
---|
96 | [BestScore BestIndex] = max(Score); |
---|
97 | NewBox(i,:) = [Box(i,1)+Shift(BestIndex,1) Box(i,2)+Shift(BestIndex,2) Box(i,1)+Shift(BestIndex,1)+V(BestIndex) Box(i,2)+Shift(BestIndex,2)+H(BestIndex)]; |
---|
98 | else |
---|
99 | V = max( round( VOri(i)*Ratio), 1); |
---|
100 | H = max( round( HOri(i)*Ratio), 1); |
---|
101 | Shift = [VOri(i) HOri(i)] - [V H]; |
---|
102 | Shift = round(Shift/2) - 1; |
---|
103 | NewBox(i,:) = Box(i,:) + [ Shift -Shift]; |
---|
104 | end |
---|
105 | end |
---|