Leptonica  1.54
Файл src/warper.c
#include <math.h>
#include "allheaders.h"

Макросы

#define USE_SIN_TABLE   0

Функции

static l_float64generateRandomNumberArray (l_int32 size)
static l_int32 applyWarpTransform (l_float32 xmag, l_float32 ymag, l_float32 xfreq, l_float32 yfreq, l_float64 *randa, l_int32 nx, l_int32 ny, l_int32 xp, l_int32 yp, l_float32 *px, l_float32 *py)
PIXpixSimpleCaptcha (PIX *pixs, l_int32 border, l_int32 nterms, l_uint32 seed, l_uint32 color, l_int32 cmapflag)
PIXpixRandomHarmonicWarp (PIX *pixs, l_float32 xmag, l_float32 ymag, l_float32 xfreq, l_float32 yfreq, l_int32 nx, l_int32 ny, l_uint32 seed, l_int32 grayval)
PIXpixWarpStereoscopic (PIX *pixs, l_int32 zbend, l_int32 zshiftt, l_int32 zshiftb, l_int32 ybendt, l_int32 ybendb, l_int32 redleft)
PIXpixStretchHorizontal (PIX *pixs, l_int32 dir, l_int32 type, l_int32 hmax, l_int32 operation, l_int32 incolor)
PIXpixStretchHorizontalSampled (PIX *pixs, l_int32 dir, l_int32 type, l_int32 hmax, l_int32 incolor)
PIXpixStretchHorizontalLI (PIX *pixs, l_int32 dir, l_int32 type, l_int32 hmax, l_int32 incolor)
PIXpixQuadraticVShear (PIX *pixs, l_int32 dir, l_int32 vmaxt, l_int32 vmaxb, l_int32 operation, l_int32 incolor)
PIXpixQuadraticVShearSampled (PIX *pixs, l_int32 dir, l_int32 vmaxt, l_int32 vmaxb, l_int32 incolor)
PIXpixQuadraticVShearLI (PIX *pixs, l_int32 dir, l_int32 vmaxt, l_int32 vmaxb, l_int32 incolor)
PIXpixStereoFromPair (PIX *pix1, PIX *pix2, l_float32 rwt, l_float32 gwt, l_float32 bwt)

Переменные

static const l_float32 L_DEFAULT_RED_WEIGHT = 0.0
static const l_float32 L_DEFAULT_GREEN_WEIGHT = 0.7
static const l_float32 L_DEFAULT_BLUE_WEIGHT = 0.3

Макросы

#define USE_SIN_TABLE   0

Функции

static l_int32 applyWarpTransform ( l_float32  xmag,
l_float32  ymag,
l_float32  xfreq,
l_float32  yfreq,
l_float64 randa,
l_int32  nx,
l_int32  ny,
l_int32  xp,
l_int32  yp,
l_float32 px,
l_float32 py 
) [static]

applyWarpTransform()

Notes: (1) Uses the internal sin function.

static l_float64 * generateRandomNumberArray ( l_int32  size) [static]
PIX* pixQuadraticVShear ( PIX pixs,
l_int32  dir,
l_int32  vmaxt,
l_int32  vmaxb,
l_int32  operation,
l_int32  incolor 
)
PIX* pixQuadraticVShearLI ( PIX pixs,
l_int32  dir,
l_int32  vmaxt,
l_int32  vmaxb,
l_int32  incolor 
)

pixQuadraticVShearLI()

Input: pixs (8 or 32 bpp, or colormapped) dir (L_WARP_TO_LEFT or L_WARP_TO_RIGHT) vmaxt (max vertical displacement at edge and at top) vmaxb (max vertical displacement at edge and at bottom) incolor (L_BRING_IN_WHITE or L_BRING_IN_BLACK) Return: pixd (stretched), or null on error

Notes: (1) See pixQuadraticVShear() for details.

PIX* pixQuadraticVShearSampled ( PIX pixs,
l_int32  dir,
l_int32  vmaxt,
l_int32  vmaxb,
l_int32  incolor 
)

pixQuadraticVShearSampled()

Input: pixs (1, 8 or 32 bpp) dir (L_WARP_TO_LEFT or L_WARP_TO_RIGHT) vmaxt (max vertical displacement at edge and at top) vmaxb (max vertical displacement at edge and at bottom) incolor (L_BRING_IN_WHITE or L_BRING_IN_BLACK) Return: pixd (stretched), or null on error

Notes: (1) See pixQuadraticVShear() for details.

PIX* pixRandomHarmonicWarp ( PIX pixs,
l_float32  xmag,
l_float32  ymag,
l_float32  xfreq,
l_float32  yfreq,
l_int32  nx,
l_int32  ny,
l_uint32  seed,
l_int32  grayval 
)

pixRandomHarmonicWarp()

Input: pixs (8 bpp; no colormap) xmag, ymag (maximum magnitude of x and y distortion) xfreq, yfreq (maximum magnitude of x and y frequency) nx, ny (number of x and y harmonic terms) seed (of random number generator) grayval (color brought in from the outside; 0 for black, 255 for white) Return: pixd (8 bpp; no colormap), or null on error

Notes: (1) To generate the warped image p(x',y'), set up the transforms that are in getWarpTransform(). For each (x',y') in the dest, the warp function computes the originating location (x, y) in the src. The differences (x - x') and (y - y') are given as a sum of products of sinusoidal terms. Each term is multiplied by a maximum amplitude (in pixels), and the angle is determined by a frequency and phase, and depends on the (x', y') value of the dest. Random numbers with a variable input seed are used to allow the warping to be unpredictable. A linear interpolation is used to find the value for the source at (x, y); this value is written into the dest. (2) This can be used to generate 'captcha's, which are somewhat randomly distorted images of text. A typical set of parameters for a captcha are: xmag = 4.0 ymag = 6.0 xfreq = 0.10 yfreq = 0.13 nx = 3 ny = 3 Other examples can be found in prog/warptest.c.

PIX* pixSimpleCaptcha ( PIX pixs,
l_int32  border,
l_int32  nterms,
l_uint32  seed,
l_uint32  color,
l_int32  cmapflag 
)

pixSimpleCaptcha()

Input: pixs (8 bpp; no colormap) border (added white pixels on each side) nterms (number of x and y harmonic terms) seed (of random number generator) color (for colorizing; in 0xrrggbb00 format; use 0 for black) cmapflag (1 for colormap output; 0 for rgb) Return: pixd (8 bpp cmap or 32 bpp rgb), or null on error

Notes: (1) This uses typical default values for generating captchas. The magnitudes of the harmonic warp are typically to be smaller when more terms are used, even though the phases are random. See, for example, prog/warptest.c.

PIX* pixStereoFromPair ( PIX pix1,
PIX pix2,
l_float32  rwt,
l_float32  gwt,
l_float32  bwt 
)

pixStereoFromPair()

Input: pix1 (32 bpp rgb) pix2 (32 bpp rgb) rwt, gwt, bwt (weighting factors used for each component in pix1 to determine the output red channel) Return: pixd (stereo enhanced), or null on error

Notes: (1) pix1 and pix2 are a pair of stereo images, ideally taken concurrently in the same plane, with some lateral translation. (2) The output red channel is determined from . The output green and blue channels are taken from the green and blue channels, respectively, of . (3) The weights determine how much of each component in goes into the output red channel. The sum of weights must be 1.0. If it's not, we scale the weights to satisfy this criterion. (4) The most general pixel mapping allowed here is: rval = rwt * r1 + gwt * g1 + bwt * b1 (from pix1) gval = g2 (from pix2) bval = b2 (from pix2) (5) The simplest method is to use rwt = 1.0, gwt = 0.0, bwt = 0.0, but this causes unpleasant visual artifacts with red in the image. Use of green and blue from in the red channel, instead of red, tends to fix that problem.

PIX* pixStretchHorizontal ( PIX pixs,
l_int32  dir,
l_int32  type,
l_int32  hmax,
l_int32  operation,
l_int32  incolor 
)
PIX* pixStretchHorizontalLI ( PIX pixs,
l_int32  dir,
l_int32  type,
l_int32  hmax,
l_int32  incolor 
)

pixStretchHorizontalLI()

Input: pixs (1, 8 or 32 bpp) dir (L_WARP_TO_LEFT or L_WARP_TO_RIGHT) type (L_LINEAR_WARP or L_QUADRATIC_WARP) hmax (horizontal displacement at edge) incolor (L_BRING_IN_WHITE or L_BRING_IN_BLACK) Return: pixd (stretched/compressed), or null on error

Notes: (1) See pixStretchHorizontal() for details.

PIX* pixStretchHorizontalSampled ( PIX pixs,
l_int32  dir,
l_int32  type,
l_int32  hmax,
l_int32  incolor 
)

pixStretchHorizontalSampled()

