Skip to content

Commit

Permalink
maxLevel 4->3
Browse files Browse the repository at this point in the history
  • Loading branch information
XuHg-zjcn committed Oct 6, 2020
1 parent 79d0c1d commit 9a24f86
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../sdk/native/jni)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../FFTW_Install)
set(FFTW3_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../fftw-3.3.8)
add_subdirectory(${FFTW3_DIR} ${CMAKE_CURRENT_BINARY_DIR}/fftw_build)
include_directories(${FFTW3_DIR}/api)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
Expand All @@ -24,7 +28,8 @@ add_library( # Sets the name of the library.
# Provides a relative path to your source file(s).
native-lib.cpp
ImageProcess.cpp
OptFlow.cpp)
OptFlow.cpp
optflow_FFT.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
Expand All @@ -47,6 +52,7 @@ target_link_libraries( # Specifies the target library.
native-lib
jnigraphics
${OpenCV_LIBRARIES}
fftw3
# Links the target library to the log library
# included in the NDK.
${log-lib} )
100 changes: 100 additions & 0 deletions app/src/main/cpp/optflow_FFT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// Created by xrj on 20-10-5.
//

#include "optflow_FFT.h"
#include "fftw3.h"
optflow_FFT::optflow_FFT(uint32_t n)
{
//ctor
this->n = n;
crop_db = (double*)fftw_malloc(sizeof(double)*n*n);
out1 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*n*(n/2+1));
out2 = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*n*(n/2+1));
mul = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*n*(n/2+1));
ifft = (double*)fftw_malloc(sizeof(double)*n*n);
if(fftw_import_wisdom_from_filename("wisdom.fftw")!=0){
p1 = fftw_plan_dft_r2c_2d(n, n, crop_db, out1, FFTW_WISDOM_ONLY);
p2 = fftw_plan_dft_r2c_2d(n, n, crop_db, out2, FFTW_WISDOM_ONLY);
p_ifft = fftw_plan_dft_c2r_2d(n, n, mul, ifft, FFTW_WISDOM_ONLY);
}else{
p1 = fftw_plan_dft_r2c_2d(n, n, crop_db, out1, FFTW_PATIENT);
p2 = fftw_plan_dft_r2c_2d(n, n, crop_db, out2, FFTW_PATIENT);
p_ifft = fftw_plan_dft_c2r_2d(n, n, mul, ifft, FFTW_PATIENT);
}
}

optflow_FFT::~optflow_FFT()
{
//dtor
fftw_free(crop_db);
fftw_free(out1);
fftw_free(out2);
fftw_destroy_plan(p1);
fftw_destroy_plan(p2);
}

void optflow_FFT::run(uint32_t n)
{
if(n==0)
fftw_execute(p1);
else
fftw_execute(p2);
}

int optflow_FFT::save()
{
FILE *fp;
fp = fopen("wisdom.fftw", "w");
if(fp==NULL){
return 1;
}
fftw_export_wisdom_to_file(fp);
fclose(fp);
return 0;
}

void optflow_FFT::fill_data(Mat &mat_in, uint32_t x0, uint32_t y0)
{
uint8_t *ptr_row;
double *ptr_db=crop_db;
for(uint32_t i=y0;i<y0+n;i++) {
ptr_row = mat_in.ptr(i, x0);
for(uint32_t j=0;j<n;j++) {
*ptr_db++=*ptr_row++;
}
}
}

void optflow_FFT::calc_delta()
{
double mul_real, mul_imag, sqrt2;
for(uint32_t i=0;i<64*33;i++) {
mul_real = out1[i][0]*out2[i][0] + out1[i][1]*out2[i][1];
mul_imag =-out1[i][0]*out2[i][1] + out1[i][1]*out2[i][0];
sqrt2 = sqrt(mul_real*mul_real + mul_imag*mul_imag);
mul[i][0] = mul_real/sqrt2;
mul[i][1] = mul_imag/sqrt2;
}
fftw_execute(p_ifft);
}

void optflow_FFT::copy_result(uint8_t* p)
{
double abs_v;
for(uint32_t i=0;i<64*64;i++) {
abs_v = ifft[i]/20;
abs_v += 32;
abs_v = abs_v<0 ? 0 : abs_v;
abs_v = abs_v>255 ? 255 : abs_v;
*p++ = (uint8_t)abs_v;
}
/*for(uint32_t i=0;i<64*33;i++) {
abs_v = mul[i][0];
abs_v = abs_v/4 + 128;
abs_v = abs_v<0 ? 0 : abs_v;
abs_v = abs_v>255 ? 255 : abs_v;
cout<<abs_v<<endl;
*p++ = (uint8_t)abs_v;
}*/
}
39 changes: 39 additions & 0 deletions app/src/main/cpp/optflow_FFT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Created by xrj on 20-10-5.
//

#ifndef SH_3DScaner_OPTFLOW_FFT_H
#define SH_3DScaner_OPTFLOW_FFT_H

#include <cstdint>
#include "fftw3.h"
#include "opencv2/core.hpp"

using namespace cv;
class optflow_FFT
{
public:
optflow_FFT(uint32_t n);
virtual ~optflow_FFT();
void run(uint32_t n);
void fill_data(Mat &in, uint32_t x0, uint32_t y0);
void calc_delta();
void copy_result(uint8_t* p);

protected:
int save();

private:
uint32_t n=0;
fftw_plan p1;
fftw_plan p2;
fftw_plan p_ifft;
double *crop_db;
fftw_complex *out1;
fftw_complex *out2;
fftw_complex *mul;
double *ifft;
};


#endif //SH_3DScaner_OPTFLOW_FFT_H

0 comments on commit 9a24f86

Please sign in to comment.