Small fixes for SimpleFlow algorithm

+ Fixed warnings
+ Add new function calcOpticalFlow with smaller number of arguments
+ Add asserts to algorithm and remove 'exit(1)'
This commit is contained in:
Yury Zemlyanskiy 2012-08-31 13:27:14 +04:00
parent 5ee632fe90
commit 7ad4c25452
5 changed files with 32 additions and 22 deletions

View File

@ -601,6 +601,8 @@ calcOpticalFlowSF
----------- -----------
Calculate an optical flow using "SimpleFlow" algorithm. Calculate an optical flow using "SimpleFlow" algorithm.
.. ocv:function:: void calcOpticalFlowSF( Mat& prev, Mat& next, Mat& flowX, Mat& flowY, int layers, int averaging_block_size, int max_flow)
.. ocv:function:: void calcOpticalFlowSF( Mat& prev, Mat& next, Mat& flowX, Mat& flowY, int layers, int averaging_block_size, int max_flow, double sigma_dist, double sigma_color, int postprocess_window, double sigma_dist_fix, double sigma_color_fix, double occ_thr, int upscale_averaging_radiud, double upscale_sigma_dist, double upscale_sigma_color, double speed_up_thr) .. ocv:function:: void calcOpticalFlowSF( Mat& prev, Mat& next, Mat& flowX, Mat& flowY, int layers, int averaging_block_size, int max_flow, double sigma_dist, double sigma_color, int postprocess_window, double sigma_dist_fix, double sigma_color_fix, double occ_thr, int upscale_averaging_radiud, double upscale_sigma_dist, double upscale_sigma_color, double speed_up_thr)
:param prev: First 8-bit 3-channel image. :param prev: First 8-bit 3-channel image.

View File

