2 * opencv-perspective-transform - Example program for cvWarpPerspective()
4 * Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
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.
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.
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/>.
22 #include <opencv2/core/core_c.h>
23 #include <opencv2/highgui/highgui_c.h>
24 #include <opencv2/imgproc/imgproc_c.h>
29 #define X_CORRECTION 200
31 int main(int argc, char *argv[])
35 IplImage *transformed_image;
39 CvMat *perspective_matrix;
42 fprintf(stderr, "usage: %s <image>\n", argv[0]);
47 image = cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR);
49 fprintf(stderr, "Cannot load image\n");
54 cvNamedWindow("Viewport", 0);
55 cvResizeWindow("Viewport", WIDTH, HEIGHT);
57 /* Trasform only a part of the whole image */
60 viewport.width = WIDTH;
61 viewport.height = HEIGHT;
62 cvSetImageROI(image, viewport);
64 /* Calculate the prospective tranformation */
74 dst[0].x = X_CORRECTION;
76 dst[1].x = WIDTH - X_CORRECTION;
83 perspective_matrix = cvCreateMat(3, 3, CV_64F);
84 if (!perspective_matrix) {
85 fprintf(stderr, "Cannot create matrix\n");
87 goto out_destroy_window;
90 perspective_matrix = cvGetPerspectiveTransform(src, dst,
93 /* Create a new image and apply the prospective transformation */
94 transformed_image = cvCreateImage(cvSize(WIDTH, HEIGHT), image->depth,
96 if (!transformed_image) {
97 fprintf(stderr, "Cannot create trasnformed image\n");
102 cvWarpPerspective(image, transformed_image, perspective_matrix,
103 CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,
106 cvShowImage("Viewport", transformed_image);
113 cvReleaseMat(&perspective_matrix);
115 cvDestroyWindow("Viewport");
116 cvReleaseImage(&image);