zoukankan      html  css  js  c++  java
  • SAD算法在opencv上的实现代码(c++)

    #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;
    }

  • 相关阅读:
    初拾Java(问题一:404错误,页面找不到)
    新年新气象--总结过去,展望未来
    接口测试[整理]
    [转]SVN-版本控制软件
    浅谈黑盒测试和白盒测试
    Bug管理工具的使用介绍(Bugger 2016)
    P2805/BZOJ1565 [NOI2009]植物大战僵尸
    近期学习目标
    P3643/BZOJ4584 [APIO2016]划艇
    P5344 【XR-1】逛森林
  • 原文地址:https://www.cnblogs.com/wangmenghan/p/5518568.html
Copyright © 2011-2022 走看看