Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
1
/*------------------------------------------------------------------------------------------*\
1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 1 of the cookbook:  
2
   This file contains material supporting chapter 1 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
4
   by Robert Laganiere, Packt Publishing, 2011.
5

5

6
   This program is free software; permission is hereby granted to use, copy, modify,
6
   This program is free software; permission is hereby granted to use, copy, modify,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
7
   and distribute this source code, or portions thereof, for any purpose, without fee,
8
   subject to the restriction that the copyright notice may not be removed
8
   subject to the restriction that the copyright notice may not be removed
9
   or altered from any source or altered source distribution.
9
   or altered from any source or altered source distribution.
10
   The software is released on an as-is basis and without any warranties of any kind.
10
   The software is released on an as-is basis and without any warranties of any kind.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
11
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
12
   The author disclaims all warranties with regard to this software, any use,
12
   The author disclaims all warranties with regard to this software, any use,
13
   and any consequent failure, is purely the responsibility of the user.
13
   and any consequent failure, is purely the responsibility of the user.
14
 
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
16
\*------------------------------------------------------------------------------------------*/
17
17
18
#include <iostream>
18
#include <iostream>
19
#include <opencv2/core/core.hpp>
19
#include <opencv2/core/core.hpp>
20
#include <opencv2/highgui/highgui.hpp>
20
#include <opencv2/highgui/highgui.hpp>
21
21
22
// function that creates and returns an image
22
// function that creates and returns an image
23
cv::Mat function() {
23
cv::Mat function() {
24
24
25
        // create image
25
        // create image
26
        cv::Mat ima(240,320,CV_8U,cv::Scalar(100));
26
        cv::Mat ima(240,320,CV_8U,cv::Scalar(100));
27
        // return it
27
        // return it
28
        return ima;
28
        return ima;
29
}
29
}
30
30
31
int main() {
31
int main() {
32
32
33
        // create image
33
        // create image
34
        cv::Mat image;
34
        cv::Mat image;
35
        // print image size
35
        // print image size
36
        std::cout << "size: " << image.size().height << " , "
36
        std::cout << "size: " << image.size().height << " , "
37
          << image.size().width << std::endl;
37
          << image.size().width << std::endl;
38
        // open image
38
        // open image
39
        image=  cv::imread("img.jpg");
39
        image=  cv::imread("baboon1.jpg");
40
        // check if image has been successfully read
40
        // check if image has been successfully read
41
        if (!image.data) {
41
        if (!image.data) {
42
                // no image has been created…
42
                // no image has been created�
43
                return 0;
43
                return 0;
44
        }
44
        }
45
        // print image size
45
        // print image size
46
    std::cout << "size (after reading): " << image.size().height << " , "
46
    std::cout << "size (after reading): " << image.size().height << " , "
47
          << image.size().width << std::endl;
47
          << image.size().width << std::endl;
48
48
49
        // display image
49
        // display image
50
        cv::namedWindow("Original Image"); // define the window
50
        cv::namedWindow("Original Image"); // define the window
51
    cv::imshow("Original Image", image); // show the image
51
    cv::imshow("Original Image", image); // show the image
52
52
53
        // create another image
53
        // create another image
54
        cv::Mat result;
54
        cv::Mat result;
55
        // flip the image
55
        // flip the image
56
        cv::flip(image,result,1); // positive for horizontal
56
        cv::flip(image,result,1); // positive for horizontal
57
                              // 0 for vertical,                                                  
57
                              // 0 for vertical,                                                  
58
                              // negative for both
58
                              // negative for both
59
        // display result
59
        // display result
60
        cv::namedWindow("Output Image");
60
        cv::namedWindow("Output Image");
61
        cv::imshow("Output Image", result);
61
        cv::imshow("Output Image", result);
62
        // wait for key pressed
62
        // wait for key pressed
63
        cv::waitKey(0);
63
        cv::waitKey(0);
64
        // write image on file
64
        // write image on file
65
        cv::imwrite("output.bmp", result);
65
        cv::imwrite("output.bmp", result);
66
66
67
        // create two new images
67
        // create two new images
68
        cv::Mat image2, image3;
68
        cv::Mat image2, image3;
69
69
70
        image2= result; // the two images refer to the same data
70
        image2= result; // the two images refer to the same data
71
        result.copyTo(image3); // a new copy is created
71
        result.copyTo(image3); // a new copy is created
72
72
73
        // flip vertically this time
73
        // flip vertically this time
74
        cv::flip(result,result,0);
74
        cv::flip(result,result,0);
75
75
76
        // display result
76
        // display result
77
        cv::namedWindow("image 2");
77
        cv::namedWindow("image 2");
78
        cv::imshow("image 2", image2);
78
        cv::imshow("image 2", image2);
79
        cv::namedWindow("image 3");
79
        cv::namedWindow("image 3");
80
        cv::imshow("image 3", image3);
80
        cv::imshow("image 3", image3);
81
81
82
        // get a gray-level image
82
        // get a gray-level image
83
        cv::Mat gray= function();
83
        cv::Mat gray= function();
84
        // display result
84
        // display result
85
        cv::namedWindow("Gray Image");
85
        cv::namedWindow("Gray Image");
86
        cv::imshow("Gray Image", gray);
86
        cv::imshow("Gray Image", gray);
87
87
88
        // wait for key pressed
88
        // wait for key pressed
89
        cv::waitKey(0);
89
        cv::waitKey(0);
90
        return 1;
90
        return 1;
91
}
91
}
92
92
93
 
93