Subversion Repositories OpenCV2-Cookbook

Rev

View as "text/plain" | Blame | Last modification | View Log | RSS feed

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

   This program is free software; permission is hereby granted to use, copy, modify,
   and distribute this source code, or portions thereof, for any purpose, without fee,
   subject to the restriction that the copyright notice may not be removed
   or altered from any source or altered source distribution.
   The software is released on an as-is basis and without any warranties of any kind.
   In particular, the software is not guaranteed to be fault-tolerant or free from failure.
   The author disclaims all warranties with regard to this software, any use,
   and any consequent failure, is purely the responsibility of the user.
 
   Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/


#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main()
{
        // Read input image
        cv::Mat image= cv::imread("../boldt.jpg",0);
        if (!image.data)
                return 0;

    // Display the image
        cv::namedWindow("Original Image");
        cv::imshow("Original Image",image);

        // Blur the image
        cv::Mat result;
        cv::GaussianBlur(image,result,cv::Size(5,5),1.5);

    // Display the blurred image
        cv::namedWindow("Gaussian filtered Image");
        cv::imshow("Gaussian filtered Image",result);

        // Get the gaussian kernel (1.5)
        cv::Mat gauss= cv::getGaussianKernel(9,1.5,CV_32F);
                 
        // Display kernel values
        cv::Mat_<float>::const_iterator it= gauss.begin<float>();  
        cv::Mat_<float>::const_iterator itend= gauss.end<float>();  
        std::cout << "[";
        for ( ; it!= itend; ++it) {
                std::cout << *it << " ";
        }
        std::cout << "]" << std::endl;

        // Get the gaussian kernel (0.5)
        gauss= cv::getGaussianKernel(9,0.5,CV_32F);
                 
        // Display kernel values
        it= gauss.begin<float>();  
        itend= gauss.end<float>();  
        std::cout << "[";
        for ( ; it!= itend; ++it) {
                std::cout << *it << " ";
        }
        std::cout << "]" << std::endl;

        // Get the gaussian kernel (2.5)
        gauss= cv::getGaussianKernel(9,2.5,CV_32F);
                 
        // Display kernel values
        it= gauss.begin<float>();  
        itend= gauss.end<float>();  
        std::cout << "[";
        for ( ; it!= itend; ++it) {
                std::cout << *it << " ";
        }
        std::cout << "]" << std::endl;

        // Get the Deriv kernel (2.5)
        cv::Mat kx, ky;
    cv::getDerivKernels(kx,ky,2,2,7,true);
                 
        // Display kernel values
        cv::Mat_<float>::const_iterator kit= kx.begin<float>();  
        cv::Mat_<float>::const_iterator kitend= kx.end<float>();  
        std::cout << "[";
        for ( ; kit!= kitend; ++kit) {
                std::cout << *kit << " ";
        }
        std::cout << "]" << std::endl;

        // Blur the image with a mean filter
        cv::blur(image,result,cv::Size(5,5));
               
    // Display the blurred image
        cv::namedWindow("Mean filtered Image");
        cv::imshow("Mean filtered Image",result);

        // Read input image with salt&pepper noise
        image= cv::imread("../salted.bmp",0);
        if (!image.data)
                return 0;

    // Display the S&P image
        cv::namedWindow("S&P Image");
        cv::imshow("S&P Image",image);

        // Blur the image with a mean filter
        cv::blur(image,result,cv::Size(5,5));
               
    // Display the blurred image
        cv::namedWindow("Mean filtered S&P Image");
        cv::imshow("Mean filtered S&P Image",result);

        // Applying a median filter
        cv::medianBlur(image,result,5);
               
    // Display the blurred image
        cv::namedWindow("Median filtered S&P Image");
        cv::imshow("Median filtered S&P Image",result);

        // Reduce by 4 the size of the image (the wrong way)
        image= cv::imread("../boldt.jpg",0);
        cv::Mat reduced(image.rows/2,image.cols/2,CV_8U);

        for (int i=0; i<reduced.rows; i++)
                for (int j=0; j<reduced.cols; j++)
                        reduced.at<uchar>(i,j)= image.at<uchar>(i*2,j*2);

    // Display the reduced image
        cv::namedWindow("Badly reduced Image");
        cv::imshow("Badly reduced Image",reduced);

        cv::waitKey();
        return 0;
}