OpenCV User Manual Image Processing Part (II): Sampling, Difference and Geometry Transformation (Chinese Translation)

xiaoxiao2021-03-06  68

Sampling, difference, and geometric transformation

Translation: Hunnish, abeer

InitLineIterator

Initialization line segment iterator

INT CVinitLineItemrator (const cvarr * image, cvpoint pt1, cvpoint pt2,

CVLINEITERATOR * LINE_ITERATOR, INT CONNECTIVITY = 8);

Image

The image of the line segment.

PT1

Line segment starting point

PT2

Line segment end point

LINE_ITERATOR

Pointer to point to line segment iterator structure

CONNECTIVITY

The number of widths of the scan line segment,

4 or 8.

Function CVinitLineItemrator initialized line segment iter and returns the number of pixel points between two points. Two points must be in the image. When the iterator is initialized, all points on the raster lines connected to the two points can be obtained continuously by calling CV_Next_Line_Point. The point on the line segment is to use the 4-connected or 8-communication with the Bresenham algorithm to calculate by point.

Example: Use the line segment iterator to calculate the pixel value on the color line

Cvscalar SUM_LINE_PIXELS (iPlimage * Image, CVPoint PT1, CVOINT PT2)

{

CVLineITOR ITERATOR;

INT Blue_sum = 0, Green_Sum = 0, Red_Sum = 0;

INT Count = CvinitLineItemrator (Image, Pt1, PT2, & Iterator, 8);

For (int i = 0; i

Blue_sum = item.ptr [0];

Green_sum = iTerator.ptr [1];

Red_sum = iTerator.ptr [2];

CV_Next_Line_Point (item);

/ * Print The Pixel Coordinates: Demonstrates How To Calculate The Coordinates * /

{

INT offset, x, y;

/ * Assume That Roi is not set, OtherWise Need To Take It Into Account. * /

Offset = iterator.ptr - (Uchar *) (iMage-> imagedata);

Y = offset / image-> widthstep;

x = (Offset - Y * Image-> Widthstep) / (3 * sizeof (uchar) / * size of pixel * /);

Printf ("(% D,% D) / N", X, Y);

}

}

Return Cvscalar (Blue_Sum, Green_Sum, Red_Sum);

}

Sampleline

Read the raster line into the buffer

Int Cvsampleline (Const Cvarr * Image, CvPoint Pt1, Cvpoint PT2,

Void * buffer, int connectivity = 8);

Image

Belt image

PT1

starting point

PT2

end

Buffer

The cache area of ​​the storage line segment must have enough size to store the point

Max (|

PT2.x

-

Pt1.x

| 1, |

Pt2.y

-

Pt1.y

| 1): 8- In the case, and |

PT2.x

-

Pt1.x

| |

Pt2.y

-

Pt1.y

| 1: 4 - Connectation in the case .connectivity

The Line Connectivity, 4 OR 8.

Function Cvsampleline implements a special application of the line segment iterator. It reads all image points on the line segments determined by two points PT1 and PT2, including the end, and store it into the cache.

GetRectSubpix

Extract pixel rectangles from the image, use sub-pixel accuracy

Void CvGetRectSubpix (Const Cvarr * SRC, CVARR * DST, CVPOINT2D32F Center);

SRC

Enter the image.

DST

The rectangle extracted.

Center

The center of the extracted pixel rectangle, floating point number coordinates. The center must be located inside the image.

Function CvGetRectsubPix extracts rectangles from the image SRC:

DST (X, Y) = SRC (X Center.x - (Width (DST) -1) * 0.5, Y Center.y - (Height (DST) -1) * 0.5)

