paillou_demo.cpp 3.58 KB
Newer Older
LaurentBerger's avatar
LaurentBerger committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install,
 *  copy or use the software.
 *
 *
 *  License Agreement
 *  For Open Source Computer Vision Library
 *  (3 - clause BSD License)
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met :
 *
14
 *  * Redistributions of source code must retain the above copyright notice,
LaurentBerger's avatar
LaurentBerger committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *  this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *  this list of conditions and the following disclaimer in the documentation
 *  and / or other materials provided with the distribution.
 *
 *  * Neither the names of the copyright holders nor the names of the contributors
 *  may be used to endorse or promote products derived from this software
 *  without specific prior written permission.
 *
 *  This software is provided by the copyright holders and contributors "as is" and
 *  any express or implied warranties, including, but not limited to, the implied
 *  warranties of merchantability and fitness for a particular purpose are disclaimed.
 *  In no event shall copyright holders or contributors be liable for any direct,
 *  indirect, incidental, special, exemplary, or consequential damages
 *  (including, but not limited to, procurement of substitute goods or services;
 *  loss of use, data, or profits; or business interruption) however caused
 *  and on any theory of liability, whether in contract, strict liability,
 *  or tort(including negligence or otherwise) arising in any way out of
 *  the use of this software, even if advised of the possibility of such damage.
 */

#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ximgproc.hpp>
#include "opencv2/ximgproc/paillou_filter.hpp"

using namespace cv;
using namespace cv::ximgproc;

#include <iostream>
using namespace std;

int aa = 100, ww = 10;
LaurentBerger's avatar
LaurentBerger committed
50

LaurentBerger's avatar
LaurentBerger committed
51 52 53 54
const char* window_name = "Gradient Modulus";

static void DisplayImage(Mat x,string s)
{
berak's avatar
berak committed
55 56 57 58 59 60 61 62 63 64 65 66
    vector<Mat> sx;
    split(x, sx);
    vector<double> minVal(3), maxVal(3);
    for (int i = 0; i < static_cast<int>(sx.size()); i++)
    {
        minMaxLoc(sx[i], &minVal[i], &maxVal[i]);
    }
    maxVal[0] = *max_element(maxVal.begin(), maxVal.end());
    minVal[0] = *min_element(minVal.begin(), minVal.end());
    Mat uc;
    x.convertTo(uc, CV_8U,255/(maxVal[0]-minVal[0]),-255*minVal[0]/(maxVal[0]-minVal[0]));
    imshow(s, uc);
LaurentBerger's avatar
LaurentBerger committed
67 68 69 70 71 72 73
}


/**
 * @function paillouFilter
 * @brief Trackbar callback
 */
berak's avatar
berak committed
74
static void PaillouFilter(int, void*pm)
LaurentBerger's avatar
LaurentBerger committed
75
{
berak's avatar
berak committed
76
    Mat img = *((Mat*)pm);
LaurentBerger's avatar
LaurentBerger committed
77
    Mat dst;
berak's avatar
berak committed
78
    double a=aa/100.0, w=ww/100.0;
LaurentBerger's avatar
LaurentBerger committed
79
    Mat rx,ry;
berak's avatar
berak committed
80 81
    GradientPaillouX(img, rx, a, w);
    GradientPaillouY(img, ry, a, w);
LaurentBerger's avatar
LaurentBerger committed
82 83
    DisplayImage(rx, "Gx");
    DisplayImage(ry, "Gy");
berak's avatar
berak committed
84 85
    add(rx.mul(rx), ry.mul(ry), dst);
    sqrt(dst, dst);
LaurentBerger's avatar
LaurentBerger committed
86 87 88 89 90 91
    DisplayImage(dst, window_name );
}


int main(int argc, char* argv[])
{
berak's avatar
berak committed
92 93 94 95 96 97 98
    if (argc < 2)
    {
        cout << "usage: paillou_demo [image]" << endl;
        return 1;
    }
    Mat img = imread(argv[1]);
    if (img.empty())
LaurentBerger's avatar
LaurentBerger committed
99 100
    {
        cout << "File not found or empty image\n";
berak's avatar
berak committed
101
        return 1;
LaurentBerger's avatar
LaurentBerger committed
102
    }
berak's avatar
berak committed
103
    imshow("Original",img);
LaurentBerger's avatar
LaurentBerger committed
104 105 106
    namedWindow( window_name, WINDOW_AUTOSIZE );

    /// Create a Trackbar for user to enter threshold
berak's avatar
berak committed
107 108 109
    createTrackbar( "a:",window_name, &aa, 400, PaillouFilter, &img );
    createTrackbar( "w:", window_name, &ww, 400, PaillouFilter, &img );
    PaillouFilter(0, &img);
LaurentBerger's avatar
LaurentBerger committed
110 111
    waitKey();
    return 0;
berak's avatar
berak committed
112
}