125 lines
2.9 KiB
C++
125 lines
2.9 KiB
C++
#include <opencv2/opencv.hpp>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <chrono>
|
|
|
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
|
|
#define IS_WIN32
|
|
#endif
|
|
|
|
#ifdef __ANDROID__
|
|
#include <android/log.h>
|
|
#endif
|
|
|
|
#ifdef IS_WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
#if defined(__GNUC__)
|
|
// Attributes to prevent 'unused' function from being removed and to make it visible
|
|
#define FUNCTION_ATTRIBUTE __attribute__((visibility("default"))) __attribute__((used))
|
|
#elif defined(_MSC_VER)
|
|
// Marking a function for export
|
|
#define FUNCTION_ATTRIBUTE __declspec(dllexport)
|
|
#endif
|
|
|
|
using namespace cv;
|
|
using namespace std;
|
|
|
|
long long int get_now()
|
|
{
|
|
return chrono::duration_cast<std::chrono::milliseconds>(
|
|
chrono::system_clock::now().time_since_epoch())
|
|
.count();
|
|
}
|
|
|
|
void platform_log(const char *fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
#ifdef __ANDROID__
|
|
__android_log_vprint(ANDROID_LOG_VERBOSE, "ndk", fmt, args);
|
|
#elif defined(IS_WIN32)
|
|
char *buf = new char[4096];
|
|
std::fill_n(buf, 4096, '\0');
|
|
_vsprintf_p(buf, 4096, fmt, args);
|
|
OutputDebugStringA(buf);
|
|
delete[] buf;
|
|
#else
|
|
vprintf(fmt, args);
|
|
#endif
|
|
va_end(args);
|
|
}
|
|
|
|
// Avoiding name mangling
|
|
extern "C"
|
|
{
|
|
FUNCTION_ATTRIBUTE
|
|
const char *version()
|
|
{
|
|
return CV_VERSION;
|
|
}
|
|
|
|
FUNCTION_ATTRIBUTE
|
|
void process_image(char *inputImagePath, char *outputImagePath, double frame_rate)
|
|
{
|
|
platform_log("Starting the process");
|
|
long long start = get_now();
|
|
|
|
//Video Capture
|
|
VideoCapture cap = VideoCapture(inputImagePath);
|
|
|
|
platform_log("DURRRR %f",frame_rate);
|
|
const double scale = 1.0 / 16.0;
|
|
|
|
//Working Image Containers
|
|
Mat src;
|
|
Mat dst;
|
|
|
|
//Background sub
|
|
Mat fgMask;
|
|
auto subtractor = createBackgroundSubtractorMOG2(150, 100);
|
|
Size k_size = Size(1, 1);
|
|
|
|
//Centroid Variables
|
|
double posx = 0.0;
|
|
double posy = 0.0;
|
|
const double alpha = 0.995;
|
|
Moments mom;
|
|
double cX;
|
|
double cY;
|
|
|
|
//Drawing
|
|
const Scalar color = Scalar(0, 0, 255);
|
|
|
|
//Loop Management
|
|
|
|
|
|
while (true)
|
|
{
|
|
cap >> src;
|
|
if (src.empty())
|
|
{
|
|
break;
|
|
}
|
|
resize(src, src, Size(), scale, scale);
|
|
blur(src, dst, k_size);
|
|
subtractor->apply(src, fgMask);
|
|
|
|
mom = moments(fgMask);
|
|
if (mom.m00 != 0)
|
|
{
|
|
cX = mom.m10 / mom.m00;
|
|
cY = mom.m01 / mom.m00;
|
|
posx = posx * alpha + cX * (1.0 - alpha);
|
|
posy = posy * alpha + cY * (1.0 - alpha);
|
|
}
|
|
|
|
platform_log("Positions %f %f %f", posx, posy, mom.m00/(src.size[0] * src.size[1]* 255));
|
|
}
|
|
|
|
int evalInMillis = static_cast<int>(get_now() - start);
|
|
platform_log("Processing done in %dms\n", evalInMillis);
|
|
}
|
|
}
|