Leptonica 1.68
C Image Processing Library

edge.c File Reference

Sobel and related edge filters. More...

#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"

Go to the source code of this file.

Functions

PIXpixSobelEdgeFilter (PIX *pixs, l_int32 orientflag)
PIXpixTwoSidedEdgeFilter (PIX *pixs, l_int32 orientflag)
l_int32 pixMeasureEdgeSmoothness (PIX *pixs, l_int32 side, l_int32 minjump, l_int32 minreversal, l_float32 *pjpl, l_float32 *pjspl, l_float32 *prpl, const char *debugfile)
NUMApixGetEdgeProfile (PIX *pixs, l_int32 side, const char *debugfile)
l_int32 pixGetLastOffPixelInRun (PIX *pixs, l_int32 x, l_int32 y, l_int32 direction, l_int32 *ploc)
l_int32 pixGetLastOnPixelInRun (PIX *pixs, l_int32 x, l_int32 y, l_int32 direction, l_int32 *ploc)

Detailed Description

Sobel and related edge filters.

    Sobel edge detecting filter
        PIX      *pixSobelEdgeFilter()

    Two-sided edge gradient filter
        PIX      *pixTwoSidedEdgeFilter()

    Measurement of edge smoothness
        l_int32   pixMeasureEdgeSmoothness()
        NUMA     *pixGetEdgeProfile()
        l_int32   pixGetLastOffPixelInRun()
        l_int32   pixGetLastOnPixelInRun()


The Sobel edge detector uses these two simple gradient filters.

     1    2    1             1    0   -1 
     0    0    0             2    0   -2
    -1   -2   -1             1    0   -1

    (horizontal)             (vertical)

To use both the vertical and horizontal filters, set the orientation
flag to L_ALL_EDGES; this sums the abs. value of their outputs,
clipped to 255.

See comments below for displaying the resulting image with
the edges dark, both for 8 bpp and 1 bpp.

Definition in file edge.c.


Function Documentation

PIX* pixSobelEdgeFilter ( PIX pixs,
l_int32  orientflag 
)

pixSobelEdgeFilter()

Input: pixs (8 bpp; no colormap) orientflag (L_HORIZONTAL_EDGES, L_VERTICAL_EDGES, L_ALL_EDGES) Return: pixd (8 bpp, edges are brighter), or null on error

Notes: (1) Invert pixd to see larger gradients as darker (grayscale). (2) To generate a binary image of the edges, threshold the result using pixThresholdToBinary(). If the high edge values are to be fg (1), invert after running pixThresholdToBinary(). (3) Label the pixels as follows: 1 4 7 2 5 8 3 6 9 Read the data incrementally across the image and unroll the loop. (4) This runs at about 45 Mpix/sec on a 3 GHz processor.

Definition at line 78 of file edge.c.

References ERROR_PTR, GET_DATA_BYTE, L_ABS, L_ALL_EDGES, L_HORIZONTAL_EDGES, L_MIN, L_VERTICAL_EDGES, NULL, pixAddMirroredBorder(), pixCreateTemplate(), L_Dewarp::pixd, pixDestroy(), pixGetData(), pixGetDimensions(), pixGetWpl(), PROCNAME, and SET_DATA_BYTE.

Referenced by main(), pixColorsForQuantization(), pixLocateBarcodes(), and pixThresholdSpreadNorm().

PIX* pixTwoSidedEdgeFilter ( PIX pixs,
l_int32  orientflag 
)

pixTwoSidedEdgeFilter()

Input: pixs (8 bpp; no colormap) orientflag (L_HORIZONTAL_EDGES, L_VERTICAL_EDGES) Return: pixd (8 bpp, edges are brighter), or null on error

Notes: (1) For detecting vertical edges, this considers the difference of the central pixel from those on the left and right. For situations where the gradient is the same sign on both sides, this computes and stores the minimum (absolute value of the) difference. The reason for checking the sign is that we are looking for pixels within a transition. By contrast, for single pixel noise, the pixel value is either larger than or smaller than its neighbors, so the gradient would change direction on each side. Horizontal edges are handled similarly, looking for vertical gradients. (2) To generate a binary image of the edges, threshold the result using pixThresholdToBinary(). If the high edge values are to be fg (1), invert after running pixThresholdToBinary(). (3) This runs at about 60 Mpix/sec on a 3 GHz processor. It is about 30% faster than Sobel, and the results are similar.

