Subversion Repositories OpenCV2-Cookbook

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 PointedEar 1
/*------------------------------------------------------------------------------------------*\
2
   This file contains material supporting chapter 4 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 <vector>
20
using namespace std;
21
 
5 PointedEar 22
#include <opencv2/core/core.hpp>
23
#include <opencv2/highgui/highgui.hpp>
24
#include <opencv2/imgproc/imgproc.hpp>
25
#include <opencv2/video/tracking.hpp>
3 PointedEar 26
 
27
#include "objectFinder.h"
28
#include "colorhistogram.h"
29
 
30
int main()
31
{
32
        // Read reference image
33
        cv::Mat image= cv::imread("../baboon1.jpg");
34
        if (!image.data)
35
                return 0;
36
 
37
        // Define ROI
38
        cv::Mat imageROI= image(cv::Rect(110,260,35,40));
39
        cv::rectangle(image, cv::Rect(110,260,35,40),cv::Scalar(0,0,255));
40
 
41
        // Display image
42
        cv::namedWindow("Image");
43
        cv::imshow("Image",image);
44
 
45
        // Get the Hue histogram
46
        int minSat=65;
47
        ColorHistogram hc;
5 PointedEar 48
        cv::MatND colorhist= hc.getHueHistogram(imageROI);
3 PointedEar 49
 
50
        ObjectFinder finder;
51
        finder.setHistogram(colorhist);
52
        finder.setThreshold(0.2f);
53
 
54
        // Convert to HSV space
55
        cv::Mat hsv;
56
        cv::cvtColor(image, hsv, CV_BGR2HSV);
57
 
58
        // Split the image
59
        vector<cv::Mat> v;
60
        cv::split(hsv,v);
61
 
62
        // Eliminate pixels with low saturation
63
        cv::threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
64
        cv::namedWindow("Saturation");
65
        cv::imshow("Saturation",v[1]);
66
 
67
        // Get back-projection of hue histogram
68
        int ch[1]={0};
69
        cv::Mat result= finder.find(hsv,0.0f,180.0f,ch,1);
70
 
71
        cv::namedWindow("Result Hue");
72
        cv::imshow("Result Hue",result);
73
 
74
        cv::bitwise_and(result,v[1],result);
75
        cv::namedWindow("Result Hue and");
76
        cv::imshow("Result Hue and",result);
77
 
78
        // Second image
79
        image= cv::imread("../baboon3.jpg");
80
 
81
        // Display image
82
        cv::namedWindow("Image 2");
83
        cv::imshow("Image 2",image);
84
 
85
        // Convert to HSV space
86
        cv::cvtColor(image, hsv, CV_BGR2HSV);
87
 
88
        // Split the image
89
        cv::split(hsv,v);
90
 
91
        // Eliminate pixels with low saturation
92
        cv::threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
93
        cv::namedWindow("Saturation");
94
        cv::imshow("Saturation",v[1]);
95
 
96
        // Get back-projection of hue histogram
97
        result= finder.find(hsv,0.0f,180.0f,ch,1);
98
 
99
        cv::namedWindow("Result Hue");
100
        cv::imshow("Result Hue",result);
101
 
102
        // Eliminate low stauration pixels
103
        cv::bitwise_and(result,v[1],result);
104
        cv::namedWindow("Result Hue and");
105
        cv::imshow("Result Hue and",result);
106
 
107
        // Get back-projection of hue histogram
108
        finder.setThreshold(-1.0f);
109
        result= finder.find(hsv,0.0f,180.0f,ch,1);
110
        cv::bitwise_and(result,v[1],result);
111
        cv::namedWindow("Result Hue and raw");
112
        cv::imshow("Result Hue and raw",result);
113
 
114
        cv::Rect rect(110,260,35,40);
115
        cv::rectangle(image, rect, cv::Scalar(0,0,255));
116
 
117
        cv::TermCriteria criteria(cv::TermCriteria::MAX_ITER,10,0.01);
118
        cout << "meanshift= " << cv::meanShift(result,rect,criteria) << endl;
119
 
120
        cv::rectangle(image, rect, cv::Scalar(0,255,0));
121
 
122
        // Display image
123
        cv::namedWindow("Image 2 result");
124
        cv::imshow("Image 2 result",image);
125
 
126
        cv::waitKey();
127
        return 0;
5 PointedEar 128
}