672795a6a7fdf3e3c19b5199697e7cf6c52a66ba
[experiments/opencv.git] / perspective-transform / opencv-perspective-transform.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <opencv2/core/core_c.h>
4 #include <opencv2/highgui/highgui_c.h>
5 #include <opencv2/imgproc/imgproc_c.h>
6
7 #define WIDTH 800
8 #define HEIGHT 480
9
10 #define X_CORRECTION 200
11
12 int main(int argc, char *argv[])
13 {
14         int ret;
15         IplImage *image;
16         IplImage *transformed_image;
17         CvRect viewport;
18         CvPoint2D32f src[4];
19         CvPoint2D32f dst[4];
20         CvMat *perspective_matrix;
21
22         if (argc != 2) {
23                 fprintf(stderr, "usage: %s <image>\n", argv[0]);
24                 ret = -EINVAL;
25                 goto out;
26         }
27
28         image = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);
29         if (!image) {
30                 fprintf(stderr, "Cannot load image\n");
31                 ret = -EINVAL;
32                 goto out;
33         }
34
35         cvNamedWindow("Viewport", 0);
36         cvResizeWindow("Viewport", WIDTH, HEIGHT);
37
38         /* Trasform only a part of the whole image */
39         viewport.x = 0;
40         viewport.y = 0;
41         viewport.width = WIDTH;
42         viewport.height = HEIGHT;
43         cvSetImageROI(image, viewport);
44
45         /* Calculate the prospective tranformation */
46         src[0].x = 0;
47         src[0].y = 0;
48         src[1].x = WIDTH;
49         src[1].y = 0;
50         src[2].x = WIDTH;
51         src[2].y = HEIGHT;
52         src[3].x = 0;
53         src[3].y = HEIGHT;
54
55         dst[0].x = X_CORRECTION;
56         dst[0].y = 0;
57         dst[1].x = WIDTH - X_CORRECTION;
58         dst[1].y = 0;
59         dst[2].x = WIDTH;
60         dst[2].y = HEIGHT;
61         dst[3].x = 0;
62         dst[3].y = HEIGHT;
63
64         perspective_matrix = cvCreateMat(3, 3, CV_64F);
65         if (!perspective_matrix) {
66                 fprintf(stderr, "Cannot create matrix\n");
67                 ret = -EINVAL;
68                 goto out_destroy_window;
69         }
70
71         perspective_matrix = cvGetPerspectiveTransform(src, dst,
72                                                        perspective_matrix);
73
74         /* Create a new image and apply the prospective transformation */
75         transformed_image = cvCreateImage(cvSize(WIDTH, HEIGHT), image->depth,
76                                           image->nChannels);
77         if (!transformed_image) {
78                 fprintf(stderr, "Cannot create trasnformed image\n");
79                 ret = -EINVAL;
80                 goto out_release_mat;
81         }
82
83         cvWarpPerspective(image, transformed_image, perspective_matrix,
84                           CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,
85                           cvScalarAll(0));
86
87         cvShowImage("Viewport", transformed_image);
88
89         cvWaitKey(0);
90
91         ret = 0;
92
93 out_release_mat:
94         cvReleaseMat(&perspective_matrix);
95 out_destroy_window:
96         cvDestroyWindow("Viewport");
97         cvReleaseImage(&image);
98 out:
99         return ret;
100 }