Skip to content

Commit 422ca21

Browse files
committed
geometric transformation
1 parent 44cd909 commit 422ca21

7 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="Geometric Transformations" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/Geometric Transformations" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="gcc" />
14+
<Compiler>
15+
<Add option="-g" />
16+
</Compiler>
17+
</Target>
18+
<Target title="Release">
19+
<Option output="bin/Release/Geometric Transformations" prefix_auto="1" extension_auto="1" />
20+
<Option object_output="obj/Release/" />
21+
<Option type="1" />
22+
<Option compiler="gcc" />
23+
<Compiler>
24+
<Add option="-O2" />
25+
</Compiler>
26+
<Linker>
27+
<Add option="-s" />
28+
</Linker>
29+
</Target>
30+
</Build>
31+
<Compiler>
32+
<Add option="-Wall" />
33+
<Add option="-fexceptions" />
34+
</Compiler>
35+
<Unit filename="main.cpp" />
36+
<Extensions>
37+
<code_completion />
38+
<envvars />
39+
<debugger />
40+
<lib_finder disable_auto="1" />
41+
</Extensions>
42+
</Project>
43+
</CodeBlocks_project_file>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# depslib dependency file v1.0
2+
1537479219 source:c:\users\annel\desktop\geometric transformations\main.cpp
3+
<iostream>
4+
<opencv2/opencv.hpp>
5+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_layout_file>
3+
<FileVersion major="1" minor="0" />
4+
<ActiveTarget name="Debug" />
5+
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6+
<Cursor>
7+
<Cursor1 position="5058" topLine="97" />
8+
</Cursor>
9+
</File>
10+
</CodeBlocks_layout_file>
Binary file not shown.
45.6 KB
Loading

Geometric Transformations/main.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}
625 KB
Binary file not shown.

0 commit comments

Comments
 (0)