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
#include <iostream>
18
#include <iostream>
19
using namespace std;
19
using namespace std;
20
20
21
#include "cv.h"
21
#include <opencv/cv.h>
22
#include "highgui.h"
22
#include <opencv/highgui.h>
23
23
24
#include "histogram.h"
24
#include "histogram.h"
25
#include "objectFinder.h"
25
#include "objectFinder.h"
26
#include "colorhistogram.h"
26
#include "colorhistogram.h"
27
27
28
int main()
28
int main()
29
{
29
{
30
        // Read input image
30
        // Read input image
31
        cv::Mat image= cv::imread("../waves.jpg",0);
31
        cv::Mat image= cv::imread("../waves.jpg",0);
32
        if (!image.data)
32
        if (!image.data)
33
                return 0;
33
                return 0;
34
34
35
        // define image ROI
35
        // define image ROI
36
        cv::Mat imageROI;
36
        cv::Mat imageROI;
37
        imageROI= image(cv::Rect(360,55,40,50)); // Cloud region
37
        imageROI= image(cv::Rect(360,55,40,50)); // Cloud region
38
38
39
        // Display reference patch
39
        // Display reference patch
40
        cv::namedWindow("Reference");
40
        cv::namedWindow("Reference");
41
        cv::imshow("Reference",imageROI);
41
        cv::imshow("Reference",imageROI);
42
42
43
        // Find histogram of reference
43
        // Find histogram of reference
44
        Histogram1D h;
44
        Histogram1D h;
45
        cv::MatND hist= h.getHistogram(imageROI);
45
        cv::MatND hist= h.getHistogram(imageROI);
46
        cv::namedWindow("Reference Hist");
46
        cv::namedWindow("Reference Hist");
47
        cv::imshow("Reference Hist",h.getHistogramImage(imageROI));
47
        cv::imshow("Reference Hist",h.getHistogramImage(imageROI));
48
48
49
        // Create the objectfinder
49
        // Create the objectfinder
50
        ContentFinder finder;
50
        ObjectFinder finder;
51
        finder.setHistogram(hist);
51
        finder.setHistogram(hist);
52
52
53
        finder.setThreshold(-1.0f);
53
        finder.setThreshold(-1.0f);
54
54
55
        // Get back-projection
55
        // Get back-projection
56
        cv::Mat result1;
56
        cv::Mat result1;
57
        result1= finder.find(image);
57
        result1= finder.find(image);
58
58
59
        // Create negative image and display result
59
        // Create negative image and display result
60
        cv::Mat tmp;
60
        cv::Mat tmp;
61
        result1.convertTo(tmp,CV_8U,-1.0,255.0);
61
        result1.convertTo(tmp,CV_8U,-1.0,255.0);
62
        cv::namedWindow("Backprojection result");
62
        cv::namedWindow("Backprojection result");
63
        cv::imshow("Backprojection result",tmp);
63
        cv::imshow("Backprojection result",tmp);
64
64
65
        // Get binary back-projection
65
        // Get binary back-projection
66
        finder.setThreshold(0.12f);
66
        finder.setThreshold(0.12f);
67
        result1= finder.find(image);
67
        result1= finder.find(image);
68
68
69
        // Draw a rectangle around the reference area
69
        // Draw a rectangle around the reference area
70
        cv::rectangle(image,cv::Rect(360,55,40,50),cv::Scalar(0,0,0));
70
        cv::rectangle(image,cv::Rect(360,55,40,50),cv::Scalar(0,0,0));
71
71
72
        // Display image
72
        // Display image
73
        cv::namedWindow("Image");
73
        cv::namedWindow("Image");
74
        cv::imshow("Image",image);
74
        cv::imshow("Image",image);
75
75
76
        // Display result
76
        // Display result
77
        cv::namedWindow("Detection Result");
77
        cv::namedWindow("Detection Result");
78
        cv::imshow("Detection Result",result1);
78
        cv::imshow("Detection Result",result1);
79
79
80
        // Second test image
80
        // Second test image
81
        cv::Mat image2= cv::imread("../dog.jpg",0);
81
        cv::Mat image2= cv::imread("../dog.jpg",0);
82
        cv::Mat result2;
82
        cv::Mat result2;
83
        result2= finder.find(image2);
83
        result2= finder.find(image2);
84
84
85
        // Display result
85
        // Display result
86
        cv::namedWindow("Result (2)");
86
        cv::namedWindow("Result (2)");
87
        cv::imshow("Result (2)",result2);
87
        cv::imshow("Result (2)",result2);
88
88
89
        // Load color image
89
        // Load color image
90
        ColorHistogram hc;
90
        ColorHistogram hc;
91
        cv::Mat color= cv::imread("../waves.jpg");
91
        cv::Mat color= cv::imread("../waves.jpg");
92
        color= hc.colorReduce(color,32);
92
        color= hc.colorReduce(color,32);
93
        cv::namedWindow("Color Image");
93
        cv::namedWindow("Color Image");
94
        cv::imshow("Color Image",color);
94
        cv::imshow("Color Image",color);
95
95
96
        imageROI= color(cv::Rect(0,0,165,75)); // blue sky area
96
        imageROI= color(cv::Rect(0,0,165,75)); // blue sky area
97
97
98
        // Get 3D color histogram
98
        // Get 3D color histogram
99
        cv::MatND shist= hc.getHistogram(imageROI);
99
        cv::MatND shist= hc.getHistogram(imageROI);
100
        // Histograms with SparseMat does not work with OpenCV2.1
100
        // Histograms with SparseMat does not work with OpenCV2.1
101
        // cv::SparseMat shist= hc.getSparseHistogram(imageROI);
101
        // cv::SparseMat shist= hc.getSparseHistogram(imageROI);
102
102
103
        finder.setHistogram(shist);
103
        finder.setHistogram(shist);
104
        finder.setThreshold(0.05f);
104
        finder.setThreshold(0.05f);
105
105
106
        // Get back-projection of color histogram
106
        // Get back-projection of color histogram
107
        result1= finder.find(color);
107
        result1= finder.find(color);
108
108
109
        cv::namedWindow("Color Backproject Result");
109
        cv::namedWindow("Color Backproject Result");
110
        cv::imshow("Color Backproject Result",result1);
110
        cv::imshow("Color Backproject Result",result1);
111
111
112
        // Second color image
112
        // Second color image
113
        cv::Mat color2= cv::imread("../dog.jpg");
113
        cv::Mat color2= cv::imread("../dog.jpg");
114
        color2= hc.colorReduce(color2,32);
114
        color2= hc.colorReduce(color2,32);
115
115
116
        // Get back-projection of color histogram
116
        // Get back-projection of color histogram
117
        result2= finder.find(color2);
117
        result2= finder.find(color2);
118
118
119
        cv::namedWindow("Result color (2)");
119
        cv::namedWindow("Result color (2)");
120
        cv::imshow("Result color (2)",result2);
120
        cv::imshow("Result color (2)",result2);
121
121
122
        // Get ab color histogram
122
        // Get ab color histogram
123
        color= cv::imread("../waves.jpg");
123
        color= cv::imread("../waves.jpg");
124
        imageROI= color(cv::Rect(0,0,165,75)); // blue sky area
124
        imageROI= color(cv::Rect(0,0,165,75)); // blue sky area
125
        cv::MatND colorhist= hc.getabHistogram(imageROI);
125
        cv::MatND colorhist= hc.getabHistogram(imageROI);
126
126
127
        finder.setHistogram(colorhist);
127
        finder.setHistogram(colorhist);
128
        finder.setThreshold(0.05f);
128
        finder.setThreshold(0.05f);
129
129
130
        // Convert to Lab space
130
        // Convert to Lab space
131
        cv::Mat lab;
131
        cv::Mat lab;
132
        cv::cvtColor(color, lab, CV_BGR2Lab);
132
        cv::cvtColor(color, lab, CV_BGR2Lab);
133
133
134
        // Get back-projection of ab histogram
134
        // Get back-projection of ab histogram
135
        int ch[2]={1,2};
135
        int ch[2]={1,2};
136
        result1= finder.find(lab,-128.0f,127.0f,ch,2);
136
        result1= finder.find(lab,-128.0f,127.0f,ch,2);
137
137
138
        cv::namedWindow("Result ab (1)");
138
        cv::namedWindow("Result ab (1)");
139
        cv::imshow("Result ab (1)",result1);
139
        cv::imshow("Result ab (1)",result1);
140
140
141
        // Second color image
141
        // Second color image
142
        color2= cv::imread("../dog.jpg");
142
        color2= cv::imread("../dog.jpg");
143
143
144
        cv::namedWindow("Color Image (2)");
144
        cv::namedWindow("Color Image (2)");
145
        cv::imshow("Color Image (2)",color2);
145
        cv::imshow("Color Image (2)",color2);
146
146
147
        cv::cvtColor(color2, lab, CV_BGR2Lab);
147
        cv::cvtColor(color2, lab, CV_BGR2Lab);
148
148
149
        result2= finder.find(lab,-128.0f,127.0f,ch,2);
149
        result2= finder.find(lab,-128.0f,127.0f,ch,2);
150
150
151
        cv::namedWindow("Result ab (2)");
151
        cv::namedWindow("Result ab (2)");
152
        cv::imshow("Result ab (2)",result2);
152
        cv::imshow("Result ab (2)",result2);
153
153
154
        // Get Hue color histogram
154
        // Get Hue color histogram
155
        color= cv::imread("../waves.jpg");
155
        color= cv::imread("../waves.jpg");
156
        imageROI= color(cv::Rect(0,0,165,75)); // blue sky area
156
        imageROI= color(cv::Rect(0,0,165,75)); // blue sky area
157
        colorhist= hc.getHueHistogram(imageROI);
157
        colorhist= hc.getHueHistogram(imageROI);
158
158
159
        finder.setHistogram(colorhist);
159
        finder.setHistogram(colorhist);
160
        finder.setThreshold(0.3f);
160
        finder.setThreshold(0.3f);
161
161
162
        // Convert to HSV space
162
        // Convert to HSV space
163
        cv::Mat hsv;
163
        cv::Mat hsv;
164
        cv::cvtColor(color, hsv, CV_BGR2HSV);
164
        cv::cvtColor(color, hsv, CV_BGR2HSV);
165
165
166
        // Get back-projection of hue histogram
166
        // Get back-projection of hue histogram
167
        ch[0]=0;
167
        ch[0]=0;
168
        result1= finder.find(hsv,0.0f,180.0f,ch,1);
168
        result1= finder.find(hsv,0.0f,180.0f,ch,1);
169
169
170
        cv::namedWindow("Result Hue (1)");
170
        cv::namedWindow("Result Hue (1)");
171
        cv::imshow("Result Hue (1)",result1);
171
        cv::imshow("Result Hue (1)",result1);
172
172
173
        // Second color image
173
        // Second color image
174
        color2= cv::imread("../dog.jpg");
174
        color2= cv::imread("../dog.jpg");
175
175
176
        cv::cvtColor(color2, hsv, CV_BGR2HSV);
176
        cv::cvtColor(color2, hsv, CV_BGR2HSV);
177
177
178
        result2= finder.find(hsv,0.0f,180.0f,ch,1);
178
        result2= finder.find(hsv,0.0f,180.0f,ch,1);
179
179
180
        cv::namedWindow("Result Hue (2)");
180
        cv::namedWindow("Result Hue (2)");
181
        cv::imshow("Result Hue (2)",result2);
181
        cv::imshow("Result Hue (2)",result2);
182
182
183
        cv::waitKey();
183
        cv::waitKey();
184
        return 0;
184
        return 0;
185
}
185
}
186
186
187
 
187