vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
gsm.cc
Go to the documentation of this file.
1/************************************************************************/
2/* */
3/* vspline - a set of generic tools for creation and evaluation */
4/* of uniform b-splines */
5/* */
6/* Copyright 2015 - 2023 by Kay F. Jahnke */
7/* */
8/* Permission is hereby granted, free of charge, to any person */
9/* obtaining a copy of this software and associated documentation */
10/* files (the "Software"), to deal in the Software without */
11/* restriction, including without limitation the rights to use, */
12/* copy, modify, merge, publish, distribute, sublicense, and/or */
13/* sell copies of the Software, and to permit persons to whom the */
14/* Software is furnished to do so, subject to the following */
15/* conditions: */
16/* */
17/* The above copyright notice and this permission notice shall be */
18/* included in all copies or substantial portions of the */
19/* Software. */
20/* */
21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
23/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
24/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
25/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
26/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
27/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
28/* OTHER DEALINGS IN THE SOFTWARE. */
29/* */
30/************************************************************************/
31
32/// \file gsm.cc
33///
34/// \brief calculating the gradient squared magnitude, derivatives
35///
36/// implementation of gsm.cc, performing the calculation of the
37/// gradient squared magnitude in a loop using two evaluators for
38/// the two derivatives, adding the squared magnitudes and writing
39/// the result to an image file
40///
41/// compile with:
42/// clang++ -std=c++11 -march=native -o gsm -O3 -pthread -DUSE_VC gsm.cc -lvigraimpex -lVc
43/// or: clang++ -std=c++11 -march=native -o gsm -O3 -pthread gsm.cc -lvigraimpex
44/// (with Vc; use -DUSE_HWY and -lhwy for highway, or -std=c++17 and -DUSE_STDSIMD
45/// for the std::simd backend)
46///
47/// invoke passing a colour image file. the result will be written to 'gsm.tif'
48
49#include <iostream>
50
51#include <vspline/vspline.h>
52
53#include <vigra/stdimage.hxx>
54#include <vigra/imageinfo.hxx>
55#include <vigra/impex.hxx>
56
57// we silently assume we have a colour image
58typedef vigra::RGBValue<float,0,1,2> pixel_type;
59
60// coordinate_type iss a 2D single precision coordinate
61typedef vigra::TinyVector < float , 2 > coordinate_type ;
62
63// target_type is a 2D array of pixels
64typedef vigra::MultiArray < 2 , pixel_type > target_type ;
65
66// b-spline evaluator producing float pixels
70
71int main ( int argc , char * argv[] )
72{
73 if ( argc < 2 )
74 {
75 std::cerr << "pass a colour image file as argument" << std::endl ;
76 exit( -1 ) ;
77 }
78
79 vigra::ImageImportInfo imageInfo ( argv[1] ) ;
80
81 // we want a b-spline with natural boundary conditions
82
83 vigra::TinyVector < vspline::bc_code , 2 > bcv ( vspline::NATURAL ) ;
84
85 // create cubic 2D b-spline object to receive the image data
86
87 vspline::bspline < pixel_type , 2 > bspl ( imageInfo.shape() , 3 , bcv ) ;
88
89 // load the image data into the bspline object's 'core'
90
91 vigra::importImage ( imageInfo , bspl.core ) ;
92
93 // prefilter the b-spline
94
95 bspl.prefilter() ;
96
97 // we create two evaluators for the b-spline, one for the horizontal and
98 // one for the vertical gradient. The derivatives for a b-spline are requested
99 // by passing a TinyVector with as many elements as the spline's dimension
100 // with the desired derivative degree for each dimension. Here we want the
101 // first derivative in x and y direction:
102
103 const vigra::TinyVector < float , 2 > dx1_spec { 1 , 0 } ;
104 const vigra::TinyVector < float , 2 > dy1_spec { 0 , 1 } ;
105
106 // we pass the derivative specifications to the two evaluators' constructors
107
108 ev_type xev ( bspl , dx1_spec ) ;
109 ev_type yev ( bspl , dy1_spec ) ;
110
111 // this is where the result should go:
112
113 target_type target ( imageInfo.shape() ) ;
114
115 // quick-shot solution, iterating in a loop, not vectorized
116
117 for ( int y = 0 ; y < target.shape(1) ; y++ )
118 {
119 for ( int x = 0 ; x < target.shape(0) ; x++ )
120 {
121 coordinate_type crd { float ( x ) , float ( y ) } ;
122
123 // now we get the two gradients by evaluating the gradient evaluators
124 // at the given coordinate
125
126 pixel_type dx , dy ;
127
128 xev.eval ( crd , dx ) ;
129 yev.eval ( crd , dy ) ;
130
131 // and conclude by writing the sum of the squared gradients to target
132
133 target [ crd ] = dx * dx + dy * dy ;
134 }
135 }
136
137 // store the result with vigra impex
138
139 vigra::ImageExportInfo eximageInfo ( "gsm.tif" );
140
141 std::cout << "storing the target image as 'gsm.tif'" << std::endl ;
142
143 vigra::exportImage ( target ,
144 eximageInfo
145 .setPixelType("UINT8") ) ;
146
147 exit ( 0 ) ;
148}
vigra::RGBValue< float, 0, 1, 2 > pixel_type
Definition: ca_correct.cc:103
vigra::TinyVector< float, 2 > coordinate_type
Definition: ca_correct.cc:107
vigra::MultiArray< 2, pixel_type > target_type
Definition: ca_correct.cc:115
class evaluator encodes evaluation of a spline-like object. This is a generalization of b-spline eval...
Definition: eval.h:1718
void eval(const typename base_type::in_type &_coordinate, typename base_type::out_type &_result) const
unvectorized evaluation function. this is delegated to 'feed' above, which reinterprets the arguments...
Definition: eval.h:1822
int main(int argc, char *argv[])
Definition: gsm.cc:71
vigra::RGBValue< float, 0, 1, 2 > pixel_type
Definition: gsm.cc:58
vigra::TinyVector< float, 2 > coordinate_type
Definition: gsm.cc:61
vspline::evaluator< coordinate_type, pixel_type > ev_type
Definition: gsm.cc:69
vigra::MultiArray< 2, pixel_type > target_type
Definition: gsm.cc:64
@ NATURAL
Definition: common.h:75
class bspline now builds on class bspline_base, adding coefficient storage, while bspline_base provid...
Definition: bspline.h:499
view_type core
Definition: bspline.h:566
void prefilter(vspline::xlf_type boost=vspline::xlf_type(1), int njobs=vspline::default_njobs)
prefilter converts the knot point data in the 'core' area into b-spline coefficients....
Definition: bspline.h:815
includes all headers from vspline (most of them indirectly)