Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:freesurfer/freesurfer into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Greve committed Mar 26, 2019
2 parents 0e0de4d + 67ecf83 commit 01e71dc
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 87 deletions.
5 changes: 4 additions & 1 deletion freeview/FSSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@ bool FSSurface::LoadOverlay( const QString& filename, const QString& fn_reg,
if (mriheader->height == 1 && mriheader->depth == 1)
{
// likely wrong overlay data
cerr << "Number of vertices in overlay data does not match with surface.\n";
printf("Number of vertices in overlay data (%d or %d) does not match with surface (%d).\n",
mriheader->width*mriheader->height*mriheader->depth,
mriheader->width*mriheader->height*mriheader->depth*mriheader->nframes,
m_MRIS->nvertices);
return false;
}

Expand Down
1 change: 1 addition & 0 deletions freeview/GenericRenderView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <vtkAssemblyNode.h>
#include <vtkCellPicker.h>
#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include <QApplication>
#include <QClipboard>
#include <QImage>
Expand Down
11 changes: 8 additions & 3 deletions freeview/GeoSWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

GeoSWorker::GeoSWorker(QObject *parent) : QObject(parent)
{
m_geos = new GeodesicMatting;
m_nMaxDistance = 10;
connect(this, SIGNAL(ComputeTriggered()), SLOT(DoCompute()));
connect(this, SIGNAL(ApplyTriggered()), SLOT(DoApply()));
Expand All @@ -25,6 +26,7 @@ GeoSWorker::~GeoSWorker()
{
m_thread.quit();
m_thread.wait();
delete m_geos;
}

void GeoSWorker::Compute(LayerMRI *mri, LayerMRI* seg, LayerMRI* seeds, int max_distance)
Expand Down Expand Up @@ -124,7 +126,6 @@ void GeoSWorker::DoCompute()
voi2->Update();
seeds = voi->GetOutput();
vtkImageData* mri = voi2->GetOutput();
GeodesicMatting geos;
std::vector<unsigned char> label_list;
label_list.push_back(1);
label_list.push_back(2);
Expand All @@ -134,10 +135,9 @@ void GeoSWorker::DoCompute()
mri->GetDimensions(dim_new);
vol_size = dim_new[0]*dim_new[1]*dim_new[2];
unsigned char* seeds_out = new unsigned char[vol_size];
bool bSuccess = geos.Compute(dim_new, (double*)mri->GetScalarPointer(), mri_range, (unsigned char*)seeds->GetScalarPointer(), label_list, seeds_out);
bool bSuccess = m_geos->Compute(dim_new, (double*)mri->GetScalarPointer(), mri_range, (unsigned char*)seeds->GetScalarPointer(), label_list, seeds_out);
if (bSuccess)
{
// m_seg->SaveForUndo();
void* p = m_seg->GetImageData()->GetScalarPointer();
int nDataType = m_seg->GetImageData()->GetScalarType();
double fillValue = m_seg->GetFillValue();
Expand Down Expand Up @@ -204,3 +204,8 @@ void GeoSWorker::DoApply()
m_seg->SetModified();
emit ApplyFinished();
}

void GeoSWorker::Abort()
{
m_geos->Abort();
}
3 changes: 3 additions & 0 deletions freeview/GeoSWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QThread>

class LayerMRI;
class GeodesicMatting;

class GeoSWorker : public QObject
{
Expand All @@ -22,6 +23,7 @@ class GeoSWorker : public QObject
public slots:
void Compute(LayerMRI* mri, LayerMRI* seg, LayerMRI* seeds, int max_distance = -1);
void Apply(LayerMRI* seg, LayerMRI* filled);
void Abort();

private slots:
void DoCompute();
Expand All @@ -33,6 +35,7 @@ private slots:
LayerMRI* m_seg;
LayerMRI* m_filled;
int m_nMaxDistance;
GeodesicMatting* m_geos;

QThread m_thread;
};
Expand Down
22 changes: 6 additions & 16 deletions freeview/LayerMRI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3929,22 +3929,6 @@ VOXEL_LIST* LabelToVoxelList(MRI* mri, LABEL *area)

bool LayerMRI::GeodesicSegmentation(LayerMRI* seeds, double lambda, int wsize, double max_dist, LayerMRI *mask)
{
// vtkSmartPointer<vtkImageWeightedSum> sum = vtkSmartPointer<vtkImageWeightedSum>::New();
// sum->AddInput(interior->GetImageData());
// sum->AddInput(exterior->GetImageData());
// vtkSmartPointer<vtkImageCast> cast = vtkSmartPointer<vtkImageCast>::New();
// cast->SetInputConnection(sum->GetOutputPort());
// cast->SetOutputScalarTypeToUnsignedChar();
// cast->Update();
// vtkImageData* seeds = cast->GetOutput();
// // find the VOI in seeds
// int* dim = seeds->GetDimensions();
// int bound[6] = {0, dim[0], 0, dim[1], 0, dim[2]};
// unsigned char* ptr = (unsigned char*)seeds->GetScalarPointer();
// for (int i = 0; )
// vtkSmartPointer<vtkExtractVOI> voi = vtkSmartPointer<vtkExtractVOI>::New();
// voi->SetInput(seeds);
// voi->SetVOI(bound);
if (!m_geos)
{
m_geos = new GeoSWorker;
Expand All @@ -3955,6 +3939,12 @@ bool LayerMRI::GeodesicSegmentation(LayerMRI* seeds, double lambda, int wsize, d
return true;
}

void LayerMRI::GeodesicSegmentationAbort()
{
if (m_geos)
m_geos->Abort();
}

void LayerMRI::GeodesicSegmentationApply(LayerMRI *filled)
{
if (!m_geos)
Expand Down
2 changes: 2 additions & 0 deletions freeview/LayerMRI.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ class LayerMRI : public LayerVolumeBase

bool GeodesicSegmentation(LayerMRI* seeds, double lambda, int wsize, double max_dist, LayerMRI* mask);

void GeodesicSegmentationAbort();

void GeodesicSegmentationApply(LayerMRI* filled);

void GetVolumeInfo(int* dim, double* voxel_size);
Expand Down
5 changes: 4 additions & 1 deletion freeview/MyVTKUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
#include "vtkPointData.h"
#include "vtkFloatArray.h"
#include "vtkPassThrough.h"
#include "vtkDiscreteMarchingCubes.h"
#if VTK_MAJOR_VERSION > 5
#include "vtkFlyingEdges3D.h"
#endif

bool MyVTKUtils::VTKScreenCapture( vtkRenderWindow* renderWnd,
vtkRenderer* renderer,
Expand Down Expand Up @@ -491,7 +495,6 @@ bool MyVTKUtils::BuildContourActor( vtkImageData* data_in,
vtkSmartPointer<vtkContourFilter> contour = vtkSmartPointer<vtkContourFilter>::New();
contour->SetInputConnection( threshold->GetOutputPort());
contour->SetValue(0, dTh1);

{
vtkSmartPointer<vtkPolyDataConnectivityFilter> conn = vtkSmartPointer<vtkPolyDataConnectivityFilter>::New();
conn->SetInputConnection( contour->GetOutputPort() );
Expand Down
1 change: 1 addition & 0 deletions freeview/RenderView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "vtkScalarBarActor.h"
#include "vtkLookupTable.h"
#include "vtkRGBAColorTransferFunction.h"
#include "vtkRenderWindowInteractor.h"
#include <QPainter>
#include <QAction>
#include <vtkCellPicker.h>
Expand Down
10 changes: 10 additions & 0 deletions freeview/ToolWindowEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ ToolWindowEdit::ToolWindowEdit(QWidget *parent) :
connect(ui->colorPickerGeoOutside, SIGNAL(colorChanged(QColor)), SLOT(OnColorPickerGeoSeg(QColor)));
connect(ui->colorPickerGeoFill, SIGNAL(colorChanged(QColor)), SLOT(OnColorPickerGeoSeg(QColor)));
connect(ui->sliderGeoOpacity, SIGNAL(valueChanged(int)), SLOT(OnSliderGeoOpacity(int)));
connect(ui->pushButtonAbort, SIGNAL(clicked(bool)), SLOT(OnButtonGeoSegAbort()));

for (int i = 0; i < 3; i++)
{
Expand Down Expand Up @@ -582,15 +583,24 @@ void ToolWindowEdit::OnButtonGeoSegGo()
ui->pushButtonGeoGo->setEnabled(false);
ui->pushButtonGeoApply->setEnabled(false);
ui->widgetBusyIndicator->show();
ui->pushButtonAbort->setEnabled(true);
}
}
}

void ToolWindowEdit::OnButtonGeoSegAbort()
{
LayerMRI* mri_fill = qobject_cast<LayerMRI*>(MainWindow::GetMainWindow()->FindSupplementLayer("GEOS_FILL"));
if (mri_fill)
mri_fill->GeodesicSegmentationAbort();
}

void ToolWindowEdit::ResetGeoSegUI()
{
ui->pushButtonGeoApply->setEnabled(true);
ui->pushButtonGeoGo->setEnabled(true);
ui->widgetBusyIndicator->hide();
ui->pushButtonAbort->setEnabled(false);
}

void ToolWindowEdit::OnButtonGeoSegApply()
Expand Down
1 change: 1 addition & 0 deletions freeview/ToolWindowEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected slots:
void OnColorPickerGeoSeg(const QColor& color);
void OnSliderGeoOpacity(int nVal);
void ResetGeoSegUI();
void OnButtonGeoSegAbort();

protected:
virtual void showEvent(QShowEvent *);
Expand Down
10 changes: 10 additions & 0 deletions freeview/ToolWindowEdit.ui
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButtonAbort">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Abort</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonGeoGo">
<property name="text">
Expand Down
52 changes: 27 additions & 25 deletions freeview/geos/GeodesicMatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
#include "kde.h"
#include <QtDebug>
#include <QElapsedTimer>
#include <QMutexLocker>

#define LARGENUMBER 1e12

using namespace std;

GeodesicMatting::GeodesicMatting()
{
m_bAbort = false;
}

void GeodesicMatting::Abort()
{
QMutexLocker locker(&m_mutex);
m_bAbort = true;
}

double GeodesicMatting::Interpolate(const std::vector<double> &v, const std::vector<double> &hf, double val)
Expand Down Expand Up @@ -209,6 +216,7 @@ template <typename T> vector<size_t> sort_indexes(const vector<T> &v) {
bool GeodesicMatting::Compute(int *dim, double *mri_in, double* mri_range_in, unsigned char *seeds_in, std::vector<unsigned char>& label_list,
unsigned char *seeds_out)
{
m_bAbort = false;
double** lHood = new double*[label_list.size()];
double** DISTS = new double*[label_list.size()];
size_t vol_size = ((size_t)dim[0])*dim[1]*dim[2];
Expand Down Expand Up @@ -305,17 +313,17 @@ bool GeodesicMatting::Compute(int *dim, double *mri_in, double* mri_range_in, un
// fast marching
bool ready = false;
int npix = 0;
while (!ready)
while (!ready && !m_bAbort)
{
npix++;
if (npix%10000 == 0)
qDebug() << npix;

double mini = LARGENUMBER;
int idx = -1;
long long idx = -1;
for (size_t i = 0; i < vol_size; i++)
{
if (TRIALVALS[i]<mini)
if (TRIALVALS[i] < mini)
{
mini=TRIALVALS[i];
idx=i;
Expand Down Expand Up @@ -458,23 +466,26 @@ bool GeodesicMatting::Compute(int *dim, double *mri_in, double* mri_range_in, un
}
memcpy(DISTS[l], D, sizeof(double)*vol_size);
}
qDebug() << timer.elapsed()/1000.;

memset(seeds_out, 0, vol_size);
for (size_t i = 0; i < vol_size; i++)
if (!m_bAbort)
{
double dmin = 1e10;
int n = 0;
for (size_t l = 0; l < label_list.size(); l++)
qDebug() << timer.elapsed()/1000.;
memset(seeds_out, 0, vol_size);
for (size_t i = 0; i < vol_size; i++)
{
if (DISTS[l][i] < dmin)
double dmin = 1e10;
int n = 0;
for (size_t l = 0; l < label_list.size(); l++)
{
dmin = DISTS[l][i];
n = l;
if (DISTS[l][i] < dmin)
{
dmin = DISTS[l][i];
n = l;
}
}
if (n != label_list.size()-1)
seeds_out[i] = label_list[n];
}
if (n != label_list.size()-1)
seeds_out[i] = label_list[n];
}
delete[] D;
delete[] TRIALVALS;
Expand All @@ -489,7 +500,7 @@ bool GeodesicMatting::Compute(int *dim, double *mri_in, double* mri_range_in, un
delete[] lHood;
delete[] DISTS;

return true;
return (!m_bAbort);
}

bool GeodesicMatting::ComputeWithSorting(int *dim, double *mri_in, double* mri_range_in, unsigned char *seeds_in, std::vector<unsigned char>& label_list,
Expand Down Expand Up @@ -604,19 +615,10 @@ bool GeodesicMatting::ComputeWithSorting(int *dim, double *mri_in, double* mri_r
double mini = LARGENUMBER;
long long idx = -1;
int n_idxs = -1;
// for (size_t i = 0; i < idxs.size(); i++)
// {
// if (TRIALVALS[idxs[i]] < mini)
// {
// mini = TRIALVALS[idxs[i]];
// idx = idxs[i];
// n_idxs = i;
// }
// }

for (size_t i = 0; i < vol_size; i++)
{
if (TRIALVALS[i]<mini)
if (TRIALVALS[i] < mini)
{
mini=TRIALVALS[i];
idx=i;
Expand Down
7 changes: 7 additions & 0 deletions freeview/geos/GeodesicMatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <math.h>
#include <vector>

#include <QMutex>

class GeodesicMatting
{
public:
Expand All @@ -14,12 +16,17 @@ class GeodesicMatting
bool ComputeWithSorting(int* dim, double* mri_in, double* mri_range_in,
unsigned char* seeds_in, std::vector<unsigned char>& label_list, unsigned char* seeds_out);

void Abort();

private:
double Interpolate(const std::vector<double>& v, const std::vector<double>& hf, double val);
void Dilate(int* dim, unsigned char* in, unsigned char* out);
double ComputeNeighDist(double** lHood, int nLabels, unsigned char* KNOWN, double* D, int* dim, int it, int jt, int kt);
int GetMinValIndex(const std::vector<double>& vals);
int GetMinValIndexInSorted(const std::vector<double>& vals, const std::vector<std::size_t>& sorted_idx);

bool m_bAbort;
QMutex m_mutex;
};

#endif // GEODESICMATTING_H
Loading

0 comments on commit 01e71dc

Please sign in to comment.