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