├── DTW.m ├── Inputs ├── scene1.row3.col3.ppm ├── scene1.row3.col4.ppm └── truedisp.row3.col3.pgm ├── LICENSE ├── Outputs ├── org.jpg ├── res1.jpg └── res2.jpg ├── README.md ├── beliefPropStereo.m ├── beliefpropagatio.m ├── blockmatching.m ├── blockmatching_DW.m ├── comp_data.m ├── differentlevel.m ├── main.m ├── main_noise.m ├── msg2.m └── twotimeres.m /DTW.m: -------------------------------------------------------------------------------- 1 | function c=DTW(blockR,blockL,maxd,cost) 2 | 3 | %% Written by Muhammet Balcilar, France, 4 | %% all rights reserved 5 | 6 | b=size(blockR,1); 7 | 8 | hb=fix(b/2); 9 | M=[]; 10 | 11 | 12 | for j=1:size(blockR,2)-b+1-maxd 13 | for i=j:min(size(blockR,2)-b+1,j+maxd) 14 | tmp=blockR(:,j:j+b-1)-blockL(:,i:i+b-1); 15 | M(j,i)=sum(abs(tmp(:))); 16 | end 17 | end 18 | 19 | M(M==0)=inf; 20 | 21 | for i=2:size(M,1) 22 | for j=i:min(size(blockR,2),i+maxd) 23 | M(i,j)=M(i,j)+min(M(i-1,:)+cost*abs([1:size(M,2)]-j+1)); 24 | end 25 | end 26 | 27 | c=zeros(1,size(blockR,2)); 28 | for i=size(M,1):-1:1 29 | [t b]=min(M(i,:)); 30 | c(i)=b-i; 31 | end 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Inputs/scene1.row3.col3.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balcilar/Comparison-of-Disparity-Estimation-Algorithms/020a41e5891a95ded6e013a96dd2e248fe7a4f8c/Inputs/scene1.row3.col3.ppm -------------------------------------------------------------------------------- /Inputs/scene1.row3.col4.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balcilar/Comparison-of-Disparity-Estimation-Algorithms/020a41e5891a95ded6e013a96dd2e248fe7a4f8c/Inputs/scene1.row3.col4.ppm -------------------------------------------------------------------------------- /Inputs/truedisp.row3.col3.pgm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balcilar/Comparison-of-Disparity-Estimation-Algorithms/020a41e5891a95ded6e013a96dd2e248fe7a4f8c/Inputs/truedisp.row3.col3.pgm -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Outputs/org.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balcilar/Comparison-of-Disparity-Estimation-Algorithms/020a41e5891a95ded6e013a96dd2e248fe7a4f8c/Outputs/org.jpg -------------------------------------------------------------------------------- /Outputs/res1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balcilar/Comparison-of-Disparity-Estimation-Algorithms/020a41e5891a95ded6e013a96dd2e248fe7a4f8c/Outputs/res1.jpg -------------------------------------------------------------------------------- /Outputs/res2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/balcilar/Comparison-of-Disparity-Estimation-Algorithms/020a41e5891a95ded6e013a96dd2e248fe7a4f8c/Outputs/res2.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Comparison of Disparity Estimation Algorithms 2 | 3 | Stereo matching, which is the most profound understanding way of human vision system, is one of the key research areas in image processing, especially in terms of 3D reconstruction, depth estimation. We can divide these literature in two sub class which are monocular and binocular stereo vision. For monocular stereo vision, we can say depth from movement too. It means we use images which was taken from the same camera but in different time with some of translation. It is obvious that, this camera has to move. Unless we cannot take different pose of environment. In that scenario there are some more issues such as we have to estimate camera transformation from first timestamp to the second time stamp and this translation is not fixed. Binocular stereo vision system is about using two identical and same baseline camera, so we know the translation to left one to the right one. That is why binocular stereo system is more practical and relatively easy than another’s. 4 | 5 | Binocular stereo system is the one concentration point of us in that research. Calibrated camera and already rectified pair of images will be input of our system. For that reason, we will just focus on horizontal disparity within these two camera images. As the same in human being visual system as well, the closest object’s horizontal disparities’ are bigger than the farthest object’s disparities. So we can say the farthest object the lesser disparity. Within disparity calculation, we can reconstruct 3D model of the environment with this information. Depth information is very important especially in medical sciences, game industry, robotic and virtual reality application. 6 | By the first step which consist of camera calibration and rectifying, we can get the two image’s line scans represent almost the same part of the sensing environment. Second step is more crucial and important in stereo vision literature which consist of finding correspondence within the left and right image. In literature there are plenty of different methods and still most of the researches carry on to this subject. We do not make a profound literature review, but basically we can divide these method into two group named, feature based and region based methods. Briefly, region based method tries to match sub-region of both left and right images, but feature based method tries to match not all the pixels but just some more important pixels. To find best matches, we can use some distance measuring approach such as correlation or SSD measures. But every times find the best matches does not give us best performance for overall matching results. To gain overall best matching in literature there are dynamic programming approach and energy minimization approach called Markov Random Field. 7 | 8 | In this project, we implemented three disparity estimation algorithm which are simple block matching, block matching with dynamic programming approach and finally Stereo Matching using Belief Propagation algorithm. 9 | 10 | ## Simple Block Matching 11 | 12 | To find corresponding points in stereo images, one of the common and benchmark method is obviously plain block matching. This algorithm is used in this research as the beginning algorithm to find corresponding points between the left and right images that used along the experimental of stereo matching. The block matching algorithm is used to minimize the matching errors between the blocks at any position into reference left image and right image. To find the most similar block we need to check all possible block in the right image which has the same row number but different columns. The searching space has to be in allowable disparity which has to be given once. To measure similarity between reference block and the block checked, sum of absolute difference (SAD) was used in this research [1]. We can summarize this approach as following equation. 13 | 14 | *diff(y,x)_d=∑_(-W≤j≤W) ∑_(-W≤i≤W) |I_left (y+j,x+i)-I_right (y+j,x+i+d)|, for d=[0,d_max]* 15 | 16 | In that formula (y,x) refers pixel position of the image. Ileft and Iright shows reference (left) and right image. W refers the halh of the block size. As you can see the summing space is from –W to +W. So we need to calculate 2*W+1 rows and cols, which means the total of block size is 2*W+1. We need to calculate this sum of absolute value for every single pixel and for every d value in range of [0, dmax]. As a result we can assign disparirt value of pixel located (y,x) by following equation. 17 | 18 | *disparity(y,x)=argmin┬d⁡(diff(y,x)_d)* 19 | 20 | Which means, we need to select optimum d value which provide us minimum diff value which calculated for location (y,x) along the 2*W+1 block size. 21 | To run that method, we provide a function named blockmatching, you can test it with following command, 22 | ``` 23 | disp=blockmatching(leftI,rightI,blockSize,maxd); 24 | ``` 25 | In that command, you should give left image, right image, blocksize and maximum disparity as parameter, then you will get calculated disparity map. 26 | 27 | ## Block Matching with Dynamic Programming Approach 28 | 29 | The most problematic side of simple block matching is it is lack of provide a spatial consistency which means the output of simple matching algorithm has big variation. That is why we can see too many artifact on the result. The main reason of that problem is simple block matching just tries to find best match blocks but not take into account the neighbor pixels disparity. To take the neighborhood pixel’s disparity into account, we implemented dynamic programming optimization algorithm which increase the cost of the match whose matching block is far away from expected position. In that approach we used the same block match approach and SAD error measure as simple method as well. But instead of just searching minimum SAD error block, we used cost value if neighborhood pixel’s disparity is different from each other. For example in any row suppose first block has match with 10pixels away block. That means, for second block, 10 pixel away block again should be expected block which has zero extra penalty. But 9 pixel away or 11 pixel away block has one unit cost in addition to SAD value of corresponding block. And also 8 pixel away and 12 pixel away block has two unit of cost in addition to their SAD error value and this calculation carries on so on. With using that schema, we provide the smooth disparities which means we take the neighborhood disparity in to account which means spatial consistency as well [2]. 30 | In mathematically speaking, our dynamic programming implementation follows following steps. 31 | 32 | *C_y (k,l)=∑_(-W≤j≤W)∑_(-W≤i≤W)|I_left (y+j,x+i+k)-I_right (y+j,x+i+l)| k=[1..N],l=[k,..M]* 33 | 34 | *C_y (k,l)=C_y (k,l)+min⁡(C_y (k-1,l-s:l+s)+γd_(k,l-s:l+s) ) k=[1..N],l=[k,..M]* 35 | 36 | In first step we calculate certain rows (yth row) possible matching SAD value. C_y (k,l) indicates the SAD cost between kth column in the left image to lth column in the right image at yth row. In second step, we aggregate the error value with using actual SAD between blocks but also their disparity differences which refers by d_kl, In that formula we add minimum of previous column SAD error plus disparity differences times cost value into actual SAD value. In that formula, γ indicates the unit penalty value if certain blocks has different disparity. As you can see we try to find minimum cost value in range of [-s +s] neighborhood. 37 | With using Dynamic programming Schema, we can find the best path with backtracking. Basically we start to find minimum aggregated cost value C_y, and find the minimum path from the last column of concerned row to the first column. After all, the path shows us the calculated disparity with dynamic programming. 38 | 39 | To run that method, we provide a function named blockmatching_DW, you can test it with following command, 40 | ``` 41 | dmap=blockmatching_DW(leftI,rightI,blockSize,maxd,cost); 42 | ``` 43 | In that command, you should give left image, right image, blocksize, maximum disparity and cost as parameter, then you will get calculated disparity map. 44 | 45 | ## Stereo Matching Using Belief Propagation 46 | 47 | Disparity estimation is an image labeling problem. It is modeled by Markov Random Field (MRF), and the energy minimization task is solved by some popular global optimization methods, i.e. Graph Cut and Belief Propagation. There are two categories of global optimization such as one dimension and two dimension optimization methods. One dimension optimization is traditional method where its estimation on the disparity is focusing on a pixel that depending on other pixels on the same scanline but independent on disparity that focus on other scanlines as how our previous dynamic programming performs. One dimension is not considered as a truly global optimization as its smoothness technique is only focus on horizontal direction. However, one dimension optimization is still being used by some of the researchers due to its simple implementation and its effectiveness on the disparity maps outputs. On the part of disparity optimization, the global optimization algorithm that chosen for this research is using Belief Propagation method. 48 | Belief propagation (BP) is one of powerful tools for learning low-level vision problems, such as motion analysis, unwrapping phase images, stereo matching, inferring shape and reflectance from photograph, or extrapolating image detail. The key idea of BP is simplified Bayes Net. It propagates information throughout a graphical model via a series of messages sent between neighboring nodes iteratively. The algorithm consists of simple local updates that can be executed and are guaranteed to converge to the correct probabilities. The term belief update is used to describe the scheme for computing the function of probabilistic belief. His algorithm is equivalent to schemes proposed independently in a wide range of fields including information theory, signal processing and optimal estimation. 49 | The disparity estimation problem can be modeled with MRFs and then solved by the BP algorithms [3]. Since the BP algorithm is computationally expensive, some algorithmic techniques are proposed in [3] to substantially improve the running time of the loopy BP approach. One of the techniques reduces the complexity of the inference algorithm to be linear rather than quadratic in the number of possible labels for each pixel. Another technique speeds up and reduces the memory requirements of BP on grid graphs. A third technique is a multi-grid method that makes it possible to obtain good results with a small fixed number of message passing iterations, independent of the size of the input images. In our project the third choice which is using multi scale techniques was used. 50 | We can summarize implemented BP method as follows. 51 | 52 | * Step1. Calculate full resolution pixel absolute differences under from 0 disparity to maximum disparity. Assume if possible maximum disparity is 16. It means we should calculate 16 different image matrix and each of them should the absolute differences between left image and shifted right image. 53 | 54 | * Step2. Calculate the absolute error for every level. Each level’s resolution is half of the previous one. So if the first original resolution is 100x100. Second level has 50x50, third level has 25x25, so on so forth. 55 | 56 | * Step3. Set the matrixes which keep, down, left and right pixel’s values from coarser level. First initialize all 4 matrix all zeros. Set level for coarser level (last level) 57 | 58 | * Step4. Simulate message passing and update both 4 matrix and repeat it for number of iter times. 59 | 60 | * Step5. Decrease level by 1 and Go step 4. If the 1st level reach go step 6. 61 | 62 | * Step6. Find sum of left, right, up, down and level 1 absolute difference value. 63 | 64 | * Step7. Find the displacement index where the value is minimum for concerned location. 65 | 66 | * Step8. Set fond index as disparity of concerned pixel. 67 | 68 | To run that method, we provide a function named beliefPropStereo, you can test it with following command, 69 | ``` 70 | disp=beliefPropStereo(leftI,rightI,maxd,levels,iter); 71 | ``` 72 | In that command, you should give left image, right image, maximum disparity, number of level and number of iteration as parameter, then you will get calculated disparity map. 73 | 74 | 75 | ## Results 76 | 77 | We did several tests with mentioned three algorithm. Our first test on Tsukuba image. In original dataset, 5 different images and one ground truth image are provided. Since the ground truth image was for 3th order image, we selected 3th image as left image and 4th image as right image. Here is left image and its ground truth disparity map. 78 | 79 | ![Sample image](Outputs/org.jpg?raw=true "Title") 80 | 81 | 82 | There is the 3 different method output under block size 11, maximum disparity 15, unit cost value for dynamic programming is 100, level for belief network is 10 and number of iteration for belief network is 10 as well. 83 | We also calculate the correlation of both three method to the ground truth image as well. According to out test simple block matching correlation coefficient is 0.81, dynamic programming reach 0.85, but belief network reach 0.88 coefficient value which has stronger correlation with ground truth image. You can run that test with main script with following command. 84 | 85 | ``` 86 | > main 87 | ``` 88 | Here is the results of all three methods. 89 | 90 | ![Sample image](Outputs/res1.jpg?raw=true "Title") 91 | 92 | We wanted to measure the method accuracy under different level of Gaussian noise. To do that, we added zero mean but 0.02 variance Gaussian noise. Following image shows the noise effect on left image. Using noisy left and right image. We took following result from both three method as follows. And also correlation coefficient found 0.51, 0.59 and 0.84 for all three method respectfully. You can run that test with main_noise script with following command. 93 | 94 | ``` 95 | > main_noise 96 | ``` 97 | 98 | Here is noisy input image and its results under methods. 99 | 100 | ![Sample image](Outputs/res2.jpg?raw=true "Title") 101 | 102 | ## Conclusion 103 | Our tests shows us the global optimization method which take into account of neighborhood disparity both horizontal and vertical axis outperformed other with very large differences. Especially when we add some noise, since the simple block matching method just tries to find minimum SAD value regardless of pixel neighborhood, it fails in many pixels. But dynamic programming approach can handle some error but not at all. As you can see especially it gives better results horizontally. But since it does not take the vertical neighborhood into account it fails significant amount of pixel. At the end belief propagation method provide us better results. 104 | 105 | 106 | ## Reference 107 | [1] Y.Chen, Y.Hung et al.2001.Fast Block Matching Algorithm Based on the Winner-Update Strategy. In IEEE, 10(8), pp.1212-1222 108 | 109 | [2] Karathanasis, J., D. Kalivas, and J. Vlontzos. "Disparity estimation using block matching and dynamic programming." Electronics, Circuits, and Systems, 1996. ICECS'96., Proceedings of the Third IEEE International Conference on. Vol. 2. IEEE, 1996. 110 | 111 | [3] Felzenszwalb, Pedro F., and Daniel P. Huttenlocher. "Efficient belief propagation for early vision." International journal of computer vision 70.1 (2006): 41-54. 112 | -------------------------------------------------------------------------------- /beliefPropStereo.m: -------------------------------------------------------------------------------- 1 | function outimg=beliefPropStereo(img1,img2,maxdisp,levels,iter) 2 | 3 | %% Written by Muhammet Balcilar, France, 4 | %% all rights reserved 5 | 6 | if length(size(img1))==3 7 | % convert it grayscale 8 | img1 = mean(img1, 3); 9 | img2 = mean(img2, 3); 10 | end 11 | 12 | data{1}=comp_data(img1, img2, maxdisp,15,0.07); 13 | 14 | for i=2:levels 15 | tdat=data{i-1}; 16 | ntdat=zeros(ceil(size(tdat,1)/2),ceil(size(tdat,2)/2),maxdisp); 17 | [a b c]=size(ntdat); 18 | if size(tdat,1)<2*a | size(tdat,2) < 2*b 19 | tdat(2*a,2*b,maxdisp)=0; 20 | end 21 | for j=1:maxdisp 22 | tmp=tdat(:,:,j); 23 | ntdat(:,:,j)=tmp(1:2:2*a,1:2:2*b)+tmp(2:2:2*a,1:2:2*b)+tmp(1:2:2*a,2:2:2*b)+tmp(2:2:2*a,2:2:2*b); 24 | end 25 | data{i}=ntdat; 26 | end 27 | 28 | u=zeros(size(data{levels})); 29 | d=zeros(size(data{levels})); 30 | l=zeros(size(data{levels})); 31 | r=zeros(size(data{levels})); 32 | [u,d,l,r]=beliefpropagatio(u, d, l, r, data{levels}, iter); 33 | 34 | for i=levels-1:-1:1 35 | i 36 | tu=twotimeres(u); 37 | td=twotimeres(d); 38 | tl=twotimeres(l); 39 | tr=twotimeres(r); 40 | 41 | [a b c]=size(data{i}); 42 | u=tu(1:a,1:b,:); 43 | d=td(1:a,1:b,:); 44 | l=tl(1:a,1:b,:); 45 | r=tr(1:a,1:b,:); 46 | 47 | [u,d,l,r]=beliefpropagatio(u, d, l, r, data{i}, iter); 48 | end 49 | 50 | 51 | [height width c]=size(u); 52 | dat=data{1}; 53 | for y=2:height-1 54 | for x=2:width-1 55 | val=u(y+1,x,:)+d(y-1,x,:)+l(y,x+1,:)+r(y,x-1)+dat(y,x,:); 56 | [a b]=min(val); 57 | outimg(y,x)=b; 58 | end 59 | end 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /beliefpropagatio.m: -------------------------------------------------------------------------------- 1 | function [u,d,l,r]=beliefpropagatio(u, d, l, r, data, iter) 2 | %% Written by Muhammet Balcilar, France, 3 | %% all rights reserved 4 | 5 | [height width c]=size(data); 6 | if height > 2 & width> 2 7 | for t=0:iter-1 8 | u(2:end-1,2:end-1,:)=msg2(u(3:end,2:end-1,:),l(2:end-1,3:end,:),r(2:end-1,1:end-2,:),data(2:end-1,2:end-1,:)); 9 | d(2:end-1,2:end-1,:)=msg2(d(1:end-2,2:end-1,:),l(2:end-1,3:end,:),r(2:end-1,1:end-2,:),data(2:end-1,2:end-1,:)); 10 | r(2:end-1,2:end-1,:)=msg2(u(3:end,2:end-1,:),d(1:end-2,2:end-1,:),r(2:end-1,1:end-2,:),data(2:end-1,2:end-1,:)); 11 | l(2:end-1,2:end-1,:)=msg2(u(3:end,2:end-1,:),d(1:end-2,2:end-1,:),l(2:end-1,3:end,:),data(2:end-1,2:end-1,:)); 12 | 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /blockmatching.m: -------------------------------------------------------------------------------- 1 | function dispImg=blockmatching(leftG,rightG,blockSize,maxd) 2 | %% Written by Muhammet Balcilar, France, 3 | %% all rights reserved 4 | 5 | if length(size(leftG))==3 6 | % convert it grayscale 7 | leftG = mean(leftG, 3); 8 | rightG = mean(rightG, 3); 9 | end 10 | 11 | hb=fix(blockSize/2); 12 | 13 | dispImg=zeros(size(rightG)); 14 | for i=hb+1:size(leftG,1)-hb 15 | i 16 | for j=hb+1:size(leftG,2)-hb 17 | 18 | blockR=rightG(i-hb:i+hb,j-hb:j+hb); 19 | 20 | bdiff=[]; 21 | 22 | for k=0:min(maxd,size(leftG,2)-hb-j) 23 | blockL=leftG(i-hb:i+hb,j-hb+k:j+hb+k); 24 | % calculate sum of absolute differences (SAD) 25 | bdiff(k+1, 1) = sum(abs(blockL(:) - blockR(:))); 26 | end 27 | 28 | [a1 b1]=min(bdiff); 29 | 30 | if size(bdiff,1)>3 & b1>1 & b1maxdif)=maxdif; 15 | data(:,maxdisp:end,i)=lambda*dI; 16 | end 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /differentlevel.m: -------------------------------------------------------------------------------- 1 | clear all 2 | clc 3 | %% Written by Muhammet Balcilar, France, 4 | %% all rights reserved 5 | 6 | % read reference image as left adn right image 7 | leftI = imread('scene1.row3.col3.ppm'); 8 | rightI = imread('scene1.row3.col4.ppm'); 9 | levels=5; 10 | iter=10; 11 | 12 | % define parameter and call belief Propogation based Stereo 13 | for iter=1:10 14 | maxd=15; 15 | 16 | disp3=beliefPropStereo(leftI,rightI,maxd,levels,iter); 17 | figure;imagesc(disp3); 18 | title(['belief Propogation based Stereo Result for iter=', num2str(iter)] ); 19 | pause(0.5) 20 | end 21 | 22 | % read ground truth disparity image 23 | dispG=imread('truedisp.row3.col3.pgm'); 24 | figure;imagesc(dispG); 25 | title('Ground Truth Image'); 26 | 27 | 28 | 29 | % error calculation 30 | [a b]=size(dispG); 31 | d1=disp1(20:a-20,20-15:b-20-15); 32 | d2=disp2(20:a-20,20-15:b-20-15); 33 | d3=disp3(20:a-20,20:b-20); 34 | dG=double(dispG(20:a-20,20:b-20)); 35 | 36 | c1=corr(dG(:),d1(:)); 37 | c2=corr(dG(:),d2(:)); 38 | c3=corr(dG(:),d3(:)); 39 | 40 | fprintf('Correlation coef for Simple block matching:%f\n',c1); 41 | fprintf('Correlation coef for Dynamic Prog. block matching:%f\n',c2); 42 | fprintf('Correlation coef for belief prop.:%f\n',c3); 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | %% Written by Muhammet Balcilar, France, 2 | %% all rights reserved 3 | 4 | clear all 5 | clc 6 | 7 | 8 | % read reference image as left adn right image 9 | leftI = imread('Inputs/scene1.row3.col3.ppm'); 10 | rightI = imread('Inputs/scene1.row3.col4.ppm'); 11 | 12 | % define parameter for block matching and call block matching algorithm 13 | blockSize=11; 14 | maxd=15; 15 | disp1=blockmatching(leftI,rightI,blockSize,maxd); 16 | figure;imagesc(disp1); 17 | title('blockmatching result'); 18 | 19 | % define parameters and call block matchning with dynamic prog optm. 20 | maxd=15; 21 | blockSize=11; 22 | cost=100; 23 | disp2=blockmatching_DW(leftI,rightI,blockSize,maxd,cost); 24 | figure;imagesc(disp2); 25 | title('blockmatching with Dynamic Prog. Opt. result'); 26 | 27 | % define parameter and call belief Propogation based Stereo 28 | levels=10; 29 | maxd=15; 30 | iter=10; 31 | disp3=beliefPropStereo(leftI,rightI,maxd,levels,iter); 32 | figure;imagesc(disp3); 33 | title('belief Propogation based Stereo Result'); 34 | 35 | % read ground truth disparity image 36 | dispG=imread('Inputs/truedisp.row3.col3.pgm'); 37 | figure;imagesc(dispG); 38 | title('Ground Truth Image'); 39 | 40 | 41 | 42 | % error calculation 43 | [a b]=size(dispG); 44 | d1=disp1(20:a-20,20-15:b-20-15); 45 | d2=disp2(20:a-20,20-15:b-20-15); 46 | d3=disp3(20:a-20,20:b-20); 47 | dG=double(dispG(20:a-20,20:b-20)); 48 | 49 | c1=corr(dG(:),d1(:)); 50 | c2=corr(dG(:),d2(:)); 51 | c3=corr(dG(:),d3(:)); 52 | 53 | fprintf('Correlation coef for Simple block matching:%f\n',c1); 54 | fprintf('Correlation coef for Dynamic Prog. block matching:%f\n',c2); 55 | fprintf('Correlation coef for belief prop.:%f\n',c3); 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /main_noise.m: -------------------------------------------------------------------------------- 1 | %% Written by Muhammet Balcilar, France, 2 | %% all rights reserved 3 | 4 | clear all 5 | clc 6 | 7 | 8 | % read reference image as left adn right image 9 | leftI = imread('Inputs/scene1.row3.col3.ppm'); 10 | rightI = imread('Inputs/scene1.row3.col4.ppm'); 11 | 12 | leftI = imnoise(leftI,'gaussian',0,0.02); 13 | rightI = imnoise(rightI,'gaussian',0,0.02); 14 | 15 | % define parameter for block matching and call block matching algorithm 16 | blockSize=11; 17 | maxd=15; 18 | disp1=blockmatching(leftI,rightI,blockSize,maxd); 19 | figure;imagesc(disp1); 20 | title('blockmatching result'); 21 | 22 | % define parameters and call block matchning with dynamic prog optm. 23 | maxd=15; 24 | blockSize=11; 25 | cost=100; 26 | disp2=blockmatching_DW(leftI,rightI,blockSize,maxd,cost); 27 | figure;imagesc(disp2); 28 | title('blockmatching with Dynamic Prog. Opt. result'); 29 | 30 | % define parameter and call belief Propogation based Stereo 31 | levels=10; 32 | maxd=15; 33 | iter=10; 34 | disp3=beliefPropStereo(leftI,rightI,maxd,levels,iter); 35 | figure;imagesc(disp3); 36 | title('belief Propogation based Stereo Result'); 37 | 38 | % read ground truth disparity image 39 | dispG=imread('Inputs/truedisp.row3.col3.pgm'); 40 | figure;imagesc(dispG); 41 | title('Ground Truth Image'); 42 | 43 | 44 | 45 | % error calculation 46 | [a b]=size(dispG); 47 | d1=disp1(20:a-20,20-15:b-20-15); 48 | d2=disp2(20:a-20,20-15:b-20-15); 49 | d3=disp3(20:a-20,20:b-20); 50 | dG=double(dispG(20:a-20,20:b-20)); 51 | 52 | c1=corr(dG(:),d1(:)); 53 | c2=corr(dG(:),d2(:)); 54 | c3=corr(dG(:),d3(:)); 55 | 56 | fprintf('Correlation coef for Simple block matching:%f\n',c1); 57 | fprintf('Correlation coef for Dynamic Prog. block matching:%f\n',c2); 58 | fprintf('Correlation coef for belief prop.:%f\n',c3); 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /msg2.m: -------------------------------------------------------------------------------- 1 | function dst=msg2(S1,S2,S3,S4) 2 | 3 | %% Written by Muhammet Balcilar, France, 4 | %% all rights reserved 5 | 6 | 7 | [a b c]=size(S1); 8 | 9 | s1=reshape(S1,[a*b,c])'; 10 | s2=reshape(S2,[a*b,c])'; 11 | s3=reshape(S3,[a*b,c])'; 12 | s4=reshape(S4,[a*b,c])'; 13 | 14 | dst=s1+s2+s3+s4; 15 | minimum=min(dst); 16 | 17 | for i=1:size(dst,2) 18 | for q=2:size(s1,1) 19 | prev=dst(q-1,i)+1; 20 | if prev< dst(q,i) 21 | dst(q,i)=prev; 22 | end 23 | end 24 | 25 | for q=size(s1,1)-1:-1:1 26 | prev=dst(q+1,i)+1; 27 | if prev< dst(q,i) 28 | dst(q,i)=prev; 29 | end 30 | end 31 | tmp=dst(:,i); 32 | tmp(tmp