@ -328,6 +328,13 @@ CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst,
bool fullAffine); bool fullAffine);
//! computes dense optical flow using Simple Flow algorithm //! computes dense optical flow using Simple Flow algorithm
CV_EXPORTS_W void calcOpticalFlowSF(Mat& from,
Mat& to,
Mat& flow,
int layers,
int averaging_block_size,
int max_flow);
CV_EXPORTS_W void calcOpticalFlowSF(Mat& from, CV_EXPORTS_W void calcOpticalFlowSF(Mat& from,
Mat& to, Mat& to,
Mat& flow, Mat& flow,

View File

@ -72,26 +72,27 @@ static void removeOcclusions(const Mat& flow,
} }
static void wd(Mat& d, int top_shift, int bottom_shift, int left_shift, int right_shift, float sigma) { static void wd(Mat& d, int top_shift, int bottom_shift, int left_shift, int right_shift, float sigma) {
const float factor = 1.0 / (2.0 * sigma * sigma);
for (int dr = -top_shift, r = 0; dr <= bottom_shift; ++dr, ++r) { for (int dr = -top_shift, r = 0; dr <= bottom_shift; ++dr, ++r) {
for (int dc = -left_shift, c = 0; dc <= right_shift; ++dc, ++c) { for (int dc = -left_shift, c = 0; dc <= right_shift; ++dc, ++c) {
d.at<float>(r, c) = -(dr*dr + dc*dc) * factor; d.at<float>(r, c) = -(dr*dr + dc*dc);
} }
} }
d *= 1.0 / (2.0 * sigma * sigma);
exp(d, d); exp(d, d);
} }
static void wc(const Mat& image, Mat& d, int r0, int c0, static void wc(const Mat& image, Mat& d, int r0, int c0,
int top_shift, int bottom_shift, int left_shift, int right_shift, float sigma) { int top_shift, int bottom_shift, int left_shift, int right_shift, float sigma) {
const float factor = 1.0 / (2.0 * sigma * sigma);
const Vec3b centeral_point = image.at<Vec3b>(r0, c0); const Vec3b centeral_point = image.at<Vec3b>(r0, c0);
int left_border = c0-left_shift, right_border = c0+right_shift;
for (int dr = r0-top_shift, r = 0; dr <= r0+bottom_shift; ++dr, ++r) { for (int dr = r0-top_shift, r = 0; dr <= r0+bottom_shift; ++dr, ++r) {
const Vec3b *row = image.ptr<Vec3b>(dr); const Vec3b *row = image.ptr<Vec3b>(dr);
float *d_row = d.ptr<float>(r); float *d_row = d.ptr<float>(r);
for (int dc = c0-left_shift, c = 0; dc <= c0+right_shift; ++dc, ++c) { for (int dc = left_border, c = 0; dc <= right_border; ++dc, ++c) {
d_row[c] = -dist(centeral_point, row[dc]) * factor; d_row[c] = -dist(centeral_point, row[dc]);
} }
} }
d *= 1.0 / (2.0 * sigma * sigma);
exp(d, d); exp(d, d);
} }
@ -163,7 +164,7 @@ static void calcOpticalFlowSingleScaleSF(const Mat& prev,
Mat diff_storage(averaging_radius*2 + 1, averaging_radius*2 + 1, CV_32F); Mat diff_storage(averaging_radius*2 + 1, averaging_radius*2 + 1, CV_32F);
Mat w_full_window(averaging_radius*2 + 1, averaging_radius*2 + 1, CV_32F); Mat w_full_window(averaging_radius*2 + 1, averaging_radius*2 + 1, CV_32F);
Mat wd_full_window(averaging_radius*2 + 1, averaging_radius*2 + 1, CV_32F); Mat wd_full_window(averaging_radius*2 + 1, averaging_radius*2 + 1, CV_32F);
float w_full_window_sum; float w_full_window_sum = 1e-9;
Mat prev_extended; Mat prev_extended;
copyMakeBorder(prev, prev_extended, copyMakeBorder(prev, prev_extended,
@ -197,7 +198,7 @@ static void calcOpticalFlowSingleScaleSF(const Mat& prev,
} }
bool first_flow_iteration = true; bool first_flow_iteration = true;
float sum_e, min_e; float sum_e = 0, min_e = 0;
for (int u = min_row_shift; u <= max_row_shift; ++u) { for (int u = min_row_shift; u <= max_row_shift; ++u) {
for (int v = min_col_shift; v <= max_col_shift; ++v) { for (int v = min_col_shift; v <= max_col_shift; ++v) {
@ -286,7 +287,7 @@ static Mat upscaleOpticalFlow(int new_rows,
int averaging_radius, int averaging_radius,
float sigma_dist, float sigma_dist,
float sigma_color) { float sigma_color) {
crossBilateralFilter(flow, image, confidence, flow, averaging_radius, sigma_color, sigma_dist, false); crossBilateralFilter(flow, image, confidence, flow, averaging_radius, sigma_color, sigma_dist, true);
Mat new_flow; Mat new_flow;
resize(flow, new_flow, Size(new_cols, new_rows), 0, 0, INTER_NEAREST); resize(flow, new_flow, Size(new_cols, new_rows), 0, 0, INTER_NEAREST);
new_flow *= 2; new_flow *= 2;
@ -495,13 +496,7 @@ void calcOpticalFlowSF(Mat& from,
buildPyramidWithResizeMethod(from, pyr_from_images, layers - 1, INTER_CUBIC); buildPyramidWithResizeMethod(from, pyr_from_images, layers - 1, INTER_CUBIC);
buildPyramidWithResizeMethod(to, pyr_to_images, layers - 1, INTER_CUBIC); buildPyramidWithResizeMethod(to, pyr_to_images, layers - 1, INTER_CUBIC);
if ((int)pyr_from_images.size() != layers) { CV_Assert((int)pyr_from_images.size() == layers && (int)pyr_to_images.size() == layers);
exit(1);
}
if ((int)pyr_to_images.size() != layers) {
exit(1);
}
Mat first_from_image = pyr_from_images[layers - 1]; Mat first_from_image = pyr_from_images[layers - 1];
Mat first_to_image = pyr_to_images[layers - 1]; Mat first_to_image = pyr_to_images[layers - 1];
@ -635,5 +630,15 @@ void calcOpticalFlowSF(Mat& from,
mixChannels(&flow, 1, &resulted_flow, 1, from_to, 2); mixChannels(&flow, 1, &resulted_flow, 1, from_to, 2);
} }
CV_EXPORTS_W void calcOpticalFlowSF(Mat& from,
Mat& to,
Mat& flow,
int layers,
int averaging_block_size,
int max_flow) {
calcOpticalFlowSF(from, to, flow, layers, averaging_block_size, max_flow,
4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);
}
} }

View File

@ -91,7 +91,7 @@ static bool isFlowCorrect(float u) {
} }
static float calc_rmse(cv::Mat flow1, cv::Mat flow2) { static float calc_rmse(cv::Mat flow1, cv::Mat flow2) {
float sum; float sum = 0;
int counter = 0; int counter = 0;
const int rows = flow1.rows; const int rows = flow1.rows;
const int cols = flow1.cols; const int cols = flow1.cols;
@ -116,8 +116,6 @@ static float calc_rmse(cv::Mat flow1, cv::Mat flow2) {
} }
void CV_SimpleFlowTest::run(int) { void CV_SimpleFlowTest::run(int) {
int code = cvtest::TS::OK;
const float MAX_RMSE = 0.6; const float MAX_RMSE = 0.6;
const string frame1_path = ts->get_data_path() + "optflow/RubberWhale1.png"; const string frame1_path = ts->get_data_path() + "optflow/RubberWhale1.png";
const string frame2_path = ts->get_data_path() + "optflow/RubberWhale2.png"; const string frame2_path = ts->get_data_path() + "optflow/RubberWhale2.png";
@ -171,9 +169,7 @@ void CV_SimpleFlowTest::run(int) {
fclose(gt_flow_file); fclose(gt_flow_file);
cv::Mat flow; cv::Mat flow;
cv::calcOpticalFlowSF(frame1, frame2, cv::calcOpticalFlowSF(frame1, frame2, flow, 3, 2, 4);
flow,
3, 4, 2, 4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);
float rmse = calc_rmse(flow_gt, flow); float rmse = calc_rmse(flow_gt, flow);

View File

@ -135,7 +135,7 @@ static bool isFlowCorrect(float u) {
} }
static float calc_rmse(Mat flow1, Mat flow2) { static float calc_rmse(Mat flow1, Mat flow2) {
float sum; float sum = 0;
int counter = 0; int counter = 0;
const int rows = flow1.rows; const int rows = flow1.rows;
const int cols = flow1.cols; const int cols = flow1.cols;