Skip to content

Commit 0a19b07

Browse files
committed
1 parent 421c5de commit 0a19b07

File tree

2 files changed

+31
-123
lines changed

2 files changed

+31
-123
lines changed

doc/tutorials/viz/histo3D/histo3D.markdown

Lines changed: 15 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -21,133 +21,27 @@ Explanation
2121
Here is the general structure of the program:
2222

2323
- You can give full path to an image in command line
24-
@code{.cpp}
25-
CommandLineParser parser(argc, argv, keys);
24+
@snippet histo3D.cpp command_line_parser
2625

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,
26+
or without path, a synthetic image is generated with pixel values are a gaussian distribution @ref cv::RNG::fill center(60+/-10,40+/-5,50+/-20) in first quadrant,
4527
(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.
28+
@snippet histo3D.cpp synthetic_image
29+
Image tridimensional histogram is calculated using opencv @ref cv::calcHist and @ref cv::normalize between 0 and 100.
30+
@snippet histo3D.cpp calchist_for_histo3d
31+
channel are 2, 1 and 0 to synchronise color with Viz axis color in objetc cv::viz::WCoordinateSystem.
7032

7133
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
34+
@snippet histo3D.cpp slide_bar_for_thresh
35+
We are ready to open a viz window with a callback function to capture keyboard event in viz window. Using @ref cv::viz::Viz3d::spinOnce enable keyboard event to be capture in @ref cv::imshow window too.
36+
@snippet histo3D.cpp manage_viz_imshow_window
37+
The function DrawHistogram3D processes histogram Mat to display it in a Viz window. Number of plan, row and column in (three dimensional Mat)[http://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#details] can be found using this code :
38+
@snippet histo3D.cpp get_cube_size
39+
To get histogram value at a specific location we use @ref cv::Mat::at(int i0,int i1, int i2) method with three arguments k, i and j where k is plane number, i row number and j column number.
40+
@snippet histo3D.cpp get_cube_values
12341

12442
- 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
43+
Principle are as mouse callback function. Key code pressed is in field code of class @ref cv::viz::KeyboardEvent.
44+
@snippet histo3D.cpp viz_keyboard_callback
15145

15246
Results
15347
-------

samples/cpp/tutorial_code/viz/histo3D.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void KeyboardViz3d(const viz::KeyboardEvent &w, void *t);
3131

3232
void DrawHistogram3D(Histo3DData &h)
3333
{
34+
//! [get_cube_size]
3435
int planSize = h.histogram.step1(0);
3536
int cols = h.histogram.step1(1);
3637
int rows = planSize / cols;
@@ -39,6 +40,8 @@ void DrawHistogram3D(Histo3DData &h)
3940
h.nbWidget=0;
4041
if (h.nbWidget==0)
4142
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
43+
//! [get_cube_size]
44+
//! [get_cube_values]
4245
for (int k = 0; k < plans; k++)
4346
{
4447
for (int i = 0; i < rows; i++)
@@ -55,9 +58,10 @@ void DrawHistogram3D(Histo3DData &h)
5558
}
5659
}
5760
}
61+
//! [get_cube_values]
5862
h.status = false;
5963
}
60-
64+
//! [viz_keyboard_callback]
6165
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
6266
{
6367
Histo3DData *x=(Histo3DData *)t;
@@ -80,6 +84,7 @@ void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
8084
DrawHistogram3D(*x);
8185
}
8286
}
87+
//! [viz_keyboard_callback]
8388

8489

8590
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int defaultSlider, int *sliderVal, void(*f)(int, void *), void *r)
@@ -101,6 +106,7 @@ void UpdateThreshold(int , void * r)
101106

102107
int main (int argc,char **argv)
103108
{
109+
//! [command_line_parser]
104110
CommandLineParser parser(argc, argv, keys);
105111

106112
if (parser.has("help"))
@@ -119,6 +125,8 @@ int main (int argc,char **argv)
119125
return 0;
120126
}
121127
}
128+
//! [command_line_parser]
129+
//! [synthetic_image]
122130
else
123131
{
124132
img = Mat(512,512,CV_8UC3);
@@ -129,6 +137,8 @@ int main (int argc,char **argv)
129137
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
130138
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
131139
}
140+
//! [synthetic_image]
141+
//! [calchist_for_histo3d]
132142
Histo3DData h;
133143
h.status=true;
134144
h.seuil=90;
@@ -141,11 +151,14 @@ int main (int argc,char **argv)
141151
calcHist(&img, 1, channel, Mat(), h.histogram, 3, histSize, etendu, true, false);
142152
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, NORM_MINMAX, -1, Mat());
143153
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
154+
//! [calchist_for_histo3d]
155+
//! [slide_bar_for_thresh]
144156
namedWindow("Image");
145157
imshow("Image",img);
146158
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
147159
waitKey(30);
148-
160+
//! [slide_bar_for_thresh]
161+
//! [manage_viz_imshow_window]
149162
h.fen3D = new viz::Viz3d("3D Histogram");
150163
h.nbWidget=0;
151164
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
@@ -158,6 +171,7 @@ int main (int argc,char **argv)
158171
if (h.code!=27)
159172
h.code= waitKey(30);
160173
}
174+
//! [manage_viz_imshow_window]
161175
return 0;
162176
}
163177
#else

0 commit comments

Comments
 (0)