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