Skip to content

Commit 421c5de

Browse files
committed
3D histogram
1 parent bc7f649 commit 421c5de

File tree

4 files changed

+335
-0
lines changed

4 files changed

+335
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
Creating a 3D histogram {#tutorial_histo3D}
2+
================
3+
4+
Goal
5+
----
6+
7+
In this tutorial you will learn how to
8+
9+
- Create your own callback keyboard function for viz window.
10+
- Show your 3D histogram in a viz window.
11+
12+
Code
13+
----
14+
15+
You can download the code from [here ](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/viz/histo3D.cpp).
16+
@include samples/cpp/tutorial_code/viz/histo3D.cpp
17+
18+
Explanation
19+
-----------
20+
21+
Here is the general structure of the program:
22+
23+
- You can give full path to an image in command line
24+
@code{.cpp}
25+
CommandLineParser parser(argc, argv, keys);
26+
27+
if (parser.has("help"))
28+
{
29+
parser.printMessage();
30+
return 0;
31+
}
32+
String nomFic = parser.get<String>(0);
33+
Mat img;
34+
if (nomFic.length() != 0)
35+
{
36+
img = imread(nomFic, IMREAD_COLOR);
37+
if (img.empty())
38+
{
39+
cout << "Image does not exist!";
40+
return 0;
41+
}
42+
}
43+
@endcode
44+
or without path, a synthetic image is generated with pixel values are a gaussian distribution center(60+/-10,40+/-5,50+/-20) in first quadrant,
45+
(160+/-20,10+/-5,50+/-10) in second quadrant, (90+/-10,100+/-20,50+/-20) in third quadrant, (100+/-10,10+/-5,150+/-40) in last quadrant.
46+
@code{.cpp}
47+
else
48+
{
49+
img = Mat(512,512,CV_8UC3);
50+
parser.printMessage();
51+
RNG r;
52+
r.fill(img(Rect(0, 0, 256, 256)), RNG::NORMAL, Vec3b(60, 40, 50), Vec3b(10, 5, 20));
53+
r.fill(img(Rect(256, 0, 256, 256)), RNG::NORMAL, Vec3b(160, 10, 50), Vec3b(20, 5, 10));
54+
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
55+
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
56+
}
57+
@endcode
58+
Image tridimensional histogram is calculated using opencv calcHist and normalize between 0 and 100.
59+
@code{.cpp}
60+
float hRange[] = { 0, 256 };
61+
const float* etendu[] = { hRange, hRange,hRange };
62+
int hBins = 32;
63+
int tailleHist[] = { hBins, hBins , hBins };
64+
int canaux[] = { 2, 1,0 };
65+
calcHist(&img, 1, canaux, Mat(), h.histogram, 3, tailleHist, etendu, true, false);
66+
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, cv::NormTypes::NORM_MINMAX, -1, cv::Mat());
67+
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
68+
@endcode
69+
channel are 2, 1 and 0 to synchronise color with Viz axis color in objetc viz::WCoordinateSystem.
70+
71+
A slidebar is inserted in image window. Init slidebar value is 90, it means that only histogram cell greater than 9/100000.0 (23 pixels for an 512X512 pixels) will be display.
72+
@code{.cpp}
73+
namedWindow("Image");
74+
imshow("Image",img);
75+
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
76+
waitKey(30);
77+
@endcode
78+
We are ready to open a viz window with a callback function to capture keyboard event in viz window. Using Viz::spinonce enable keyboard event to be capture in imshow window too.
79+
@code{.cpp}
80+
h.fen3D = new viz::Viz3d("3D Histogram");
81+
h.nbWidget=0;
82+
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
83+
DrawHistogram3D(h);
84+
while (h.code!=27)
85+
{
86+
h.fen3D->spinOnce(1);
87+
if (h.status)
88+
DrawHistogram3D(h);
89+
if (h.code!=27)
90+
h.code= waitKey(30);
91+
}
92+
@endcode
93+
The function DrawHistogram3D processes histogram Mat to display it in a Viz window. Number of plan, row and column in three dimensional Mat ca be found using this code :
94+
@code{.cpp}
95+
int planSize = h.histogram.step1(0);
96+
int cols = h.histogram.step1(1);
97+
int rows = planSize / cols;
98+
int plans = h.histogram.total() / planSize;
99+
h.fen3D->removeAllWidgets();
100+
h.nbWidget=0;
101+
if (h.nbWidget==0)
102+
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
103+
@endcode
104+
To get histogram value at a specific location we use at method with three arguments k, i and j where k is plane number, i row number and j column number.
105+
@code{.cpp}
106+
for (int k = 0; k < plans; k++)
107+
{
108+
for (int i = 0; i < rows; i++)
109+
{
110+
for (int j = 0; j < cols; j++)
111+
{
112+
double x = h.histogram.at<float>(k, i, j);
113+
if (x >= h.threshold)
114+
{
115+
double r=std::max(x/h.maxH,0.1);
116+
viz::WCube s(Point3d(k - r / 2, i - r / 2, j - r / 2), Point3d(k + r / 2, i + r / 2, j + r / 2), false, viz::Color(j / double(plans) * 255, i / double(rows) * 255, k / double(cols) * 255));
117+
h.fen3D->showWidget(format("I3d%d", h.nbWidget++), s);
118+
}
119+
}
120+
}
121+
}
122+
@endcode
123+
124+
- Callback function
125+
Principle are as mouse callback function. Key code pressed is in field code of class viz::KeyboardEvent.
126+
@code{.cpp}
127+
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
128+
{
129+
Histo3DData *x=(Histo3DData *)t;
130+
if (w.action)
131+
cout << "you pressed "<< w.symbol<< " in viz window "<<x->fen3D->getWindowName()<<"\n";
132+
x->code= w.code;
133+
switch (w.code) {
134+
case '/':
135+
x->status=true;
136+
x->threshold *= 0.9;
137+
break;
138+
case '*':
139+
x->status = true;
140+
x->threshold *= 1.1;
141+
break;
142+
143+
}
144+
if (x->status)
145+
{
146+
cout << x->threshold << "\n";
147+
DrawHistogram3D(*x);
148+
}
149+
}
150+
@endcode
151+
152+
Results
153+
-------
154+
155+
Here is the result of the program with no argument and threshold equal to 50.
156+
157+
![](images/histo50.png)
839 KB
Loading

