#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
const int w = 384;
const int h = 288;
const int n = 3;
void SAD(uchar* Limg,
uchar* Rimg,
uchar* Oimg)
{
for (int y = 0; y< h; y++)
{
for (int x = 0; x< w; x++){
unsigned int bestCost = 999999;
unsigned int bestDisparity = 0;
for (int d = 0; d <= w-x-5; d++)
{
unsigned int cost = 0;
for (int i = -n; i <= n; i++)
{
for (int j = -n; j <= n; j++)
{
int yy, xx, xxd;
yy = y + i;
if (yy < 0) yy = 0;
if (yy >= h) yy = h;
xx = x + j;
if (xx < 0) xx = 0;
if (xx >= w) xx = w;
xxd = xx - d;
if (xxd < 0) xxd = 0;
if (xxd >= w) xxd = w;
cost += abs((int)(Limg[yy*w + xx] - Rimg[yy*w + xxd]));
}
}
if (cost < bestCost)
{
bestCost = cost;
bestDisparity = d*d;
}
Oimg[y*w + x] = bestDisparity;
}
}
}
}

int main()
{
Mat imL, imR, imO;
imL = imread("C:\\Users\\Administrator\\Desktop\\im2.png", 0);
if (imL.empty())
{
return -1;
}
imR = imread("C:\\Users\\Administrator\\Desktop\\im6.png", 0);
if (imR.empty())
{
return -1;
}
imO.create(imL.rows, imL.cols, imL.type());
SAD(imL.data, imR.data, imO.data);
namedWindow("left", WINDOW_AUTOSIZE);
namedWindow("right", WINDOW_AUTOSIZE);
namedWindow("Output", WINDOW_AUTOSIZE);
imshow("Output", imO);
imshow("left", imL);
imshow("right", imR);
waitKey(0);
return 0;
}

SAD算法在opencv上的实现代码(c++)的更多相关文章

  1. (转载)利用SIFT和RANSAC算法(openCV框架)实现物体的检测与定位,并求出变换矩阵(findFundamentalMat和findHomography的比较) 置顶

    原文链接:https://blog.csdn.net/qq_25352981/article/details/46914837#commentsedit 本文目标是通过使用SIFT和RANSAC算法, ...

  2. [转]Android通过NDK调用JNI,使用opencv做本地c++代码开发配置方法

    原文地址:http://blog.csdn.net/watkinsong/article/details/9849973 有一种方式不需要自己配置所有的Sun JDK, Android SDK以及ND ...

  3. C++算法之大数加法计算的代码

    如下代码段是关于C++算法之大数加法计算的代码,希望对大家有用. { int length; int index; int smaller; int prefix = 0; if(NULL == sr ...

  4. Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码

    以前写过两个Android开发配置文档,使用NDK进行JNI开发,这样能够利用以前已经写好的C++代码. 前两篇博客地址: http://blog.csdn.net/watkinsong/articl ...

  5. 基本算法思想Java实现的详细代码

    基本算法思想Java实现的详细代码 算法是一个程序的灵魂,一个好的算法往往可以化繁为简,高效的求解问题.在程序设计中算法是独立于语言的,无论使用哪一种语言都可以使用这些算法,本文笔者将以Java语言为 ...

  6. 在Eclipse上使用egit插件通过ssh协议方式上传项目代码的具体步骤

    在Eclipse上使用egit插件通过ssh协议方式上传项目代码 前戏: 使用ssh方式可以不通过https协议,避免直接提供账号密码的方式上传项目到git在线服务器,如Bitbucket.GitHu ...

  7. 基于dsp_builder的算法在FPGA上的实现

    基于dsp_builder的算法在FPGA上的实现   一.摘要 结合dsp_builder.matlab.modelsim和quartus ii等软件完成算法的FPGA实现. 二.实验平台 硬件平台 ...

  8. 【优化算法】Greedy Randomized Adaptive Search算法 超详细解析,附代码实现TSP问题求解

    01 概述 Greedy Randomized Adaptive Search,贪婪随机自适应搜索(GRAS),是组合优化问题中的多起点元启发式算法,在算法的每次迭代中,主要由两个阶段组成:构造(co ...

  9. 清晰化算法在DSP上的实现

    清晰化算法在DSP TIDM642上的实现,之前的部分工作摘要于此. 1 DSP平台的选择 1.1 DM642 Evolution Module 选择现有的DM642 Evolution Module ...

随机推荐

  1. poj 3461Oulipo

    题目链接:http://poj.org/problem?id=3461 统计字符串出现的次数 #include<cstdio> #include<iostream> #incl ...

  2. 写了个简单的pdo的封装类

    <?php class PD { //造对象 public $dsn = "mysql:dbname=test2;host=localhost"; //数据库类型,数据库名和 ...

  3. 重拾smslib

    http://www.tuicool.com/articles/mm2yQrN http://blog.csdn.net/ll136078/article/details/8737348 http:/ ...

  4. 与你相遇好幸运,MongoDB小技巧

    保存为bat方便: "C://Program Files//MongoDB//Server//3.2//bin//mongod.exe" --dbpath=D://corp//db ...

  5. C# 面试宝典

    1.简述 private. protected. public. internal 修饰符的访问权限. private  私有成员 只有类成员才能访问 protected  保护成员 只有该类及该类的 ...

  6. Delphi中函数定义和声明的位置

    当函数(或过程)A定义在函数(或过程)B之前,那么函数B就可以调用函数A,并且编译成功,例如下面的 procedure TForm1.btn1Click(Sender: TObject); 和   f ...

  7. MVC4 自定义错误页面(三)

    一.概述 MVC4框架自带了定义错误页,该页面位于Shared/Error,该页面能够显示系统未能捕获的异常,如何才能使用该页面: 二.使用步骤: 1.配置WebConfig文件,在System.We ...

  8. SimpleHashTable

    简单的Hash Table 实现,下次被问到,至少不是从0开始.不过笔试问这个毕竟不多. public struct Item<K, V> { public K Key { get; se ...

  9. js this的使用举例

    js this的使用举例 <script type="text/javascript"> function test(obj){ obj.style.width= ob ...

  10. [Linux][VMWare] 学习笔记之安装Linux系统-网络配置

    最近开始折腾Linux,在本机装了个VMWare和Centos,装完之后虚拟机里面的OS可以上网,但是使用SecureCRT连接不上虚拟机,开始折腾这个网络. vmware安装好以后,会自动添加两张网 ...