Commit b8e0c0f6 authored by Zhiping Jiang (蒋志平)'s avatar Zhiping Jiang (蒋志平) 💬

move complex operations into OpenCVExtensions

parent bfb7fae0
......@@ -4,7 +4,6 @@ ELSEIF (APPLE)
SET(ScreenCameraInfo_Source os_dependent/ScreenCameraInfo_Mac.cpp)
ENDIF ()
FILE(GLOB OpenCVExtensions_SOURCES
*.* ${ScreenCameraInfo_Source})
FILE(GLOB OpenCVExtensions_SOURCES *.* ${ScreenCameraInfo_Source})
ADD_LIBRARY(OpenCVExtensions SHARED ${OpenCVExtensions_SOURCES})
TARGET_LINK_LIBRARIES(OpenCVExtensions ${OpenCV_LIBS})
\ No newline at end of file
TARGET_LINK_LIBRARIES(OpenCVExtensions ${OpenCV_LIBS})
//
// CvComplex.cpp
// OpenCVExtensions
//
// Created by 蒋志平 on 2017/12/6.
//
#include "CvComplex.h"
namespace cve {
namespace complex {
cv::Mat1d magnitude(const cv::Mat2d & csi) {
auto magnitude = cv::Mat1d(csi.size());
for(auto i = 0 ; i < csi.total(); i++) {
magnitude(i) = cv::norm(csi(i));
}
return magnitude;
}
cv::Mat1d phase(const cv::Mat2d & csi) {
auto phase = cv::Mat1d(csi.size());
for(auto i = 0 ; i < csi.total(); i++) {
auto complexNumber = csi(i);
phase(i) = atan2(complexNumber(2), complexNumber(1));
}
return phase;
}
cv::Mat1d unwrap(const cv::Mat1d & phase) {
#define MAX_LENGTH 10000
static double dp[MAX_LENGTH];
static double dps[MAX_LENGTH];
static double dp_corr[MAX_LENGTH];
static double cumsum[MAX_LENGTH];
int j = 0;
int N = phase.total();
cv::Mat1d unwrapped_phase;
phase.copyTo(unwrapped_phase);
memset(dp, 0, N);
memset(dps, 0, N);
memset(dp_corr, 0, N);
memset(cumsum, 0, N);
assert(N < MAX_LENGTH);
// incremental phase variation
// MATLAB: dp = diff(p, 1, 1);
for (j = 0; j < N-1; j++)
dp[j] = phase(j+1) - phase(j);
// equivalent phase variation in [-pi, pi]
// MATLAB: dps = mod(dp+dp,2*pi) - pi;
for (j = 0; j < N-1; j++)
dps[j] = (dp[j]+M_PI) - floor((dp[j]+M_PI) / (2*M_PI))*(2*M_PI) - M_PI;
// preserve variation sign for +pi vs. -pi
// MATLAB: dps(dps==pi & dp>0,:) = pi;
for (j = 0; j < N-1; j++)
if ((dps[j] == -M_PI) && (dp[j] > 0))
dps[j] = M_PI;
// incremental phase correction
// MATLAB: dp_corr = dps - dp;
for (j = 0; j < N-1; j++)
dp_corr[j] = dps[j] - dp[j];
// Ignore correction when incremental variation is smaller than cutoff
// MATLAB: dp_corr(abs(dp)<cutoff,:) = 0;
for (j = 0; j < N-1; j++)
if (fabs(dp[j]) < M_PI)
dp_corr[j] = 0;
// Find cumulative sum of deltas
// MATLAB: cumsum = cumsum(dp_corr, 1);
cumsum[0] = dp_corr[0];
for (j = 1; j < N-1; j++)
cumsum[j] = cumsum[j-1] + dp_corr[j];
// Integrate corrections and add to P to produce smoothed phase values
// MATLAB: p(2:m,:) = p(2:m,:) + cumsum(dp_corr,1);
for (j = 1; j < N; j++)
unwrapped_phase(j) += cumsum[j-1];
#undef MAX_LENGTH
return unwrapped_phase;
}
cv::Mat1d linSpace(double start, double end, double interval) {
std::vector<double> result;
for (double s= start; s<end; s+=interval) {
result.push_back(s);
}
return cv::Mat1d(result);
}
}
}
//
// CvComplex.h
// OpenCVExtensions
//
// Created by 蒋志平 on 2017/12/6.
//
#ifndef CvComplex_h
#define CvComplex_h
#include <iostream>
#include <opencv2/opencv.hpp>
namespace cve{
namespace complex {
cv::Mat1d magnitude(const cv::Mat2d & csi);
cv::Mat1d phase(const cv::Mat2d & csi);
/**
* Mimic of Matlab unwrap operation, should produce identical result as Matlab.
*
* the original version of code is from: https://www.medphysics.wisc.edu/~ethan/phaseunwrap/
*
* @param p data
* @param N data length
*/
cv::Mat1d unwrap(const cv::Mat1d & phase);
cv::Mat1d linSpace(double start, double end, double interval);
}
}
#endif /* CvComplex_h */
......@@ -12,6 +12,7 @@
#include "OpencvMatxExtensions.h"
#include "MatxGeometry.h"
#include "CvTypeConversions.h"
#include "CvComplex.h"
#include "jzplib_core.h"
#include "jzplib_camera.h"
#include "jzplib_draw.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment