From 5973d002f7fe9b79060c27980ecb77693f6994e2 Mon Sep 17 00:00:00 2001 From: Michael Kolb Date: Tue, 29 Jan 2013 10:33:22 -0800 Subject: Move Camera Java/Native source into Gallery2 Change-Id: I968efe4d656e88a7760d3c0044f65b4adac2ddd1 --- .../feature_mos/src/mosaic/AlignFeatures.cpp | 231 +++++++++++++++++++++ jni_mosaic/feature_mos/src/mosaic/AlignFeatures.h | 93 +++++++++ 2 files changed, 324 insertions(+) create mode 100644 jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp create mode 100644 jni_mosaic/feature_mos/src/mosaic/AlignFeatures.h (limited to 'jni_mosaic') diff --git a/jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp b/jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp new file mode 100644 index 000000000..aeabf8f97 --- /dev/null +++ b/jni_mosaic/feature_mos/src/mosaic/AlignFeatures.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/////////////////////////////////////////////////// +// AlignFeatures.cpp +// S.O. # : +// Author(s): zkira, mbansal, bsouthall, narodits +// $Id: AlignFeatures.cpp,v 1.20 2011/06/17 13:35:47 mbansal Exp $ + +#include +#include + +#include "trsMatrix.h" +#include "MatrixUtils.h" +#include "AlignFeatures.h" +#include "Log.h" + +#define LOG_TAG "AlignFeatures" + +Align::Align() +{ + width = height = 0; + frame_number = 0; + num_frames_captured = 0; + reference_frame_index = 0; + db_Identity3x3(Hcurr); + db_Identity3x3(Hprev); +} + +Align::~Align() +{ + // Free gray-scale image + if (imageGray != ImageUtils::IMAGE_TYPE_NOIMAGE) + ImageUtils::freeImage(imageGray); +} + +char* Align::getRegProfileString() +{ + return reg.profile_string; +} + +int Align::initialize(int width, int height, bool _quarter_res, float _thresh_still) +{ + int nr_corners = DEFAULT_NR_CORNERS; + double max_disparity = DEFAULT_MAX_DISPARITY; + int motion_model_type = DEFAULT_MOTION_MODEL; + int nrsamples = DB_DEFAULT_NR_SAMPLES; + double scale = DB_POINT_STANDARDDEV; + int chunk_size = DB_DEFAULT_CHUNK_SIZE; + int nrhorz = width/48; // Empirically determined number of horizontal + int nrvert = height/60; // and vertical buckets for harris corner detection. + bool linear_polish = false; + unsigned int reference_update_period = DEFAULT_REFERENCE_UPDATE_PERIOD; + + const bool DEFAULT_USE_SMALLER_MATCHING_WINDOW = false; + bool use_smaller_matching_window = DEFAULT_USE_SMALLER_MATCHING_WINDOW; + + quarter_res = _quarter_res; + thresh_still = _thresh_still; + + frame_number = 0; + num_frames_captured = 0; + reference_frame_index = 0; + db_Identity3x3(Hcurr); + db_Identity3x3(Hprev); + + if (!reg.Initialized()) + { + reg.Init(width, height, motion_model_type, 20, linear_polish, quarter_res, + scale, reference_update_period, false, 0, nrsamples, chunk_size, + nr_corners, max_disparity, use_smaller_matching_window, + nrhorz, nrvert); + } + this->width = width; + this->height = height; + + imageGray = ImageUtils::allocateImage(width, height, 1); + + if (reg.Initialized()) + return ALIGN_RET_OK; + else + return ALIGN_RET_ERROR; +} + +int Align::addFrameRGB(ImageType imageRGB) +{ + ImageUtils::rgb2gray(imageGray, imageRGB, width, height); + return addFrame(imageGray); +} + +int Align::addFrame(ImageType imageGray_) +{ + int ret_code = ALIGN_RET_OK; + + // Obtain a vector of pointers to rows in image and pass in to dbreg + ImageType *m_rows = ImageUtils::imageTypeToRowPointers(imageGray_, width, height); + + if (frame_number == 0) + { + reg.AddFrame(m_rows, Hcurr, true); // Force this to be a reference frame + int num_corner_ref = reg.GetNrRefCorners(); + + if (num_corner_ref < MIN_NR_REF_CORNERS) + { + return ALIGN_RET_LOW_TEXTURE; + } + } + else + { + reg.AddFrame(m_rows, Hcurr, false); + } + + // Average translation per frame = + // [Translation from Frame0 to Frame(n-1)] / [(n-1)] + average_tx_per_frame = (num_frames_captured < 2) ? 0.0 : + Hprev[2] / (num_frames_captured - 1); + + // Increment the captured frame counter if we already have a reference frame + num_frames_captured++; + + if (frame_number != 0) + { + int num_inliers = reg.GetNrInliers(); + + if(num_inliers < MIN_NR_INLIERS) + { + ret_code = ALIGN_RET_FEW_INLIERS; + + Hcurr[0] = 1.0; + Hcurr[1] = 0.0; + // Set this as the average per frame translation taking into acccount + // the separation of the current frame from the reference frame... + Hcurr[2] = -average_tx_per_frame * + (num_frames_captured - reference_frame_index); + Hcurr[3] = 0.0; + Hcurr[4] = 1.0; + Hcurr[5] = 0.0; + Hcurr[6] = 0.0; + Hcurr[7] = 0.0; + Hcurr[8] = 1.0; + } + + if(fabs(Hcurr[2]) + +#include "ImageUtils.h" +#include "MatrixUtils.h" + +class Align { + +public: + // Types of alignment possible + static const int ALIGN_TYPE_PAN = 1; + + // Return codes + static const int ALIGN_RET_LOW_TEXTURE = -2; + static const int ALIGN_RET_ERROR = -1; + static const int ALIGN_RET_OK = 0; + static const int ALIGN_RET_FEW_INLIERS = 1; + + ///// Settings for feature-based alignment + // Number of features to use from corner detection + static const int DEFAULT_NR_CORNERS=750; + static const double DEFAULT_MAX_DISPARITY=0.1;//0.4; + // Type of homography to model + static const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_R_T; +// static const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_PROJECTIVE; +// static const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_AFFINE; + static const unsigned int DEFAULT_REFERENCE_UPDATE_PERIOD=1500; // Manual reference frame update so set this to a large number + + static const int MIN_NR_REF_CORNERS = 25; + static const int MIN_NR_INLIERS = 10; + + Align(); + ~Align(); + + // Initialization of structures, etc. + int initialize(int width, int height, bool quarter_res, float thresh_still); + + // Add a frame. Note: The alignment computation is performed + // in this function + int addFrameRGB(ImageType image); + int addFrame(ImageType image); + + // Obtain the TRS matrix from the last two frames + int getLastTRS(double trs[3][3]); + char* getRegProfileString(); + +protected: + + db_FrameToReferenceRegistration reg; + + int frame_number; + + double Hcurr[9]; // Homography from the alignment reference to the frame-t + double Hprev[9]; // Homography from frame-0 to the frame-(t-1) + + int reference_frame_index; // Index of the reference frame from all captured frames + int num_frames_captured; // Total number of frames captured (different from frame_number) + double average_tx_per_frame; // Average pixel translation per captured frame + + int width,height; + + bool quarter_res; // Whether to process at quarter resolution + float thresh_still; // Translation threshold in pixels to detect still camera + ImageType imageGray; +}; + + +#endif -- cgit v1.2.3