#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>
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
{
ImageImportInfo info(argv[1]);
vigra_precondition(info.numBands() == 3, "an RGB image is required.");
MultiArray<2, TinyVector<float, 3> > imageArrayRGB(info.shape()),
imageArrayLab(info.shape());
MultiArray<2, float> gradMag(imageArrayLab.shape());
MultiArray<2, unsigned int> labelArray(gradMag.shape());
unsigned int max_label =
WatershedOptions().unionFind());
MultiArray<2, TinyVector<float, 3> > imageArrayBig(info.shape()*2-Shape2(1));
RGBValue<float>( 255, 0, 0 ), EdgeOverlayOnly);
typedef GridGraph<2, undirected_tag > ImageGraph;
ImageGraph imageGraph(labelArray.shape());
typedef AdjacencyListGraph RAG;
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,
ClusteringOptions().minRegionCount(numClusters)
.nodeFeatureImportance(beta)
.sizeImportance(wardness)
);
[&nodeLabels](unsigned int oldlabel)
{
return nodeLabels[oldlabel];
});
RGBValue<float>( 0, 255, 0), EdgeOverlayOnly);
}
catch (std::exception & e)
{
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}
void importImage(...)
Read an image from a file.
void exportImage(...)
Write an image to a file.
image import and export functions
void extractFeatures(...)
PowerSum< 0 > Count
Alias. Count.
Definition: accumulator-grammar.hxx:157
DivideByCount< Sum > Mean
Alias. Mean.
Definition: accumulator-grammar.hxx:173
@ L2Norm
Euclidean distance (L2 norm)
Definition: metrics.hxx:235
void regionImageToCrackEdgeImage(...)
Transform a labeled image into a crack edge (interpixel edge) image.
std::string impexListFormats()
List the image formats VIGRA can read and write.
void resizeMultiArraySplineInterpolation(...)
Resize MultiArray using B-spline interpolation.
void transformMultiArray(...)
Transform a multi-dimensional array with a unary function or functor.
Label watershedsMultiArray(...)
Watershed segmentation of an arbitrary-dimensional array.
@ DirectNeighborhood
use only direct neighbors
Definition: multi_fwd.hxx:187
void gaussianGradientMagnitude(...)
Calculate the gradient magnitude by means of a 1st derivatives of Gaussian filter.
void makeRegionAdjacencyGraph(GRAPH_IN graphIn, GRAPH_IN_NODE_LABEL_MAP labels, AdjacencyListGraph &rag, typename AdjacencyListGraph::template EdgeMap< std::vector< typename GRAPH_IN::Edge > > &affiliatedEdges, const Int64 ignoreLabel=-1)
make a region adjacency graph from a graph and labels w.r.t. that graph
Definition: graph_algorithms.hxx:165
void hierarchicalClustering(...)
Reduce the number of nodes in a graph by iteratively contracting the cheapest edge.