Image Interpolation with Geometric Contour Stencils
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Data Structures | Macros | Typedefs | Functions | Variables
sset.c File Reference

Contour stencil set construction. More...

#include <stdio.h>
#include "sset.h"
#include "imageio.h"
Include dependency graph for sset.c:

Go to the source code of this file.

Data Structures

struct  stencilentry
 A single contour stencil $\mathcal{S}$. More...
 
struct  stencilsetcell
 A cell in a contour stencil, $\Omega_k \subset \mathbb{R}^2$. More...
 
struct  stencilqvec
 Quantized vector. More...
 
struct  ssetstruct
 A contour stencil set $\Sigma$. More...
 

Macros

#define NUMANGLES   64
 The angular resolution for stencil quantization. More...
 
#define QUAD_RES   8
 Number of quadrature panels to use for integrals. More...
 
#define INITIAL_CAPACITY   16
 Initial capacity of a stencil set. More...
 
#define STABILITY_THRESH   1e-4
 Stability threshold. More...
 

Typedefs

typedef struct ssetstruct ssettype
 A contour stencil set $\Sigma$. More...
 

Functions

void FreeStencilSet (sset *Set)
 Free a stencil set. More...
 
ssetNewStencilSet (double StencilRadius, double(*Psf)(double, double, const void *), const void *PsfParam, double PsfRadius)
 Create a new stencil set. More...
 
int GetNumStencils (const sset *Set)
 Get the number of stencils in a stencil set. More...
 
const void * GetPsfParam (const sset *Set)
 Get the PSF parameter pointer. More...
 
double GetPsfRadius (const sset *Set)
 Get the support radius of the PSF. More...
 
double EvalPhi (const sset *Set, int S, double x, double y)
 Evaluate the $\varphi_\mathcal{S}$ associated with a stencil. More...
 
int AddStencil (sset *Set, double(*Phi)(double, double, const void *), const void *PhiParam, double Rotation, int(*DrawFun)(pen *Pen, const void *Param), const void *DrawParam, const float *DrawColor)
 Add a stencil to a stencil set. More...
 
int StencilConfusion (double *ConfusionMatrix, sset *Set)
 Compute the confusion matrix of a stencil set. More...
 
int PrintStencilConfusion (sset *Set)
 Print stencil confusion information about a stencil set. More...
 
void FitStencils (int *Stencil, const sset *Set, const float *Image, int ImageWidth, int ImageHeight)
 Determine the best-fitting stencil at each point in an image. More...
 
int DrawContours (const char *FileName, int OutputWidth, int OutputHeight, double ScaleFactor, const int *Stencil, const sset *Set, const float *Input, int InputWidth, int InputHeight)
 Visualize the estimated contours. More...
 
int DrawContoursEps (const char *EpsFile, const int *Stencil, const sset *Set, const float *Input, int InputWidth, int InputHeight)
 Visualize the estimated contours as an EPS image. More...
 
int DrawContoursSvg (const char *SvgFile, const char *BgFile, const int *Stencil, const sset *Set, const float *Input, int InputWidth, int InputHeight)
 Visualize the estimated contours as an SVG image. More...
 

Variables

double(*)(double, double,
const void *) 
GetPsf (const sset *Set)
 Get the PSF. More...
 

Detailed Description

Contour stencil set construction.

Author
Pascal Getreuer getre.nosp@m.uer@.nosp@m.gmail.nosp@m..com

Copyright (c) 2010-2011, Pascal Getreuer All rights reserved.

This program is free software: you can use, modify and/or redistribute it under the terms of the simplified BSD License. You should have received a copy of this license along this program. If not, see http://www.opensource.org/licenses/bsd-license.html.

Definition in file sset.c.

Macro Definition Documentation

#define INITIAL_CAPACITY   16

Initial capacity of a stencil set.

Definition at line 25 of file sset.c.

#define NUMANGLES   64

The angular resolution for stencil quantization.

Definition at line 21 of file sset.c.

#define QUAD_RES   8

Number of quadrature panels to use for integrals.

Definition at line 23 of file sset.c.

#define STABILITY_THRESH   1e-4

Stability threshold.

Definition at line 28 of file sset.c.

Typedef Documentation

typedef struct ssetstruct ssettype

A contour stencil set $\Sigma$.

A stencil set is a collection of contour stencils. This is represented as an array ssetstruct::Stencil of type stencilentry. For computational efficiency, an array of quantized stencil vectors stencilqvec is also stored in ssetstruct::StencilTable. The locations of the cells are stored in stencilsetcell::Cell. The point spread function (PSF) to model the sampling of the input image is specified by ssetstruct::Psf.

ssetstruct is the definition of sset, which is encapsulated by forward declaration in sset.h.

Stencil sets are managed with the following functions:

Function Documentation

int AddStencil ( sset Set,
double(*)(double, double, const void *)  Phi,
const void *  PhiParam,
double  Rotation,
int(*)(pen *Pen, const void *Param)  DrawFun,
const void *  DrawParam,
const float *  DrawColor 
)

Add a stencil to a stencil set.

Parameters
Setthe stencil set
Phithe distance function from which to derive the stencil
PhiParamparameters for the distance function
Rotationrotation of the stencil
DrawFunfunction that draws the stencil
DrawParamparameter for DrawFun
DrawColorRGB color for drawing the stencil
Returns
1 on success, 0 on failure

Definition at line 522 of file sset.c.

int DrawContours ( const char *  FileName,
int  OutputWidth,
int  OutputHeight,
double  ScaleFactor,
const int *  Stencil,
const sset Set,
const float *  Input,
int  InputWidth,
int  InputHeight 
)

Visualize the estimated contours.

Parameters
Outputoutput image
OutputWidth,OutputHeightdimensions of the output image
ScaleFactorscale factor between input and output images
Stencilbest-fitting stencils $\mathcal{S}^\star$
Setthe stencil set
Inputinput image
InputWidth,InputHeightdimensions of the input image

Renders a visualization of the estimated contours to the output image. A nearest neighbor interpolation of the input image is created where the colors are darkened and converted to grayscale. For each pixel, the best-fitting stencil is drawn by calling the stencil's stencilentry::DrawFun.

Definition at line 1049 of file sset.c.

int DrawContoursEps ( const char *  EpsFile,
const int *  Stencil,
const sset Set,
const float *  Input,
int  InputWidth,
int  InputHeight 
)

Visualize the estimated contours as an EPS image.

Parameters
EpsFileoutput EPS file name
Stencilbest-fitting stencils $\mathcal{S}^\star$
Setthe stencil set
Inputinput image
InputWidth,InputHeightdimensions of the input image
Returns
1 on success, 0 on failure

Renders a visualization of the estimated contours as an EPS image. For each pixel, the best-fitting stencil is drawn by calling the stencil's stencilentry::DrawFun.

Definition at line 1120 of file sset.c.

int DrawContoursSvg ( const char *  SvgFile,
const char *  BgFile,
const int *  Stencil,
const sset Set,
const float *  Input,
int  InputWidth,
int  InputHeight 
)

Visualize the estimated contours as an SVG image.

Parameters
SvgFileoutput SVG file name
BgFileoutput background image file name (PNG or JPEG)
Stencilbest-fitting stencils $\mathcal{S}^\star$
Setthe stencil set
Inputinput image
InputWidth,InputHeightdimensions of the input image
Returns
1 on success, 0 on failure

Renders a visualization of the estimated contours as an SVG image. For each pixel, the best-fitting stencil is drawn by calling the stencil's stencilentry::DrawFun.

Definition at line 1210 of file sset.c.

double EvalPhi ( const sset Set,
int  S,
double  x,
double  y 
)

Evaluate the $\varphi_\mathcal{S}$ associated with a stencil.

Parameters
Setthe stencil set
Sindex of the stencil
x,ylocation to evaluate

Evaluate the distance function $\varphi_\mathcal{S}$ associated with stencil S at (x,y). The stencil rotation from stencilentry::PhiTrans is applied.

Definition at line 502 of file sset.c.

void FitStencils ( int *  Stencil,
const sset Set,
const float *  Image,
int  ImageWidth,
int  ImageHeight 
)

Determine the best-fitting stencil at each point in an image.

Parameters
Stencilthe selected best-fitting stencils $\mathcal{S}^\star$
Setstencil set to use
Imagethe input image
ImageWidth,ImageHeightimage dimensions

This is the main routine for determining the best-fitting stencils on an image. The result is stored in Stencil, which should be an array of size ImageWidth by ImageHeight. For each pixel, the index of the best-fitting stencil is stored in Stencil.

For computational efficiency, the stencil fitting uses the quantized stencil vectors ssetstruct::StencilTable to approximate the cell total variations

\[ V_{i,j}(\mathcal{S}, v) \approx |\mathcal{S}_{i,j}| \cdot V_{i,j}\bigl( (\!\begin{smallmatrix} \cos \theta \\ \sin \theta \end{smallmatrix}\!), v\bigr), \quad |\theta - \angle \mathcal{S}_{i,j}| \le \tfrac{\pi}{128}. \]

First, the cell total variation are computed for each pixel at each angle 0, ..., NUMANGLES-1. Then then stencils are computed by summing the cell total variations. Finally, the stencil with the smallest total variation is identified as the best-fitting stencil,

\[ \mathcal{S}^\star(k) = \operatorname*{arg\,min}_{\mathcal{S}\in\Sigma} \, V\bigl(\mathcal{S},v(\cdot-k)\bigr). \]

If the difference between the smallest and second smallest total variations is below a threshold, the circle stencil is selected.

Definition at line 819 of file sset.c.

void FreeStencilSet ( sset Set)

Free a stencil set.

Parameters
Setthe stencil set to free

Frees all memory associated with a stencil set.

Definition at line 244 of file sset.c.

int GetNumStencils ( const sset Set)

Get the number of stencils in a stencil set.

Parameters
Setthe stencil set
Returns
the number of stencils in Set

Definition at line 453 of file sset.c.

const void* GetPsfParam ( const sset Set)

Get the PSF parameter pointer.

Parameters
Setthe stencil set
Returns
the PSF parameters

Definition at line 475 of file sset.c.

double GetPsfRadius ( const sset Set)

Get the support radius of the PSF.

Parameters
Setthe stencil set
Returns
the PSF support radius

Definition at line 486 of file sset.c.

sset* NewStencilSet ( double  StencilRadius,
double(*)(double, double, const void *)  Psf,
const void *  PsfParam,
double  PsfRadius 
)

Create a new stencil set.

Parameters
StencilRadiusthe circular radius of $\Omega$
Psfpoint spread function modeling the sampling of the input image
PsfParamparameters to pass to Psf
PsfRadiussupport radius of Psf
Returns
an sset* on success, NULL on failure

Definition at line 393 of file sset.c.

int PrintStencilConfusion ( sset Set)

Print stencil confusion information about a stencil set.

Parameters
Setthe stencil set
Returns
1 on success, 0 on failure

Prints information from the stencil confusion matrix computed by StencilConfusion().

Definition at line 715 of file sset.c.

int StencilConfusion ( double *  ConfusionMatrix,
sset Set 
)

Compute the confusion matrix of a stencil set.

Parameters
ConfusionMatrixpointer to an array to hold the result
Setthe stencil set
Returns
1 on success, 0 on failure

For each distance function $\varphi$ used in the stencil set, all stencil TVs are computed. The results are stored in column-major order in ConfusionMatrix as

\[ \text{\tt ConfusionMatrix[i + j*GetNumStencils(Set)]} = V(\mathcal{S}_j, h * \varphi_i). \]

The stencil associated with a distance function should have the lowest TV value, that is, the diagonal entries should be the smallest in each column. Small off-diagonal entries indicate stencils that are likely to be confused.

Definition at line 615 of file sset.c.

Variable Documentation

double(*)(double, double, const void*) GetPsf(const sset *Set)

Get the PSF.

Parameters
Setthe stencil set
Returns
function pointer to the PSF

Definition at line 464 of file sset.c.