Home | History | Annotate | Download | only in libdvs
      1 /*
      2  * libdvs.cpp - abstract for DVS (Digital Video Stabilizer)
      3  *
      4  *    Copyright (c) 2014-2016 Intel Corporation
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *        http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  *
     18  * Author: Zong Wei <wei.zong (at) intel.com>
     19  */
     20 
     21 #include <opencv2/core.hpp>
     22 #include <opencv2/core/ocl.hpp>
     23 #include <opencv2/core/utility.hpp>
     24 
     25 #include "libdvs.h"
     26 #include "stabilizer.h"
     27 
     28 struct DigitalVideoStabilizer : DvsInterface
     29 {
     30     virtual ~DigitalVideoStabilizer() {}
     31 
     32     int init(int width, int height, bool twoPass);
     33 
     34     void setConfig(DvsConfig* config);
     35 
     36     void release();
     37 
     38     void nextStabilizedMotion(DvsData* frame, DvsResult* result);
     39 
     40 
     41     VideoStabilizer* _videoStab;
     42 
     43     DigitalVideoStabilizer () {
     44         _videoStab = NULL;
     45     }
     46 };
     47 
     48 int DigitalVideoStabilizer::init(int width, int height, bool twoPass)
     49 {
     50     cv::Size frameSize;
     51     frameSize.width = width;
     52     frameSize.height = height;
     53 
     54     if (_videoStab != NULL) {
     55         delete _videoStab;
     56         _videoStab = NULL;
     57     }
     58     _videoStab = new VideoStabilizer(twoPass, false, false, false);
     59     if (_videoStab == NULL) {
     60         return -1;
     61     }
     62 
     63     // stabilizer configuration
     64     _videoStab->setFrameSize(frameSize);
     65     _videoStab->configFeatureDetector(1000, 15);
     66 
     67     return 0;
     68 }
     69 
     70 void DigitalVideoStabilizer::setConfig(DvsConfig* config)
     71 {
     72     if (NULL == _videoStab) {
     73         return;
     74     }
     75     // stabilizer configuration
     76     _videoStab->setFrameSize(cv::Size(config->frame_width, config->frame_height));
     77     _videoStab->configMotionFilter(config->radius, config->stdev);
     78     _videoStab->configFeatureDetector(config->features, config->minDistance);
     79 }
     80 
     81 void DigitalVideoStabilizer::release()
     82 {
     83     if (_videoStab != NULL) {
     84         delete _videoStab;
     85     }
     86 }
     87 
     88 void DigitalVideoStabilizer::nextStabilizedMotion(DvsData* frame, DvsResult* result)
     89 {
     90     if ((NULL == _videoStab) || (NULL == result)) {
     91         return;
     92     }
     93     result->frame_id = -1;
     94     result->frame_width = _videoStab->getFrameSize().width;
     95     result->frame_height = _videoStab->getFrameSize().height;
     96 
     97     cv::Mat HMatrix = _videoStab->nextStabilizedMotion(frame, result->frame_id);
     98 
     99     if (HMatrix.empty()) {
    100         result->valid = false;
    101         result->proj_mat[0][0] = 1.0f;
    102         result->proj_mat[0][1] = 0.0f;
    103         result->proj_mat[0][2] = 0.0f;
    104         result->proj_mat[1][0] = 0.0f;
    105         result->proj_mat[1][1] = 1.0f;
    106         result->proj_mat[1][2] = 0.0f;
    107         result->proj_mat[2][0] = 0.0f;
    108         result->proj_mat[2][1] = 0.0f;
    109         result->proj_mat[2][2] = 1.0f;
    110         return;
    111     }
    112 
    113     cv::Mat invHMat = HMatrix.inv();
    114     result->valid = true;
    115 
    116     for( int i = 0; i < 3; i++ ) {
    117         for( int j = 0; j < 3; j++ ) {
    118             result->proj_mat[i][j] = invHMat.at<float>(i, j);
    119         }
    120     }
    121 #if 0
    122     printf ("proj_mat(%d, :)={%f, %f, %f, %f, %f, %f, %f, %f, %f}; \n", result->frame_id + 1,
    123             result->proj_mat[0][0], result->proj_mat[0][1], result->proj_mat[0][2],
    124             result->proj_mat[1][0], result->proj_mat[1][1], result->proj_mat[1][2],
    125             result->proj_mat[2][0], result->proj_mat[2][1], result->proj_mat[2][2]);
    126 
    127     printf ("amplitude(%d, :)={%f, %f}; \n", result->frame_id + 1,
    128             result->proj_mat[0][2], result->proj_mat[1][2]);
    129 #endif
    130 }
    131 
    132 DvsInterface* getDigitalVideoStabilizer(void)
    133 {
    134     return new DigitalVideoStabilizer();
    135 }
    136 
    137 
    138 
    139