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

Auto filter optimization #240

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions autoscoper/src/ui/AutoscoperMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,18 @@ void AutoscoperMainWindow::on_toolButtonTrackCurrent_clicked() {
tracking_dialog->retrack();
}*/

void AutoscoperMainWindow::on_toolButtonFilterTuning_clicked() {
// Probably should prompt the user to save the current filters if they want
if (tracker) {
this->filters_widget->clearFilters();
this->filters_widget->setupFilterTuning();
redrawGL();
// Clear the filters
this->filters_widget->updateFiltersAfterTuning(this->tracker->optimizeFilters());
// Once completed update the filters in the UI
}
}

void AutoscoperMainWindow::on_actionExport_NCC_as_csv_triggered(bool checked) {
QString filename = get_filename(true, "*.ncc");
if (filename.compare("") != 0) {
Expand Down
2 changes: 2 additions & 0 deletions autoscoper/src/ui/AutoscoperMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ class AutoscoperMainWindow : public QMainWindow{
void on_toolButtonTrackCurrent_clicked();
//void on_toolButtonRetrack_clicked();

void on_toolButtonFilterTuning_clicked();


//Shortcuts
void key_w_pressed();
Expand Down
12 changes: 12 additions & 0 deletions autoscoper/src/ui/FilterDockWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ void FilterDockWidget::loadFilterSettings(int camera, QString filename)
void FilterDockWidget::addCamera(View * view){
dock->treeWidget->addCamera(view);
}

void FilterDockWidget::clearFilters() {
dock->treeWidget->clearFilters();
}

void FilterDockWidget::setupFilterTuning() {
dock->treeWidget->setupFilterTuning();
}

void FilterDockWidget::updateFiltersAfterTuning(std::vector<float> & filter_params) {
dock->treeWidget->updateFiltersAfterTuning(filter_params);
}
4 changes: 4 additions & 0 deletions autoscoper/src/ui/FilterDockWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class FilterDockWidget : public QDockWidget{
void loadAllSettings(QString directory);
void loadFilterSettings(int camera, QString filename);

void clearFilters();
void setupFilterTuning();
void updateFiltersAfterTuning(std::vector<float> & filter_params);

AutoscoperMainWindow * getMainWindow(){return mainwindow;};

private:
Expand Down
70 changes: 70 additions & 0 deletions autoscoper/src/ui/FilterTreeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "ui/FilterTreeWidgetItem.h"
#include "ui/FilterDockWidget.h"
#include "ui/AutoscoperMainWindow.h"
#include "ui/FilterTreeWidgetParameter.h"

#include "View.hpp"
#include <math.h>
Expand Down Expand Up @@ -308,6 +309,75 @@ void FilterTreeWidget::loadFilterSettings(int camera, QString filename)
}
}

void FilterTreeWidget::clearFilters() {
for (int i = 0; i < this->topLevelItemCount(); ++i) {
CameraTreeWidgetItem* camera = dynamic_cast<CameraTreeWidgetItem*> (topLevelItem(i));
if (camera) {
for (int j = 0; j < camera->childCount(); ++j) {
ModelViewTreeWidgetItem* model = dynamic_cast<ModelViewTreeWidgetItem*> (camera->child(j));
if (model) {
model->clearFilters();
}
}
}
}
}

void FilterTreeWidget::setupFilterTuning() {
// Right now we want to add a sobel filter to each drr renderer and to each rad renderer
for (int i = 0; i < this->topLevelItemCount(); ++i) {
CameraTreeWidgetItem* camera = dynamic_cast<CameraTreeWidgetItem*> (topLevelItem(i));
if (camera) {
for (int j = 0; j < camera->childCount(); ++j) {
ModelViewTreeWidgetItem* model = dynamic_cast<ModelViewTreeWidgetItem*> (camera->child(j));
if (model) {
if (model->getType() == 1) { // DRR
FilterTreeWidgetItem* filter = new FilterTreeWidgetItem(0); // Sobel
filter->addToModelViewTreeWidgetItem(this, model);
}
else if (model->getType() == 0) { // RAD
FilterTreeWidgetItem* filter = new FilterTreeWidgetItem(0); // Sobel
filter->addToModelViewTreeWidgetItem(this, model);
}
}
}
}
}
}

void FilterTreeWidget::updateFiltersAfterTuning(std::vector<float>& filter_params) {
//Vector is organized as [cam0_rad_scale, cam0_rad_blend, cam0_drr_scale, cam0_drr_blend, cam1_rad_scale, cam1_rad_blend, cam1_drr_scale, cam1_drr_blend]
//Right now we want to add a sobel filter to each drr renderer and to each rad renderer
for (int i = 0; i < this->topLevelItemCount(); ++i) {
CameraTreeWidgetItem* camera = dynamic_cast<CameraTreeWidgetItem*> (topLevelItem(i));
if (camera) {
for (int j = 0; j < camera->childCount(); ++j) {
ModelViewTreeWidgetItem* model = dynamic_cast<ModelViewTreeWidgetItem*> (camera->child(j));
if (model) {
if (model->getType() == 1) { // DRR
FilterTreeWidgetItem* filter = dynamic_cast<FilterTreeWidgetItem*> (model->child(0));
if (filter) {
std::vector<FilterTreeWidgetParameter*>* fp = filter->getParameters();
fp->at(1)->value = filter_params.at(4 * i + 2 * j + 1);
fp->at(0)->value = filter_params.at(4 * i + 2 * j);
filter->updateFilter();
}
}
else if (model->getType() == 0) { // RAD
FilterTreeWidgetItem* filter = dynamic_cast<FilterTreeWidgetItem*> (model->child(0));
if (filter) {
std::vector<FilterTreeWidgetParameter*>* fp = filter->getParameters();
fp->at(1)->value = filter_params.at(4 * i + 2 * j + 1);
fp->at(0)->value = filter_params.at(4 * i + 2 * j);
filter->updateFilter();
}
}
}
}
}
}
}

void FilterTreeWidget::toggle_drrs(){
for(int i=0;i<this->topLevelItemCount(); ++i){
CameraTreeWidgetItem * camera = dynamic_cast<CameraTreeWidgetItem*> (topLevelItem(i));
Expand Down
4 changes: 4 additions & 0 deletions autoscoper/src/ui/FilterTreeWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class FilterTreeWidget : public QTreeWidget{
void saveAllSettings(QString directory);
void loadAllSettings(QString directory);
void loadFilterSettings(int camera, QString filename);

void clearFilters();
void setupFilterTuning();
void updateFiltersAfterTuning(std::vector<float>& filter_params);
private:
void printTree();
void resetFilterTree();
Expand Down
8 changes: 8 additions & 0 deletions autoscoper/src/ui/ModelViewTreeWidgetItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ void ModelViewTreeWidgetItem::removeFilter(FilterTreeWidgetItem* filterItem, boo
if(removeFromTree)this->removeChild(filterItem);
}

void ModelViewTreeWidgetItem::clearFilters() {
for (auto filterItem : filterTreeWidgets) {
this->removeChild(filterItem);
}
filterTreeWidgets.clear();
m_filters->clear();
}

void ModelViewTreeWidgetItem::printFilters(){
for(int i = 0; i < filterTreeWidgets.size(); i++){
fprintf(stderr," Filter%d %s\n",i,filterTreeWidgets[i]->getName().toStdString().c_str());
Expand Down
1 change: 1 addition & 0 deletions autoscoper/src/ui/ModelViewTreeWidgetItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ModelViewTreeWidgetItem : public QObject ,public QTreeWidgetItem
void loadSettings(std::ifstream & file);
void loadFilters(std::ifstream & file);
void toggleVisible();
void clearFilters();

private:
void init();
Expand Down
28 changes: 26 additions & 2 deletions autoscoper/src/ui/ui-files/AutoscoperMainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<width>987</width>
<height>600</height>
</rect>
</property>
Expand Down Expand Up @@ -384,6 +384,30 @@ Dialog</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolButtonFilterTuning">
<property name="minimumSize">
<size>
<width>85</width>
<height>60</height>
</size>
</property>
<property name="text">
<string>Auto Filter
Tuning</string>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand All @@ -410,7 +434,7 @@ Dialog</string>
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<width>987</width>
<height>25</height>
</rect>
</property>
Expand Down
5 changes: 5 additions & 0 deletions libautoscoper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ set(libautoscoper_HEADERS
src/CoordFrame.hpp
src/DownhillSimplex.hpp
src/Filter.hpp
src/FilterParticle.hpp
src/KeyCurve.hpp
src/Particle.hpp
src/PositionParticle.hpp
src/PSO.hpp
src/TiffImage.h
src/Tracker.hpp
Expand All @@ -21,6 +24,8 @@ set(libautoscoper_SOURCES
src/Camera.cpp
src/CoordFrame.cpp
src/DownhillSimplex.cpp
src/FilterParticle.cpp
src/PositionParticle.cpp
src/PSO.cpp
src/KeyCurve.cpp
src/TiffImage.cpp
Expand Down
71 changes: 71 additions & 0 deletions libautoscoper/src/FilterParticle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "FilterParticle.hpp"

// Default constructor
FilterParticle::FilterParticle() : Particle(8) {
this->Filter_Settings = std::vector<float>(this->NUM_OF_DIMENSIONS, 0.f);
this->Velocity = std::vector<float>(this->NUM_OF_DIMENSIONS, 0.f);
}

// Copy constructor
FilterParticle::FilterParticle(const FilterParticle& p) : Particle(8) {
this->Filter_Settings = p.Filter_Settings;
this->Velocity = p.Velocity;
this->NCC = p.NCC;
}

// Filter settings constructor
FilterParticle::FilterParticle(const std::vector<float>& filter_settings) : Particle(8) {
this->Filter_Settings = filter_settings;
this->Velocity = std::vector<float>(this->NUM_OF_DIMENSIONS, 0.f);
}

// Constructor with rand range
FilterParticle::FilterParticle(float start_range_min, float start_range_max) : Particle(8) {
this->Filter_Settings = std::vector<float>(this->NUM_OF_DIMENSIONS, 0.f);
this->Velocity = std::vector<float>(this->NUM_OF_DIMENSIONS, 0.f);
for (int i = 0; i < this->NUM_OF_DIMENSIONS; i++) {
this->Filter_Settings[i] = getRandom(start_range_min, start_range_max);
}
}

void FilterParticle::updateParticle(const Particle& pBest, const Particle& gBest, float omega) {
// Check that the particles are the same type
const FilterParticle* p = dynamic_cast<const FilterParticle*>(&pBest);
const FilterParticle* g = dynamic_cast<const FilterParticle*>(&gBest);
if (p == nullptr || g == nullptr) {
std::cout << "Error: Either pBest or gBest is not a FilterParticle" << std::endl;
return;
}
for (int dim = 0; dim < this->NUM_OF_DIMENSIONS; dim++) {
float rp = getRandomClamped();
float rg = getRandomClamped();

this->Velocity[dim] =
omega * this->Velocity[dim]
+ c1 * rp * (p->Filter_Settings[dim] - this->Filter_Settings[dim])
+ c2 * rg * (g->Filter_Settings[dim] - this->Filter_Settings[dim]);

this->Filter_Settings[dim] += this->Velocity[dim];
}
}

// Assignment operator
Particle& FilterParticle::operator=(const Particle& p) {
const FilterParticle* fp = dynamic_cast<const FilterParticle*>(&p);
if (fp == nullptr) {
std::cerr << "ERROR: p is not a FilterParticle" << std::endl;
return *this;
}
this->Filter_Settings = fp->Filter_Settings;
this->Velocity = fp->Velocity;
this->NCC = fp->NCC;
return *this;
}

// Output operator
std::ostream& operator<<(std::ostream& os, const FilterParticle& p) {
os << "Filter Settings: " << p.Filter_Settings << std::endl;
os << "Velocity: " << p.Velocity << std::endl;
os << "NCC: " << p.NCC;
return os;
}
20 changes: 20 additions & 0 deletions libautoscoper/src/FilterParticle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "Particle.hpp"
#include "View.hpp"

struct FilterParticle : public Particle {
std::vector<float> Filter_Settings; // Organized as [cam0_rad_scale, cam0_rad_blend, cam0_drr_scale, cam0_drr_blend, cam1_rad_scale, cam1_rad_blend, cam1_drr_scale, cam1_drr_blend]

// Default constructor
FilterParticle();
// Copy constructor
FilterParticle(const FilterParticle& p);
// Filter settings constructor
FilterParticle(const std::vector<float>& filter_settings);
// Random initialization constructor
FilterParticle(float start_range_min, float start_range_max);
// Assignment operator
Particle& operator=(const Particle& p);
void updateParticle(const Particle& pBest, const Particle& gBest, float omega);
};

extern std::ostream& operator<<(std::ostream& os, const FilterParticle& p);
Loading