graph_agglomerative_clustering.cxx
|
|
#include <iostream>
#include <vigra/multi_array.hxx>
#include <vigra/multi_resize.hxx>
#include <vigra/colorconversions.hxx>
#include <vigra/multi_convolution.hxx>
#include <vigra/multi_watersheds.hxx>
#include <vigra/multi_gridgraph.hxx>
#include <vigra/accumulator.hxx>
#include <vigra/adjacency_list_graph.hxx>
#include <vigra/graph_algorithms.hxx>
#include <vigra/hierarchical_clustering.hxx>
#include <vigra/metrics.hxx>
using namespace vigra;
int main (int argc, char ** argv)
{
float sigmaGradMag = 3.0f;
float beta = 0.5f;
float wardness = 0.8f;
int numClusters = 30;
if(argc != 3)
{
std::cout << "Usage: " << argv[0] << " infile outfile" << std::endl;
std::cout << "(only color images)" << std::endl;
return 1;
}
try
{
vigra_precondition(info.
numBands() == 3,
"an RGB image is required.");
imageArrayLab(info.
shape());
unsigned int max_label =
ImageGraph imageGraph(labelArray.shape());
RAG rag;
RAG::EdgeMap<std::vector<ImageGraph::Edge>> affiliatedEdges(rag);
RAG::EdgeMap<float> edgeWeights(rag),
edgeLengths(rag);
for(RAG::EdgeIt rag_edge(rag); rag_edge != lemon::INVALID; ++rag_edge)
{
for(unsigned int k = 0; k < affiliatedEdges[*rag_edge].size(); ++k)
{
auto const & grid_edge = affiliatedEdges[*rag_edge][k];
auto start = imageGraph.u(grid_edge),
end = imageGraph.v(grid_edge);
double grid_edge_gradient = 0.5 * (gradMag[start] + gradMag[end]);
edgeWeights[*rag_edge] += grid_edge_gradient;
}
edgeLengths[*rag_edge] = affiliatedEdges[*rag_edge].size();
edgeWeights[*rag_edge] /= edgeLengths[*rag_edge];
}
using namespace acc;
AccumulatorChainArray<CoupledArrays<2, TinyVector<float, 3>, unsigned int>,
Select<DataArg<1>, LabelArg<2>,
features;
RAG::NodeMap<TinyVector<float, 3>> meanColor(rag);
RAG::NodeMap<unsigned int> regionSize(rag);
for(unsigned int k=0; k<=max_label; ++k)
{
meanColor[k] = get<Mean>(features, k);
regionSize[k] = get<Count>(features, k);
}
RAG::NodeMap<unsigned int> nodeLabels(rag);
edgeWeights, edgeLengths, meanColor, regionSize,
nodeLabels,
.nodeFeatureImportance(beta)
.sizeImportance(wardness)
);
[&nodeLabels](unsigned int oldlabel)
{
return nodeLabels[oldlabel];
});
}
catch (std::exception & e)
{
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}