00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "AimBoxes.h"
00020
00021
00022 using std::ostringstream;
00023 using std::vector;
00024
00025 using namespace Marsyas;
00026
00027 AimBoxes::AimBoxes(mrs_string name):MarSystem("AimBoxes",name)
00028 {
00029 is_initialized = false;
00030 initialized_israte = 0.0;
00031 initialized_inobservations = 0;
00032 initialized_insamples = 0;
00033 initialized_box_size_spectral = 0;
00034 initialized_box_size_temporal = 0;
00035
00036 is_reset = false;
00037 addControls();
00038 }
00039
00040 AimBoxes::AimBoxes(const AimBoxes& a): MarSystem(a)
00041 {
00042 is_initialized = false;
00043 initialized_israte = 0.0;
00044 initialized_inobservations = 0;
00045 initialized_insamples = 0;
00046 initialized_box_size_spectral = 0;
00047 initialized_box_size_temporal = 0;
00048
00049 is_reset = false;
00050
00051 ctrl_box_size_spectral_= getctrl("mrs_natural/box_size_spectral");
00052 ctrl_box_size_temporal_= getctrl("mrs_natural/box_size_temporal");
00053 }
00054
00055 AimBoxes::~AimBoxes()
00056 {
00057 }
00058
00059 MarSystem*
00060 AimBoxes::clone() const
00061 {
00062 return new AimBoxes(*this);
00063 }
00064
00065 void
00066 AimBoxes::addControls()
00067 {
00068 addControl("mrs_natural/box_size_spectral", 16 , ctrl_box_size_spectral_);
00069 addControl("mrs_natural/box_size_temporal", 32 , ctrl_box_size_temporal_);
00070 }
00071
00072 void
00073 AimBoxes::myUpdate(MarControlPtr sender)
00074 {
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 if (initialized_israte != ctrl_israte_->to<mrs_real>() ||
00085 initialized_inobservations != ctrl_inObservations_->to<mrs_natural>() ||
00086 initialized_insamples != ctrl_inSamples_->to<mrs_natural>() ||
00087 initialized_box_size_spectral != ctrl_box_size_spectral_->to<mrs_natural>() ||
00088 initialized_box_size_temporal != ctrl_box_size_temporal_->to<mrs_natural>()) {
00089 is_initialized = false;
00090 }
00091
00092 if (!is_initialized) {
00093 InitializeInternal();
00094 is_initialized = true;
00095 initialized_israte = ctrl_israte_->to<mrs_real>();
00096 initialized_inobservations = ctrl_inObservations_->to<mrs_natural>();
00097 initialized_insamples = ctrl_inSamples_->to<mrs_natural>();
00098 initialized_box_size_spectral = ctrl_box_size_spectral_->to<mrs_natural>();
00099 initialized_box_size_temporal = ctrl_box_size_temporal_->to<mrs_natural>();
00100 }
00101
00102 (void) sender;
00103 MRSDIAG("AimBoxes.cpp - AimBoxes:myUpdate");
00104
00105
00106 ctrl_onSamples_->setValue(feature_size_, NOUPDATE);
00107 ctrl_onObservations_->setValue(box_count_, NOUPDATE);
00108 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00109 ctrl_onObsNames_->setValue("AimBoxes_" + ctrl_inObsNames_->to<mrs_string>() , NOUPDATE);
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 }
00125
00126 void
00127 AimBoxes::InitializeInternal() {
00128
00129 mrs_natural buffer_length = ctrl_inSamples_->to<mrs_natural>();
00130 mrs_natural channel_count = ctrl_inObservations_->to<mrs_natural>();
00131 mrs_natural channels_height = ctrl_box_size_spectral_->to<mrs_natural>();
00132 mrs_natural temporal_width = ctrl_box_size_temporal_->to<mrs_natural>();
00133
00134
00135
00136 if ((channels_height == 0) || (temporal_width == 0)) {
00137 return;
00138 }
00139
00140 box_limits_channels_.clear();
00141 box_limits_time_.clear();
00142
00143 while (channels_height < channel_count / 2) {
00144 int top = channel_count - 1;
00145 while (top - channels_height >= 0) {
00146 box_limits_channels_.push_back(std::make_pair(top,
00147 top - channels_height));
00148 top -= channels_height / 2;
00149 }
00150 channels_height *= 2;
00151 }
00152
00153 while (temporal_width < buffer_length) {
00154 box_limits_time_.push_back(temporal_width);
00155 temporal_width *= 2;
00156 }
00157
00158 box_count_ = box_limits_time_.size() * box_limits_channels_.size();
00159 feature_size_ = ctrl_box_size_spectral_->to<mrs_natural>() + ctrl_box_size_temporal_->to<mrs_natural>();
00160
00161 }
00162
00163 void
00164 AimBoxes::ResetInternal() {
00165 }
00166
00167 void
00168 AimBoxes::myProcess(realvec& in, realvec& out)
00169 {
00170 mrs_natural box_size_temporal = ctrl_box_size_temporal_->to<mrs_natural>();
00171 mrs_natural box_size_spectral = ctrl_box_size_spectral_->to<mrs_natural>();
00172
00173 int box_index = 0;
00174 for (int c = 0; c < static_cast<int>(box_limits_channels_.size()); ++c) {
00175 for (int s = 0; s < static_cast<int>(box_limits_time_.size()); ++s) {
00176 int pixel_size_channels = (box_limits_channels_[c].first
00177 - box_limits_channels_[c].second)
00178 / box_size_spectral;
00179 int pixel_size_samples = box_limits_time_[s] / box_size_temporal;
00180 vector<vector<float> > box;
00181 vector<float> line;
00182 line.resize(box_size_temporal, 0.0f);
00183 box.resize(box_size_spectral, line);
00184 for (int i = 0; i < box_size_spectral; ++i) {
00185 for (int j = 0; j < box_size_temporal; ++j) {
00186 float pixel_value = 0.0f;
00187 for (int k = i * pixel_size_channels; k < (i + 1) * pixel_size_channels; ++k) {
00188 for (int l = j * pixel_size_samples; l < (j + 1) * pixel_size_samples; ++l) {
00189 pixel_value += in(k + box_limits_channels_[c].second, l);
00190
00191 }
00192 }
00193 pixel_value /= (pixel_size_channels * pixel_size_samples);
00194 box[i][j] = pixel_value;
00195 }
00196 }
00197 int feature_index = 0;
00198 for (int i = 0; i < box_size_spectral; ++i) {
00199 float feature_value = 0.0f;
00200 for (int j = 0; j < box_size_temporal; ++j) {
00201 feature_value += box[i][j];
00202 }
00203 feature_value /= box_size_temporal;
00204
00205 out(box_index, feature_index) = feature_value;
00206
00207 ++feature_index;
00208 }
00209 for (int j = 0; j < box_size_temporal; ++j) {
00210 float feature_value = 0.0f;
00211 for (int i = 0; i < box_size_spectral; ++i) {
00212 feature_value += box[i][j];
00213 }
00214 feature_value /= box_size_spectral;
00215 out(box_index,feature_index) = feature_value;
00216
00217 ++feature_index;
00218 }
00219 ++box_index;
00220 }
00221 }
00222
00223 }