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 6 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 <iostream>
19
#include <opencv2/core/core.hpp>
20
#include <opencv2/imgproc/imgproc.hpp>
21
#include <opencv2/highgui/highgui.hpp>
22
 
23
int main()
24
{
25
        // Read input image
26
        cv::Mat image= cv::imread("../boldt.jpg",0);
27
        if (!image.data)
28
                return 0;
29
 
30
    // Display the image
31
        cv::namedWindow("Original Image");
32
        cv::imshow("Original Image",image);
33
 
34
        // Blur the image
35
        cv::Mat result;
36
        cv::GaussianBlur(image,result,cv::Size(5,5),1.5);
37
 
38
    // Display the blurred image
39
        cv::namedWindow("Gaussian filtered Image");
40
        cv::imshow("Gaussian filtered Image",result);
41
 
42
        // Get the gaussian kernel (1.5)
43
        cv::Mat gauss= cv::getGaussianKernel(9,1.5,CV_32F);
44
 
45
        // Display kernel values
46
        cv::Mat_<float>::const_iterator it= gauss.begin<float>();  
47
        cv::Mat_<float>::const_iterator itend= gauss.end<float>();  
48
        std::cout << "[";
49
        for ( ; it!= itend; ++it) {
50
                std::cout << *it << " ";
51
        }
52
        std::cout << "]" << std::endl;
53
 
54
        // Get the gaussian kernel (0.5)
55
        gauss= cv::getGaussianKernel(9,0.5,CV_32F);
56
 
57
        // Display kernel values
58
        it= gauss.begin<float>();  
59
        itend= gauss.end<float>();  
60
        std::cout << "[";
61
        for ( ; it!= itend; ++it) {
62
                std::cout << *it << " ";
63
        }
64
        std::cout << "]" << std::endl;
65
 
66
        // Get the gaussian kernel (2.5)
67
        gauss= cv::getGaussianKernel(9,2.5,CV_32F);
68
 
69
        // Display kernel values
70
        it= gauss.begin<float>();  
71
        itend= gauss.end<float>();  
72
        std::cout << "[";
73
        for ( ; it!= itend; ++it) {
74
                std::cout << *it << " ";
75
        }
76
        std::cout << "]" << std::endl;
77
 
78
        // Get the Deriv kernel (2.5)
79
        cv::Mat kx, ky;
80
    cv::getDerivKernels(kx,ky,2,2,7,true);
81
 
82
        // Display kernel values
83
        cv::Mat_<float>::const_iterator kit= kx.begin<float>();  
84
        cv::Mat_<float>::const_iterator kitend= kx.end<float>();  
85
        std::cout << "[";
86
        for ( ; kit!= kitend; ++kit) {
87
                std::cout << *kit << " ";
88
        }
89
        std::cout << "]" << std::endl;
90
 
91
        // Blur the image with a mean filter
92
        cv::blur(image,result,cv::Size(5,5));
93
 
94
    // Display the blurred image
95
        cv::namedWindow("Mean filtered Image");
96
        cv::imshow("Mean filtered Image",result);
97
 
98
        // Read input image with salt&pepper noise
99
        image= cv::imread("../salted.bmp",0);
100
        if (!image.data)
101
                return 0;
102
 
103
    // Display the S&P image
104
        cv::namedWindow("S&P Image");
105
        cv::imshow("S&P Image",image);
106
 
107
        // Blur the image with a mean filter
108
        cv::blur(image,result,cv::Size(5,5));
109
 
110
    // Display the blurred image
111
        cv::namedWindow("Mean filtered S&P Image");
112
        cv::imshow("Mean filtered S&P Image",result);
113
 
114
        // Applying a median filter
115
        cv::medianBlur(image,result,5);
116
 
117
    // Display the blurred image
118
        cv::namedWindow("Median filtered S&P Image");
119
        cv::imshow("Median filtered S&P Image",result);
120
 
121
        // Reduce by 4 the size of the image (the wrong way)
122
        image= cv::imread("../boldt.jpg",0);
123
        cv::Mat reduced(image.rows/2,image.cols/2,CV_8U);
124
 
125
        for (int i=0; i<reduced.rows; i++)
126
                for (int j=0; j<reduced.cols; j++)
127
                        reduced.at<uchar>(i,j)= image.at<uchar>(i*2,j*2);
128
 
129
    // Display the reduced image
130
        cv::namedWindow("Badly reduced Image");
131
        cv::imshow("Badly reduced Image",reduced);
132
 
133
        cv::waitKey();
134
        return 0;
135
}