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 4 of the cookbook:  
2
   This file contains material supporting chapter 4 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
#if !defined ICOMPARATOR
18
#if !defined ICOMPARATOR
19
#define ICOMPARATOR
19
#define ICOMPARATOR
20
20
21
#include <opencv2\core\core.hpp>
21
#include <opencv2/core/core.hpp>
22
#include <opencv2\imgproc\imgproc.hpp>
22
#include <opencv2/imgproc/imgproc.hpp>
23
#include "colorhistogram.h"
23
#include "colorhistogram.h"
24
24
25
class ImageComparator {
25
class ImageComparator {
26
26
27
  private:
27
  private:
28
28
29
        cv::Mat reference;
29
        cv::Mat reference;
30
        cv::Mat input;
30
        cv::Mat input;
31
        cv::MatND refH;
31
        cv::MatND refH;
32
        cv::MatND inputH;
32
        cv::MatND inputH;
33
33
34
        ColorHistogram hist;
34
        ColorHistogram hist;
35
        int div;
35
        int div;
36
36
37
  public:
37
  public:
38
38
39
        ImageComparator() : div(32) {
39
        ImageComparator() : div(32) {
40
40
41
        }
41
        }
42
42
43
        // Color reduction factor
43
        // Color reduction factor
44
        // The comparaison will be made on images with
44
        // The comparaison will be made on images with
45
        // color space reduced by this factor in each dimension
45
        // color space reduced by this factor in each dimension
46
        void setColorReduction( int factor) {
46
        void setColorReduction( int factor) {
47
47
48
                div= factor;
48
                div= factor;
49
        }
49
        }
50
50
51
        int getColorReduction() {
51
        int getColorReduction() {
52
52
53
                return div;
53
                return div;
54
        }
54
        }
55
55
56
        void setReferenceImage(const cv::Mat& image) {
56
        void setReferenceImage(const cv::Mat& image) {
57
57
58
                reference= hist.colorReduce(image,div);
58
                reference= hist.colorReduce(image,div);
59
                refH= hist.getHistogram(reference);
59
                refH= hist.getHistogram(reference);
60
        }
60
        }
61
61
62
        double compare(const cv::Mat& image) {
62
        double compare(const cv::Mat& image) {
63
63
64
                input= hist.colorReduce(image,div);
64
                input= hist.colorReduce(image,div);
65
                inputH= hist.getHistogram(input);
65
                inputH= hist.getHistogram(input);
66
66
67
                return cv::compareHist(refH,inputH,CV_COMP_INTERSECT);
67
                return cv::compareHist(refH,inputH,CV_COMP_INTERSECT);
68
        }
68
        }
69
};
69
};
70
70
71
71
72
#endif
72
#endif
73
 
73