Among them, non-integer pixel point coordinates are extracted with double linear differences. For multi-channel images, each channel is individually extracted separately. The rectangular center must be in the inside of the image, and the entire rectangle can be partially not in the image. In this case, the copy boundary is used to get pixel values ​​outside the image boundary (Hunnish:

Getquadranglesubpix

Extract pixel quadrangios, use sub-pixel accuracy

Void CvgetquadrangLesubpix (Const Cvarr * SRC, CVARR * DST, Const Cvmat * Map_matrix,

INT FILL_OUTLIERS = 0, cvscalar fill_value = cvscalataLL (0));

SRC

Enter the image.

DST

The extracted quadrilateral shape.

Map_matrix

3 × 2 transform matrix [

A

|

B

] (See discussion).

Fill_outliers

This flag specifies whether the pixel point outside the original image boundary uses the replication mode (

Fill_outliers = 0) Perform difference or set it to the specified value (

Fill_outliers = 1).

Fill_Value

Set a fixed value for pixels outside the original image boundary,

Fill_outliers

= 1.

The function cvgetquadrangleSubPix extracts the quadrilateral shape from the image SRC, using sub-pixel accuracy, and stores the result to DST, the calculation formula is:

DST (X Width (DST) / 2, Y Height (DST) / 2) = SRC (A11X A12Y B1, A21X A22Y B2),

WHERE A and B Are Taken from Map_matrix

| A11 A12 B1 |

Map_matrix = | | |

| A21 A22 B2 |

Among them, the pixel point values ​​of non-integer coordinates A • (X, Y) T B are obtained by bilinear transformation. Each channel of a multi-channel image is calculated separately.

Example: Image rotation using CvgetQuadrangleSubpix

#include "cv.h"

#include "highgui.h"

#include "math.h"

INT main (int Argc, char ** argv)

{

IPLIMAGE * SRC;

/ * The First Command Line Parameter Must Be Image File Name * /

IF (Argc == 2 && (src = CVLoadImage (Argv [1], -1)))! = 0) {

IPLIMAGE * DST = CVCloneImage (SRC);

INT Delta = 1;

INT ANGLE = 0;

CvnamedWindow ("SRC", 1);

CvShowImage ("SRC", SRC);

For (;;)

{

Float M [6];

Double factor = (COS (Angle * CV_PI / 180.) 1.1) * 3;

CVMAT M = CVMAT (2, 3, CV_32F, M);

INT W = SRC-> Width;

INT H = SRC-> Height;

M [0] = (float) (-angle * 2 * CV_PI / 180.));

M [1] = (float) (-angle * 2 * CV_PI / 180.));

M [2] = w * 0.5f;

M [3] = -m [1];

M [4] = m [0];

M [5] = h * 0.5f;

Cvgetquadranglesubpix (SRC, DST, & M, 1, CVSCALALL (0));

CvnamedWindow ("DST", 1);

CvShowImage ("DST", DST);

IF (CvwaitKey (5) == 27)

Break;

Angle = (Angle Delta)% 360;

}

}

Return 0;

}

Resize

Image size transformation

Void Cvresize (Const Cvarr * SRC, CVARR * DST, INTITETERNEAR);

SRC

Enter the image.

DST

Output image.

Interpolation

Difference method:

CV_INTER_NN - Recent neighbor difference, CV_INTER_LINEAR - Double linear difference (default) CV_INTER_AREA - use pixel relationships. When the image is reduced, the method can avoid corrugation. When the image is amplified, similar to the CV_Inter_nn method .. CV_Inter_cubic - cubic difference.

Function CVResize changes the image SRC to the same size as DST. If the ROI is set, the function will support ROI according to regular.

Warpaffine

Avoid transform to images

Void Cvwarpaffine (Const Cvarr * SRC, CVARR * DST, Const Cvmat * Map_matrix,

INT FLAGS = CV_INTER_LINEAR CV_WARP_FILL_OUTLIERS,

Cvscalar Fillval = CvscalataLL (0));

SRC

Enter the image.

DST

Output image.

Map_matrix

2 × 3 transform matrix

Flags

Differential methods and switch options:

CV_WARP_FILL_OUTLIERS - Pack all the pixels of all reduced images. If the partial pixel falls outside the boundary of the input image, then their value is set to FillVal. CV_WARP_INVERSE_MAP - Specifying Matrix to output an image to the input image inverse transform, so it can be used directly to do pixel differences. Otherwise, the function gets a reverse transform from MAP_MAMATRIX.