Definition at line 184 of file edge.c.

References ERROR_PTR, GET_DATA_BYTE, L_HORIZONTAL_EDGES, L_MAX, L_MIN, L_VERTICAL_EDGES, NULL, pixCreateTemplate(), L_Dewarp::pixd, pixGetData(), pixGetDimensions(), pixGetWpl(), PROCNAME, and SET_DATA_BYTE.

Referenced by pixThresholdSpreadNorm().

l_int32 pixMeasureEdgeSmoothness ( PIX pixs,
l_int32  side,
l_int32  minjump,
l_int32  minreversal,
l_float32 pjpl,
l_float32 pjspl,
l_float32 prpl,
const char *  debugfile 
)

pixMeasureEdgeSmoothness()

Input: pixs (1 bpp) side (L_FROM_LEFT, L_FROM_RIGHT, L_FROM_TOP, L_FROM_BOTTOM) minjump (minimum jump to be counted; >= 1) minreversal (minimum reversal size for new peak or valley) &jpl (<optional return>=""> jumps/length: number of jumps, normalized to length of component side) &jspl (<optional return>=""> jumpsum/length: sum of all sufficiently large jumps, normalized to length of component side) &rpl (<optional return>=""> reversals/length: number of peak-to-valley or valley-to-peak reversals, normalized to length of component side) debugfile (<optional> displays constructed edge; use NULL for no output) Return: 0 if OK, 1 on error

Notes: (1) This computes three measures of smoothness of the edge of a connected component: * jumps/length: (jpl) the number of jumps of size >= , normalized to the length of the side * jump sum/length: (jspl) the sum of all jump lengths of size >= , normalized to the length of the side * reversals/length: (rpl) the number of peak <--> valley reversals, using as a minimum deviation of the peak or valley from its preceeding extremum, normalized to the length of the side (2) The input pix should be a single connected component, but this is not required.

Definition at line 292 of file edge.c.

References ERROR_INT, L_ABS, L_FROM_BOTTOM, L_FROM_LEFT, L_FROM_RIGHT, L_FROM_TOP, NULL, numaDestroy(), numaFindExtrema(), numaGetCount(), numaGetIValue(), pixGetDepth(), pixGetEdgeProfile(), and PROCNAME.

Referenced by PixAddEdgeData().

NUMA* pixGetEdgeProfile ( PIX pixs,
l_int32  side,
const char *  debugfile 
)

pixGetEdgeProfile()

Input: pixs (1 bpp) side (L_FROM_LEFT, L_FROM_RIGHT, L_FROM_TOP, L_FROM_BOTTOM) debugfile (<optional> displays constructed edge; use NULL for no output) Return: na (of fg edge pixel locations), or null on error

Definition at line 369 of file edge.c.

References ERROR_PTR, IFF_PNG, L_FROM_BOTTOM, L_FROM_LEFT, L_FROM_RIGHT, L_FROM_TOP, NULL, numaAddNumber(), numaCreate(), numaGetCount(), numaGetIValue(), pixcmapAddColor(), pixcmapGetCount(), pixConvertTo8(), pixDestroy(), pixGetColormap(), pixGetDepth(), pixGetDimensions(), pixGetLastOffPixelInRun(), pixGetLastOnPixelInRun(), pixGetPixel(), pixSetPixel(), pixWrite(), PROCNAME, and TRUE.

Referenced by pixMeasureEdgeSmoothness().

l_int32 pixGetLastOffPixelInRun ( PIX pixs,
l_int32  x,
l_int32  y,
l_int32  direction,
l_int32 ploc 
)
l_int32 pixGetLastOnPixelInRun ( PIX pixs,
l_int32  x,
l_int32  y,
l_int32  direction,
l_int32 ploc 
)
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines