Subversion Repositories OpenCV2-Cookbook

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 5 of the cookbook:  
3
   Computer Vision Programming using the OpenCV Library.
4
   by Robert Laganiere, Packt Publishing, 2011.
5
 
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,
8
   subject to the restriction that the copyright notice may not be removed
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.
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,
13
   and any consequent failure, is purely the responsibility of the user.
14
 
15
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
16
\*------------------------------------------------------------------------------------------*/
17
 
18
#include <opencv2/core/core.hpp>
19
#include <opencv2/imgproc/imgproc.hpp>
20
#include <opencv2/highgui/highgui.hpp>
21
 
22
int main()
23
{
24
        // Read input image
25
        cv::Mat image= cv::imread("../binary.bmp");
26
        if (!image.data)
27
                return 0;
28
 
29
    // Display the image
30
        cv::namedWindow("Image");
31
        cv::imshow("Image",image);
32
 
33
        // Erode the image
34
        cv::Mat eroded;
35
        cv::erode(image,eroded,cv::Mat());
36
 
37
    // Display the eroded image
38
        cv::namedWindow("Eroded Image");
39
        cv::imshow("Eroded Image",eroded);
40
 
41
        // Dilate the image
42
        cv::Mat dilated;
43
        cv::dilate(image,dilated,cv::Mat());
44
 
45
    // Display the dialted image
46
        cv::namedWindow("Dilated Image");
47
        cv::imshow("Dilated Image",dilated);
48
 
49
        // Erode the image with a larger s.e.
50
        cv::Mat element(7,7,CV_8U,cv::Scalar(1));
51
        cv::erode(image,eroded,element);
52
 
53
    // Display the eroded image
54
        cv::namedWindow("Eroded Image (7x7)");
55
        cv::imshow("Eroded Image (7x7)",eroded);
56
 
57
        // Erode the image 3 times.
58
        cv::erode(image,eroded,cv::Mat(),cv::Point(-1,-1),3);
59
 
60
    // Display the eroded image
61
        cv::namedWindow("Eroded Image (3 times)");
62
        cv::imshow("Eroded Image (3 times)",eroded);
63
 
64
        // Close the image
65
        cv::Mat element5(5,5,CV_8U,cv::Scalar(1));
66
        cv::Mat closed;
67
        cv::morphologyEx(image,closed,cv::MORPH_CLOSE,element5);
68
 
69
    // Display the opened image
70
        cv::namedWindow("Closed Image");
71
        cv::imshow("Closed Image",closed);
72
 
73
        // Open the image
74
        cv::Mat opened;
75
        cv::morphologyEx(image,opened,cv::MORPH_OPEN,element5);
76
 
77
    // Display the opened image
78
        cv::namedWindow("Opened Image");
79
        cv::imshow("Opened Image",opened);
80
 
81
        // Close and Open the image
82
        cv::morphologyEx(image,image,cv::MORPH_CLOSE,element5);
83
        cv::morphologyEx(image,image,cv::MORPH_OPEN,element5);
84
 
85
    // Display the close/opened image
86
        cv::namedWindow("Closed and Opened Image");
87
        cv::imshow("Closed and Opened Image",image);
88
        cv::imwrite("binaryGroup.bmp",image);
89
 
90
        // Read input image
91
        image= cv::imread("../binary.bmp");
92
 
93
        // Open and Close the image
94
        cv::morphologyEx(image,image,cv::MORPH_OPEN,element5);
95
        cv::morphologyEx(image,image,cv::MORPH_CLOSE,element5);
96
 
97
    // Display the close/opened image
98
        cv::namedWindow("Opened and Closed Image");
99
        cv::imshow("Opened and Closed Image",image);
100
 
101
        cv::waitKey();
102
        return 0;
103
}
104