55d8c24e61f8ad931b3adb6c4896f72892510d30
[experiments/opencv.git] / perspective-transform / opencv-perspective-transform.c
1 /* 
2  * opencv-perspective-transform - Example program for cvWarpPerspective()
3  *
4  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <errno.h>
22 #include <opencv2/core/core_c.h>
23 #include <opencv2/highgui/highgui_c.h>
24 #include <opencv2/imgproc/imgproc_c.h>
25
26 #define WIDTH 800
27 #define HEIGHT 480
28
29 #define X_CORRECTION 200
30
31 int main(int argc, char *argv[])
32 {
33         int ret;
34         IplImage *image;
35         IplImage *transformed_image;
36         CvRect viewport;
37         CvPoint2D32f src[4];
38         CvPoint2D32f dst[4];
39         CvMat *perspective_matrix;
40
41         if (argc != 2) {
42                 fprintf(stderr, "usage: %s <image>\n", argv[0]);
43                 ret = -EINVAL;
44                 goto out;
45         }
46
47         image = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);
48         if (!image) {
49                 fprintf(stderr, "Cannot load image\n");
50                 ret = -EINVAL;
51                 goto out;
52         }
53
54         cvNamedWindow("Viewport", 0);
55         cvResizeWindow("Viewport", WIDTH, HEIGHT);
56
57         /* Trasform only a part of the whole image */
58         viewport.x = 0;
59         viewport.y = 0;
60         viewport.width = WIDTH;
61         viewport.height = HEIGHT;
62         cvSetImageROI(image, viewport);
63
64         /* Calculate the prospective tranformation */
65         src[0].x = 0;
66         src[0].y = 0;
67         src[1].x = WIDTH;
68         src[1].y = 0;
69         src[2].x = WIDTH;
70         src[2].y = HEIGHT;
71         src[3].x = 0;
72         src[3].y = HEIGHT;
73
74         dst[0].x = X_CORRECTION;
75         dst[0].y = 0;
76         dst[1].x = WIDTH - X_CORRECTION;
77         dst[1].y = 0;
78         dst[2].x = WIDTH;
79         dst[2].y = HEIGHT;
80         dst[3].x = 0;
81         dst[3].y = HEIGHT;
82
83         perspective_matrix = cvCreateMat(3, 3, CV_64F);
84         if (!perspective_matrix) {
85                 fprintf(stderr, "Cannot create matrix\n");
86                 ret = -EINVAL;
87                 goto out_destroy_window;
88         }
89
90         perspective_matrix = cvGetPerspectiveTransform(src, dst,
91                                                        perspective_matrix);
92
93         /* Create a new image and apply the prospective transformation */
94         transformed_image = cvCreateImage(cvSize(WIDTH, HEIGHT), image->depth,
95                                           image->nChannels);
96         if (!transformed_image) {
97                 fprintf(stderr, "Cannot create trasnformed image\n");
98                 ret = -EINVAL;
99                 goto out_release_mat;
100         }
101
102         cvWarpPerspective(image, transformed_image, perspective_matrix,
103                           CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,
104                           cvScalarAll(0));
105
106         cvShowImage("Viewport", transformed_image);
107
108         cvWaitKey(0);
109
110         ret = 0;
111
112 out_release_mat:
113         cvReleaseMat(&perspective_matrix);
114 out_destroy_window:
115         cvDestroyWindow("Viewport");
116         cvReleaseImage(&image);
117 out:
118         return ret;
119 }