doc/tutorials/viz/table_of_content_viz.markdown

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ OpenCV Viz {#tutorial_table_of_content_viz}
3232
*Author:* Ozan Tonkal
3333

3434
You will learn how to create your own widgets.
35+
36+
- @subpage tutorial_histo3D
37+
38+
*Compatibility:* \> OpenCV 3.0.0
39+
40+
*Author:* Laurent Berger
41+
42+
You will learn how to plot a 3D histogram.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#include <opencv2/opencv.hpp>
2+
#include <iostream>
3+
4+
using namespace std;
5+
using namespace cv;
6+
7+
#ifdef HAVE_OPENCV_VIZ
8+
9+
const String keys =
10+
"{Aide h usage ? help | | print this message }"
11+
"{@arg1 | | Full path to color imag (3 channels)}"
12+
;
13+
14+
15+
struct Histo3DData {
16+
Mat histogram;
17+
int seuil;
18+
double threshold;
19+
Ptr<viz::Viz3d> fen3D;
20+
int nbWidget;
21+
bool status;
22+
double maxH;
23+
int code;
24+
};
25+
26+
void DrawHistogram3D(Histo3DData &);
27+
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int valeurDefaut, int *sliderVal, void(*f)(int, void *), void *r);
28+
void UpdateThreshold(int , void * r);
29+
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t);
30+
31+
32+
void DrawHistogram3D(Histo3DData &h)
33+
{
34+
int planSize = h.histogram.step1(0);
35+
int cols = h.histogram.step1(1);
36+
int rows = planSize / cols;
37+
int plans = h.histogram.total() / planSize;
38+
h.fen3D->removeAllWidgets();
39+
h.nbWidget=0;
40+
if (h.nbWidget==0)
41+
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
42+
for (int k = 0; k < plans; k++)
43+
{
44+
for (int i = 0; i < rows; i++)
45+
{
46+
for (int j = 0; j < cols; j++)
47+
{
48+
double x = h.histogram.at<float>(k, i, j);
49+
if (x >= h.threshold)
50+
{
51+
double r=std::max(x/h.maxH,0.1);
52+
viz::WCube s(Point3d(k - r / 2, i - r / 2, j - r / 2), Point3d(k + r / 2, i + r / 2, j + r / 2), false, viz::Color(j / double(plans) * 255, i / double(rows) * 255, k / double(cols) * 255));
53+
h.fen3D->showWidget(format("I3d%d", h.nbWidget++), s);
54+
}
55+
}
56+
}
57+
}
58+
h.status = false;
59+
}
60+
61+
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
62+
{
63+
Histo3DData *x=(Histo3DData *)t;
64+
if (w.action)
65+
cout << "you pressed "<< w.symbol<< " in viz window "<<x->fen3D->getWindowName()<<"\n";
66+
x->code= w.code;
67+
switch (w.code) {
68+
case '/':
69+
x->status=true;
70+
x->threshold *= 0.9;
71+
break;
72+
case '*':
73+
x->status = true;
74+
x->threshold *= 1.1;
75+
break;
76+
}
77+
if (x->status)
78+
{
79+
cout << x->threshold << "\n";
80+
DrawHistogram3D(*x);
81+
}
82+
}
83+
84+
85+
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int defaultSlider, int *sliderVal, void(*f)(int, void *), void *r)
86+
{
87+
createTrackbar(sliderName, windowName, sliderVal, 1, f, r);
88+
setTrackbarMin(sliderName, windowName, sliderMin);
89+
setTrackbarMax(sliderName, windowName, sliderMax);
90+
setTrackbarPos(sliderName, windowName, defaultSlider);
91+
}
92+
93+
94+
void UpdateThreshold(int , void * r)
95+
{
96+
Histo3DData *h = (Histo3DData *)r;
97+
h->status=true;
98+
h->threshold = h->seuil/1000000.0;
99+
cout<<"Widget : "<<h->nbWidget<<","<< h->threshold<<"\n";
100+
}
101+
102+
int main (int argc,char **argv)
103+
{
104+
CommandLineParser parser(argc, argv, keys);
105+
106+
if (parser.has("help"))
107+
{
108+
parser.printMessage();
109+
return 0;
110+
}
111+
String nomFic = parser.get<String>(0);
112+
Mat img;
113+
if (nomFic.length() != 0)
114+
{
115+
img = imread(nomFic, IMREAD_COLOR);
116+
if (img.empty())
117+
{
118+
cout << "Image does not exist!";
119+
return 0;
120+
}
121+
}
122+
else
123+
{
124+
img = Mat(512,512,CV_8UC3);
125+
parser.printMessage();
126+
RNG r;
127+
r.fill(img(Rect(0, 0, 256, 256)), RNG::NORMAL, Vec3b(60, 40, 50), Vec3b(10, 5, 20));
128+
r.fill(img(Rect(256, 0, 256, 256)), RNG::NORMAL, Vec3b(160, 10, 50), Vec3b(20, 5, 10));
129+
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
130+
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
131+
}
132+
Histo3DData h;
133+
h.status=true;
134+
h.seuil=90;
135+
h.threshold= h.seuil/1000000.0;
136+
float hRange[] = { 0, 256 };
137+
const float* etendu[] = { hRange, hRange,hRange };
138+
int hBins = 32;
139+
int histSize[] = { hBins, hBins , hBins };
140+
int channel[] = { 2, 1,0 };
141+
calcHist(&img, 1, channel, Mat(), h.histogram, 3, histSize, etendu, true, false);
142+
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, NORM_MINMAX, -1, Mat());
143+
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
144+
namedWindow("Image");
145+
imshow("Image",img);
146+
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
147+
waitKey(30);
148+
149+
h.fen3D = new viz::Viz3d("3D Histogram");
150+
h.nbWidget=0;
151+
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
152+
DrawHistogram3D(h);
153+
while (h.code!=27)
154+
{
155+
h.fen3D->spinOnce(1);
156+
if (h.status)
157+
DrawHistogram3D(h);
158+
if (h.code!=27)
159+
h.code= waitKey(30);
160+
}
161+
return 0;
162+
}
163+
#else
164+
165+
int main(int argc, char **argv)
166+
{
167+
cout << " you need VIZ module\n";
168+
return 0;
169+
}
170+
#endif

0 commit comments

Comments
 (0)