Ask Your Question

Revision history [back]

It's a gift! (with this license)

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>

using namespace std;
using namespace cv;

struct ParamThresh {
    int highFreq;
    String winName;
    Mat m1,m1f,tfM1f;
    Mat m2,m2f, tfM2f;
    Mat fDist;
};

void AjouteGlissiere(String nomGlissiere, String nomFenetre, int minGlissiere, int maxGlissiere, int valeurDefaut, int *valGlissiere, void(*f)(int, void *), void *r)
{
    createTrackbar(nomGlissiere, nomFenetre, valGlissiere, 1, f, r);
    setTrackbarMin(nomGlissiere, nomFenetre, minGlissiere);
    setTrackbarMax(nomGlissiere, nomFenetre, maxGlissiere);
    setTrackbarPos(nomGlissiere, nomFenetre, valeurDefaut);
}

void ChooseThreshold(int x, void *r)
{
    ParamThresh *pgc = (ParamThresh*)r;
    if (!pgc->m1.empty() && pgc->m1.size() == pgc->m2.size())
    {
        Mat tfR = Mat::zeros(pgc->tfM1f.size(), pgc->tfM1f.type());
        Mat mask = pgc->fDist > pgc->highFreq;
        Mat f = pgc->tfM1f.clone(),res,dst;
        pgc->tfM2f.copyTo(f, mask);
        idft(f, res, cv::DFT_SCALE | DFT_REAL_OUTPUT);
        res.convertTo(dst, CV_8UC1);
        imshow(pgc->winName, dst);
        waitKey(50);
    }
}

int main (int argc,char **argv)
{
        ParamThresh pgc;
        pgc.m1 = imread("g:/lib/opencv/samples/data/apple.jpg", IMREAD_GRAYSCALE);
        pgc.m2 = imread("g:/lib/opencv/samples/data/baboon.jpg", IMREAD_GRAYSCALE);
        pgc.winName = "Hybrid";
        CV_Assert(pgc.m1.size() == pgc.m2.size() && pgc.m1.rows==pgc.m1.cols);
        imshow("original1", pgc.m1);
        imshow("original2", pgc.m2);
        pgc.highFreq =pgc.m1.rows / 2;

        namedWindow(pgc.winName, WINDOW_GUI_EXPANDED);
        AjouteGlissiere("thresh", pgc.winName, 0, pgc.m1.rows/2, pgc.m1.rows /2, &pgc.highFreq, ChooseThreshold, (void*)&pgc);
        waitKey(20);
        pgc.m1.convertTo(pgc.m1f, CV_32F);
        pgc.m2.convertTo(pgc.m2f, CV_32F);
        pgc.fDist = Mat(pgc.m2f.size(), CV_32F);
        for (int i = 0; i<=pgc.fDist.rows / 2; i++)
            for (int j = 0; j <= pgc.fDist.cols / 2; j++)
            {
                float d = sqrt(i*i*1.0 + j * j*1.0);
                pgc.fDist.at<float>(i, j) = d;
                if (i != 0)
                    pgc.fDist.at<float>(pgc.fDist.rows - i, j) = d;
                if (j != 0)
                    pgc.fDist.at<float>(i, pgc.fDist.cols - j) = d;
                if (j != 0 && i != 0)
                    pgc.fDist.at<float>(pgc.fDist.rows - i, pgc.fDist.cols - j) = d;
            }

        dft(pgc.m1f, pgc.tfM1f, cv::DFT_COMPLEX_OUTPUT);
        dft(pgc.m2f, pgc.tfM2f, cv::DFT_COMPLEX_OUTPUT);
        char code = 0;
        while (code != 27)
            code = waitKey(10);

}

It's a gift! (with this license)

image description image from https://github.com/opencv/opencv/wiki/ChangeLog#version34

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>

using namespace std;
using namespace cv;

struct ParamThresh {
    int highFreq;
    String winName;
    Mat m1,m1f,tfM1f;
    Mat m2,m2f, tfM2f;
    Mat fDist;
};

void AjouteGlissiere(String nomGlissiere, String nomFenetre, int minGlissiere, int maxGlissiere, int valeurDefaut, int *valGlissiere, void(*f)(int, void *), void *r)
{
    createTrackbar(nomGlissiere, nomFenetre, valGlissiere, 1, f, r);
    setTrackbarMin(nomGlissiere, nomFenetre, minGlissiere);
    setTrackbarMax(nomGlissiere, nomFenetre, maxGlissiere);
    setTrackbarPos(nomGlissiere, nomFenetre, valeurDefaut);
}

void ChooseThreshold(int x, void *r)
{
    ParamThresh *pgc = (ParamThresh*)r;
    if (!pgc->m1.empty() && pgc->m1.size() == pgc->m2.size())
    {
        Mat tfR = Mat::zeros(pgc->tfM1f.size(), pgc->tfM1f.type());
        Mat mask = pgc->fDist > pgc->highFreq;
        Mat f = pgc->tfM1f.clone(),res,dst;
        pgc->tfM2f.copyTo(f, mask);
        idft(f, res, cv::DFT_SCALE | DFT_REAL_OUTPUT);
        res.convertTo(dst, CV_8UC1);
        imshow(pgc->winName, dst);
        waitKey(50);
    }
}

int main (int argc,char **argv)
{
        ParamThresh pgc;
        pgc.m1 = imread("g:/lib/opencv/samples/data/apple.jpg", IMREAD_GRAYSCALE);
        pgc.m2 = imread("g:/lib/opencv/samples/data/baboon.jpg", IMREAD_GRAYSCALE);
        pgc.winName = "Hybrid";
        CV_Assert(pgc.m1.size() == pgc.m2.size() && pgc.m1.rows==pgc.m1.cols);
        imshow("original1", pgc.m1);
        imshow("original2", pgc.m2);
        pgc.highFreq =pgc.m1.rows / 2;

        namedWindow(pgc.winName, WINDOW_GUI_EXPANDED);
        AjouteGlissiere("thresh", pgc.winName, 0, pgc.m1.rows/2, pgc.m1.rows /2, &pgc.highFreq, ChooseThreshold, (void*)&pgc);
        waitKey(20);
        pgc.m1.convertTo(pgc.m1f, CV_32F);
        pgc.m2.convertTo(pgc.m2f, CV_32F);
        pgc.fDist = Mat(pgc.m2f.size(), CV_32F);
        for (int i = 0; i<=pgc.fDist.rows / 2; i++)
            for (int j = 0; j <= pgc.fDist.cols / 2; j++)
            {
                float d = sqrt(i*i*1.0 + j * j*1.0);
                pgc.fDist.at<float>(i, j) = d;
                if (i != 0)
                    pgc.fDist.at<float>(pgc.fDist.rows - i, j) = d;
                if (j != 0)
                    pgc.fDist.at<float>(i, pgc.fDist.cols - j) = d;
                if (j != 0 && i != 0)
                    pgc.fDist.at<float>(pgc.fDist.rows - i, pgc.fDist.cols - j) = d;
            }

        dft(pgc.m1f, pgc.tfM1f, cv::DFT_COMPLEX_OUTPUT);
        dft(pgc.m2f, pgc.tfM2f, cv::DFT_COMPLEX_OUTPUT);
        char code = 0;
        while (code != 27)
            code = waitKey(10);

}