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

  • 相关阅读:
    HSF的原理分析
    python--cryptography加密
    PyQt5--QTDesigner--UI资源
    PyQt5--QTDesigner--UI样式表
    python--struct
    python--ffmpy3与ffmpeg
    python--you-get:视频下载
    python--base64--简单的加密和解密
    python--StringIO-在内存中读写str
    电脑技巧
  • 原文地址:https://www.cnblogs.com/wangmenghan/p/5518568.html
Copyright © 2011-2022 走看看