Fillval

Value used to fill outside the boundary

Function Cvwarpaffine Enter an image using the matrix transformation specified below:

DST (X ', Y') <- SRC (X, Y)

If you do not specify CV_WARP_INVERSE_MAP, (x ', y') t = map_matrix • (x, y, 1) t b,

Otherwise, (x, y) T = Map_matrix • (x ', y & apos, 1) t b

The function is similar to cvgetquadrangleSubpix, but it is not exactly the same. Cvwarpaffine requires the input and output images with the same data type, and there is a larger resource overhead (so that the large image is not suitable) and the portion of the output image can remain unchanged. CvgetQuadrangleSubPix can accurately extract the quadrilateral to the floating point cache in the 8-bit image, with a relatively small system overhead, and always change the contents of the output image.

To change the sparse matrix, use the function CVTransform in CXCORE.

2DROTATIONMAMATRIX

Calculate the affine transformation matrix of two-dimensional rotation

Cvmat * CV2DROTAMATRIX (CvpoInt2D32f Center, Double Angle,

Double Scale, CVMAT * MAP_MAMATRIX);

Center

Enter the rotation center of the image

Angle

Rotate angle (degrees). Positive value indicates counterclockwise rotation

(Coordinate original assumptions in the upper left corner).

Scale

Measure factor

Map_matrix

Output

2 × 3 matrix pointer

Function CV2DROTATIONMAMATRIX calculation matrix:

[α β | (1-α) * center.x - β * center.y]

[-β α | β * center.x (1-α) * center.y]

Where α = scale * cos (Angle), β = scale * sin (Angle)

This transform map rotates the center to itself. If this is not a purpose, you should adjust the translation (hunnish: this passage: The Transformation Maps the rotation center to itself. If this is not the purpose, the shift be adjusted)

WARPPERSPECTIVE

Visually transform images

Void Cvwarpperspective (Const Cvarr * SRC, CVARR * DST, Const CVMAT * MAP_MAMATRIX,

INT FLAGS = CV_INTER_LINEAR CV_WARP_FILL_OUTLIERS,

Cvscalar Fillval = CvscalataLL (0));

SRC

Enter the image.

DST

Output image.

Map_matrix

3 × 3 transform matrix

Flags

Switch options for differential methods:

CV_WARP_FILL_OUTLIERS - Pack all the pixels of all reduced images. If the partial pixel falls outside the boundary of the input image, then their value is set to FillVal. CV_WARP_INVERSE_MAP - Specifying Matrix to output an image to the input image inverse transform, so it can be used directly to do pixel differences. Otherwise, the function gets a reverse transform from MAP_MAMATRIX.

Fillval

Value used to fill outside the boundary

Function CvwarppPective Use the specified matrix transformation in the following:

DST (X ', Y') <- SRC (X, Y)

If you specify CV_WARP_INVERSE_MAP, (TX ', TY', T) T = Map_Matrix • (x, y, 1) T B

Otherwise, (TX, TY, T) T = Map_Matrix • (X ', Y & APOS, 1) T B

To change the sparse matrix, use the function CVTransform in CXCORE. WARPPERSPECTIVEQMATRIX

Calculate the perspective transformation matrix with 4 corresponding points

Cvmat * Cvwarpperspectiveqmatrix (const cvpoint2d32f * src,

Const CvpoInt2D32F * DST,

CVMAT * map_matrix;

SRC

Input image quadrilateral

4 point coordinates

DST

Output image corresponding quadrangular

4 point coordinates

Map_matrix

Output

3 × 3 matrix

Function CvwarpPerspectiveqmatrix calculates a perspective transformation matrix, making:

(TiX'i, Tiy'i, Ti) T = Matrix • (xi, yi, 1) t

WHERE DST (I) = (X'i, Y'I), SRC (I) = (Xi, Yi), i = 0..3.

转载请注明原文地址:https://www.9cbs.com/read-120624.html

New Post(0)