Input: pixs (1, 8 or 32 bpp) dir (L_WARP_TO_LEFT or L_WARP_TO_RIGHT) type (L_LINEAR_WARP or L_QUADRATIC_WARP) hmax (horizontal displacement at edge) incolor (L_BRING_IN_WHITE or L_BRING_IN_BLACK) Return: pixd (stretched/compressed), or null on error

Notes: (1) See pixStretchHorizontal() for details.

PIX* pixWarpStereoscopic ( PIX pixs,
l_int32  zbend,
l_int32  zshiftt,
l_int32  zshiftb,
l_int32  ybendt,
l_int32  ybendb,
l_int32  redleft 
)

pixWarpStereoscopic()

Input: pixs (any depth, colormap ok) zbend (horizontal separation in pixels of red and cyan at the left and right sides, that gives rise to quadratic curvature out of the image plane) zshiftt (uniform pixel translation difference between red and cyan, that pushes the top of the image plane away from the viewer (zshiftt > 0) or towards the viewer (zshiftt < 0)) zshiftb (uniform pixel translation difference between red and cyan, that pushes the bottom of the image plane away from the viewer (zshiftb > 0) or towards the viewer (zshiftb < 0)) ybendt (multiplicative parameter for in-plane vertical displacement at the left or right edge at the top: y = ybendt * (2x/w - 1)^2 ) ybendb (same as ybendt, except at the left or right edge at the bottom) redleft (1 if the red filter is on the left; 0 otherwise) Return: pixd (32 bpp), or null on error

Notes: (1) This function splits out the red channel, mucks around with it, then recombines with the unmolested cyan channel. (2) By using a quadratically increasing shift of the red pixels horizontally and away from the vertical centerline, the image appears to bend quadratically out of the image plane, symmetrically with respect to the vertical center line. A positive value of causes the plane to be curved away from the viewer. We use linearly interpolated stretching to avoid the appearance of kinks in the curve. (3) The parameters and tilt the image plane about a horizontal line through the center, and at the same time move that line either in toward the viewer or away. This is implemented by a combination of horizontal shear about the center line (for the tilt) and horizontal translation (to move the entire plane in or out). A positive value of moves the top of the plane away from the viewer, and a positive value of moves the bottom of the plane away. We use linear interpolated shear to avoid visible vertical steps in the tilted image. (4) The image can be bent in the plane and about the vertical centerline. The centerline does not shift, and the parameter gives the relative shift at left and right edges, with a downward shift for positive values of . (6) When writing out a steroscopic (red/cyan) image in jpeg, first call pixSetChromaSampling(pix, 0) to get sufficient resolution in the red channel. (7) Typical values are: zbend = 20 zshiftt = 15 zshiftb = -15 ybendt = 30 ybendb = 0 If the disparity z-values are too large, it is difficult for the brain to register the two images. (8) This function has been cleverly reimplemented by Jeff Breidenbach. The original implementation used two 32 bpp rgb images, and merged them at the end. The result is somewhat faded, and has a parameter "thresh" that controls the amount of color in the result. (The present implementation avoids these two problems, skipping both the colorization and the alpha blending at the end, and is about 3x faster) The basic operations with 32 bpp are as follows: // Immediate conversion to 32 bpp Pix *pixt1 = pixConvertTo32(pixs); // Do vertical shear Pix *pixr = pixQuadraticVerticalShear(pixt1, L_WARP_TO_RIGHT, ybendt, ybendb, L_BRING_IN_WHITE); // Colorize two versions, toward red and cyan Pix *pixc = pixCopy(NULL, pixr); l_int32 thresh = 150; // if higher, get less original color pixColorGray(pixr, NULL, L_PAINT_DARK, thresh, 255, 0, 0); pixColorGray(pixc, NULL, L_PAINT_DARK, thresh, 0, 255, 255); // Shift the red pixels; e.g., by stretching Pix *pixrs = pixStretchHorizontal(pixr, L_WARP_TO_RIGHT, L_QUADRATIC_WARP, zbend, L_INTERPOLATED, L_BRING_IN_WHITE); // Blend the shifted red and unshifted cyan 50:50 Pix *pixg = pixCreate(w, h, 8); pixSetAllArbitrary(pixg, 128); pixd = pixBlendWithGrayMask(pixrs, pixc, pixg, 0, 0);


Переменные

const l_float32 L_DEFAULT_BLUE_WEIGHT = 0.3 [static]
const l_float32 L_DEFAULT_GREEN_WEIGHT = 0.7 [static]
const l_float32 L_DEFAULT_RED_WEIGHT = 0.0 [static]