|
| 1 | +#include <iostream> |
| 2 | +#include <opencv2/opencv.hpp> |
| 3 | + |
| 4 | +using namespace std; |
| 5 | +using namespace cv; |
| 6 | + |
| 7 | +int main() |
| 8 | +{ |
| 9 | + Mat image = imread("horse1.jpg", CV_LOAD_IMAGE_COLOR); |
| 10 | + /* |
| 11 | + Scaling is just resizing the image. |
| 12 | + OpenCV comes with a function cv.resize() for this purpose. |
| 13 | + The size of the image can be specified manually, or you |
| 14 | + can specify the scaling factor. Different interpolation |
| 15 | + methods are used. Preferable interpolation methods are |
| 16 | + cv.INTER_AREA for shrinking and cv.INTER_CUBIC (slow) & |
| 17 | + cv.INTER_LINEAR for zooming. |
| 18 | +
|
| 19 | + cv.resize (src, dst, dsize, fx = 0, fy = 0, interpolation = cv.INTER_LINEAR) |
| 20 | +
|
| 21 | + src - input image |
| 22 | + dst - output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src. |
| 23 | + dsize - output image size; if it equals zero, it is computed as: |
| 24 | + Either dsize or both fx and fy must be non-zero. |
| 25 | +
|
| 26 | + fx - scale factor along the horizontal axis; when it equals 0, it is computed as |
| 27 | + (double)dsize.width/src.cols |
| 28 | +
|
| 29 | + fy - scale factor along the vertical axis; when it equals 0, it is computed as |
| 30 | + (double)dsize.height/src.rows |
| 31 | +
|
| 32 | + In the mathematical field of numerical analysis, interpolation is a method of constructing new data points within the range of a discrete set of known data points. |
| 33 | +
|
| 34 | + Image interpolation occurs when you resize or distort your image from one pixel grid to another. Image resizing is necessary when you need to increase or decrease the total number of pixels, whereas remapping can occur when you are correcting for lens distortion or rotating an image. Zooming refers to increase the quantity of pixels, so that when you zoom an image, you will see more detail. |
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | + INTER_NEAREST - a nearest-neighbor interpolation |
| 39 | + INTER_LINEAR - a bilinear interpolation (used by default) |
| 40 | + INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method. |
| 41 | + INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood |
| 42 | + INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood |
| 43 | +
|
| 44 | + */ |
| 45 | + |
| 46 | + Mat scaled_img; |
| 47 | + cv::resize(image, scaled_img, Size(), 0.5, 0.5); |
| 48 | + |
| 49 | + /* |
| 50 | + Translation: |
| 51 | + Translation is the shifting of object's location. If you know the shift in (x,y) direction, let it be (tx,ty), you can create the transformation matrix M as follows: |
| 52 | + M = [ 1 0 tx ] |
| 53 | + [ 0 1 ty ] |
| 54 | +
|
| 55 | + We use the function: cv.warpAffine (src, dst, M, dsize, flags = cv.INTER_LINEAR, borderMode = cv.BORDER_CONSTANT, borderValue = new cv.Scalar()) |
| 56 | +
|
| 57 | + src – input image. |
| 58 | + dst – output image that has the size dsize and the same type as src . |
| 59 | + M – 2\times 3 transformation matrix. |
| 60 | + dsize – size of the output image. |
| 61 | + flags – combination of interpolation methods (see resize() ) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( \texttt{dst}\rightarrow\texttt{src} ). |
| 62 | + borderMode – pixel extrapolation method (see borderInterpolate()); when borderMode=BORDER_TRANSPARENT , it means that the pixels in the destination image corresponding to the “outliers” in the source image are not modified by the function. |
| 63 | + borderValue – value used in case of a constant border; by default, it is 0. |
| 64 | +
|
| 65 | + The function warpAffine transforms the source image using the specified matrix: |
| 66 | +
|
| 67 | + dst(x,y) = src(m11x + m12y + m13, m21x + m22y + m23) |
| 68 | +
|
| 69 | + */ |
| 70 | + |
| 71 | + Mat translation_image; |
| 72 | + |
| 73 | + // size of the image |
| 74 | + int width = image.cols; |
| 75 | + int height = image.rows; |
| 76 | + |
| 77 | + Mat matrix = (Mat_<double>(2,3) << 1, 0, 100, 0, 1, -30); |
| 78 | + |
| 79 | + warpAffine(image, translation_image,matrix,Size(width, height)); |
| 80 | + |
| 81 | + /* |
| 82 | + Rotate image: Rotation of an image for an angle theta is achieved by the transformation matrix of the form |
| 83 | + M = [ cos theta - sin theta ] |
| 84 | + [ sin theta cos theta ] |
| 85 | +
|
| 86 | + But OpenCV provides scaled rotation with adjustable center of rotation so that you can rotate at any location you prefer. Modified transformation matrix is given by |
| 87 | +
|
| 88 | + [ alpha beta (1 - alpha) . center . x - beta . center . y ] |
| 89 | + [ -beta alpha beta . center . x + ( 1 - alpha) . center . y] |
| 90 | +
|
| 91 | + where: alpha = scale . cos theta |
| 92 | + beta = scale . sin theta |
| 93 | +
|
| 94 | + We use the function: getRotationMatrix2D (center, angle, scale) |
| 95 | + center - center of the rotation in the source image. |
| 96 | + angle - rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner). |
| 97 | + scale - isotropic scale factor. |
| 98 | +
|
| 99 | + */ |
| 100 | + |
| 101 | + Mat rotated_image; |
| 102 | + Mat matr = getRotationMatrix2D(Point(width / 2, width / 2), 90, 1); |
| 103 | + |
| 104 | + warpAffine(image, rotated_image, matr, Size(width, height)); |
| 105 | + imshow("Image", image); |
| 106 | + imshow("Scaled Image", scaled_img); |
| 107 | + imshow("Translated Image", translation_image); |
| 108 | + imshow("Rotated Image", rotated_image); |
| 109 | + |
| 110 | + |
| 111 | + waitKey(); |
| 112 | + return 0; |
| 113 | +} |
0 commit comments