Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding settable seek range for stabilizing larger shifts #116

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Make sure that you use [unsharp](http://www.ffmpeg.org/ffmpeg-filters.html#unsha
<dd>Set the path to the file used to write the transforms information. Default value is <b>transforms.trf</b>.</dd>
<dt><b>shakiness</b></dt>
<dd>Set the shakiness of input video or quickness of camera. It accepts an integer in the range 1-10, a value of 1 means little shakiness, a value of 10 means strong shakiness. Default value is 5.</dd>
<dt><b>seek_range</b></dt>
<dd>Set the pixel range in which to seek for matches. Default value (-1) means that the range is (1/7) of the smaller dimension of the input. Accepts integers from 0 to 1000</dd>
<dt><b>accuracy</b></dt>
<dd>Set the accuracy of the detection process. It must be a value in the range 1-15. A value of 1 means low accuracy, a value of 15 means high accuracy. Default value is 15.</dd>
<dt><b>stepsize</b></dt>
Expand Down
13 changes: 9 additions & 4 deletions src/motiondetect.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ VSMotionDetectConfig vsMotionDetectGetDefaultConfig(const char* modName){
conf.stepSize = 6;
conf.accuracy = 15;
conf.shakiness = 5;
conf.seek_range = -1;
conf.virtualTripod = 0;
conf.contrastThreshold = 0.25;
conf.show = 0;
Expand Down Expand Up @@ -130,7 +131,11 @@ int vsMotionDetectInit(VSMotionDetect* md, const VSMotionDetectConfig* conf, con
// md->fieldSize = VS_MAX(4,VS_MIN(minDimension/6, (minDimension*md->conf.shakiness)/40));

// fixed size and shift now
int maxShift = VS_MAX(16, minDimension/7);
int maxShift;
if ( md->conf.seek_range < 0 )
maxShift = VS_MAX(16, minDimension/7);
else
maxShift = md->conf.seek_range;
int fieldSize = VS_MAX(16, minDimension/10);
int fieldSizeFine = VS_MAX(6, minDimension/60);
#if defined(USE_SSE2) || defined(USE_SSE2_ASM)
Expand Down Expand Up @@ -288,8 +293,8 @@ int initFields(VSMotionDetect* md, VSMotionDetectFields* fs,
fs->useOffset = 0;
fs->contrastThreshold = contrastThreshold;

int rows = VS_MAX(3,(md->fi.height - fs->maxShift*2)/(size+spacing)-1);
int cols = VS_MAX(3,(md->fi.width - fs->maxShift*2)/(size+spacing)-1);
int rows = VS_MAX(3,(md->fi.height - fs->fieldSize*2)/(size+spacing)-1);
int cols = VS_MAX(3,(md->fi.width - fs->fieldSize*2)/(size+spacing)-1);
// make sure that the remaining rows have the same length
fs->fieldNum = rows * cols;
fs->fieldRows = rows;
Expand All @@ -304,7 +309,7 @@ int initFields(VSMotionDetect* md, VSMotionDetectFields* fs,
// have to be away from the image boundary
// (stepsize is added in case shift is increased through stepsize)
if(keepBorder)
border = size / 2 + fs->maxShift + fs->stepSize;
border = size / 2 + fs->fieldSize + fs->stepSize;
int step_x = (md->fi.width - 2 * border) / VS_MAX(cols-1,1);
int step_y = (md->fi.height - 2 * border) / VS_MAX(rows-1,1);
for (j = 0; j < rows; j++) {
Expand Down
3 changes: 3 additions & 0 deletions src/motiondetect.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
typedef struct _vsmotiondetectconfig {
/* meta parameter for maxshift and fieldsize between 1 and 15 */
int shakiness;
int seek_range; // user-selected maximum range in pixels for finding matches
int accuracy; // meta parameter for number of fields between 1 and 10
int stepSize; // stepsize of field transformation detection
int algo; // deprecated
Expand Down Expand Up @@ -101,6 +102,8 @@ static const char vs_motiondetect_help[] = ""
" (def:inputfile.stab)\n"
" 'shakiness' how shaky is the video and how quick is the camera?\n"
" 1: little (fast) 10: very strong/quick (slow) (def: 5)\n"
" 'seek_range' How far (in pixels) can we search for local matches?\n"
" (def: 1/7 of the smallest image dimension )\n"
" 'accuracy' accuracy of detection process (>=shakiness)\n"
" 1: low (fast) 15: high (slow) (def: 9)\n"
" 'stepsize' stepsize of search process, region around minimum \n"
Expand Down
5 changes: 4 additions & 1 deletion src/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ int vsPrepareFileText(const VSMotionDetect* md, FILE* f){
fprintf(f, "VID.STAB %i\n", LIBVIDSTAB_FILE_FORMAT_VERSION);
fprintf(f, "# accuracy = %d\n", md->conf.accuracy);
fprintf(f, "# shakiness = %d\n", md->conf.shakiness);
fprintf(f, "# seek_range = %d\n", md->conf.seek_range);
fprintf(f, "# stepsize = %d\n", md->conf.stepSize);
fprintf(f, "# mincontrast = %f\n", md->conf.contrastThreshold);
return VS_OK;
Expand All @@ -318,6 +319,7 @@ int vsPrepareFileBinary(const VSMotionDetect* md, FILE* f){
fprintf(f, "TRF%hhu", kFileFormatVersion);
writeInt32(&md->conf.accuracy, f);
writeInt32(&md->conf.shakiness, f);
writeInt32(&md->conf.seek_range, f);
writeInt32(&md->conf.stepSize, f);
writeDouble(&md->conf.contrastThreshold, f);
return VS_OK;
Expand Down Expand Up @@ -386,9 +388,10 @@ int vsReadFileVersionBinary(FILE* f){
unsigned char version;
VSMotionDetectConfig conf;

if(fscanf(f, "TRF%hhu\n", &version)!=LIBVIDSTAB_FILE_FORMAT_VERSION) goto parse_error_handling;
if(fscanf(f, "TRF%hhu", &version)!=LIBVIDSTAB_FILE_FORMAT_VERSION) goto parse_error_handling;
if(readInt32(&conf.accuracy, f)<=0) goto parse_error_handling;
if(readInt32(&conf.shakiness, f)<=0) goto parse_error_handling;
if(readInt32(&conf.seek_range, f)<=0) goto parse_error_handling;
if(readInt32(&conf.stepSize, f)<=0) goto parse_error_handling;
if(readDouble(&conf.contrastThreshold, f)<=0) goto parse_error_handling;

Expand Down
3 changes: 3 additions & 0 deletions transcode/filter_stabilize.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ static int stabilize_configure(TCModuleInstance *self,
optstr_get(options, "fileformat", "%d", &md->serializationMode);
optstr_get(options, "result", "%[^:]", sd->result);
optstr_get(options, "shakiness", "%d", &conf.shakiness);
optstr_get(options, "seek_range", "%d", &conf.seek_range);
optstr_get(options, "accuracy", "%d", &conf.accuracy);
optstr_get(options, "stepsize", "%d", &conf.stepSize);
optstr_get(options, "algo", "%d", &conf.algo);
Expand All @@ -193,6 +194,7 @@ static int stabilize_configure(TCModuleInstance *self,
if (verbose) {
tc_log_info(MOD_NAME, "Image Stabilization Settings:");
tc_log_info(MOD_NAME, " shakiness = %d", conf.shakiness);
tc_log_info(MOD_NAME, " seek_range = %d", conf.seek_range);
tc_log_info(MOD_NAME, " accuracy = %d", conf.accuracy);
tc_log_info(MOD_NAME, " stepsize = %d", conf.stepSize);
tc_log_info(MOD_NAME, " algo = %d", conf.algo);
Expand Down Expand Up @@ -303,6 +305,7 @@ static int stabilize_inspect(TCModuleInstance *self,
vsMotionDetectGetConfig(&conf,md);

CHECKPARAM("shakiness","shakiness=%d", conf.shakiness);
CHECKPARAM("seek_range","seek_range=%d", conf.seek_range);
CHECKPARAM("accuracy", "accuracy=%d", conf.accuracy);
CHECKPARAM("stepsize", "stepsize=%d", conf.stepSize);
CHECKPARAM("algo", "algo=%d", conf.algo);
Expand Down