Skip to content

Commit 0739f28

Browse files
committed
Merge pull request opencv#9786 from LaurentBerger:Histo3d
2 parents 0460452 + 752f232 commit 0739f28

File tree

5 files changed

+244
-1
lines changed

5 files changed

+244
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
@snippet histo3D.cpp command_line_parser
25+
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,
27+
(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.
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.
32+
33+
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.
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](@ref CVMat_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
41+
42+
- Callback function
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
45+
46+
Results
47+
-------
48+
49+
Here is the result of the program with no argument and threshold equal to 50.
50+
51+
![](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.

modules/core/include/opencv2/core/mat.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ struct CV_EXPORTS MatStep
582582
An example demonstrating the serial out capabilities of cv::Mat
583583
*/
584584

585-
/** @brief n-dimensional dense array class
585+
/** @brief n-dimensional dense array class \anchor CVMat_Details
586586
587587
The class Mat represents an n-dimensional dense numerical single-channel or multi-channel array. It
588588
can be used to store real or complex-valued vectors and matrices, grayscale or color images, voxel
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
//! [get_cube_size]
35+
int planSize = h.histogram.step1(0);
36+
int cols = h.histogram.step1(1);
37+
int rows = planSize / cols;
38+
int plans = h.histogram.total() / planSize;
39+
h.fen3D->removeAllWidgets();
40+
h.nbWidget=0;
41+
if (h.nbWidget==0)
42+
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
43+
//! [get_cube_size]
44+
//! [get_cube_values]
45+
for (int k = 0; k < plans; k++)
46+
{
47+
for (int i = 0; i < rows; i++)
48+
{
49+
for (int j = 0; j < cols; j++)
50+
{
51+
double x = h.histogram.at<float>(k, i, j);
52+
if (x >= h.threshold)
53+
{
54+
double r=std::max(x/h.maxH,0.1);
55+
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));
56+
h.fen3D->showWidget(format("I3d%d", h.nbWidget++), s);
57+
}
58+
}
59+
}
60+
}
61+
//! [get_cube_values]
62+
h.status = false;
63+
}
64+
//! [viz_keyboard_callback]
65+
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
66+
{
67+
Histo3DData *x=(Histo3DData *)t;
68+
if (w.action)
69+
cout << "you pressed "<< w.symbol<< " in viz window "<<x->fen3D->getWindowName()<<"\n";
70+
x->code= w.code;
71+
switch (w.code) {
72+
case '/':
73+
x->status=true;
74+
x->threshold *= 0.9;
75+
break;
76+
case '*':
77+
x->status = true;
78+
x->threshold *= 1.1;
79+
break;
80+
}
81+
if (x->status)
82+
{
83+
cout << x->threshold << "\n";
84+
DrawHistogram3D(*x);
85+
}
86+
}
87+
//! [viz_keyboard_callback]
88+
89+
90+
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int defaultSlider, int *sliderVal, void(*f)(int, void *), void *r)
91+
{
92+
createTrackbar(sliderName, windowName, sliderVal, 1, f, r);
93+
setTrackbarMin(sliderName, windowName, sliderMin);
94+
setTrackbarMax(sliderName, windowName, sliderMax);
95+
setTrackbarPos(sliderName, windowName, defaultSlider);
96+
}
97+
98+
99+
void UpdateThreshold(int , void * r)
100+
{
101+
Histo3DData *h = (Histo3DData *)r;
102+
h->status=true;
103+
h->threshold = h->seuil/1000000.0;
104+
cout<<"Widget : "<<h->nbWidget<<","<< h->threshold<<"\n";
105+
}
106+
107+
int main (int argc,char **argv)
108+
{
109+
//! [command_line_parser]
110+
CommandLineParser parser(argc, argv, keys);
111+
112+
if (parser.has("help"))
113+
{
114+
parser.printMessage();
115+
return 0;
116+
}
117+
String nomFic = parser.get<String>(0);
118+
Mat img;
119+
if (nomFic.length() != 0)
120+
{
121+
img = imread(nomFic, IMREAD_COLOR);
122+
if (img.empty())
123+
{
124+
cout << "Image does not exist!";
125+
return 0;
126+
}
127+
}
128+
//! [command_line_parser]
129+
//! [synthetic_image]
130+
else
131+
{
132+
img = Mat(512,512,CV_8UC3);
133+
parser.printMessage();
134+
RNG r;
135+
r.fill(img(Rect(0, 0, 256, 256)), RNG::NORMAL, Vec3b(60, 40, 50), Vec3b(10, 5, 20));
136+
r.fill(img(Rect(256, 0, 256, 256)), RNG::NORMAL, Vec3b(160, 10, 50), Vec3b(20, 5, 10));
137+
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
138+
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
139+
}
140+
//! [synthetic_image]
141+
//! [calchist_for_histo3d]
142+
Histo3DData h;
143+
h.status=true;
144+
h.seuil=90;
145+
h.threshold= h.seuil/1000000.0;
146+
float hRange[] = { 0, 256 };
147+
const float* etendu[] = { hRange, hRange,hRange };
148+
int hBins = 32;
149+
int histSize[] = { hBins, hBins , hBins };
150+
int channel[] = { 2, 1,0 };
151+
calcHist(&img, 1, channel, Mat(), h.histogram, 3, histSize, etendu, true, false);
152+
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, NORM_MINMAX, -1, Mat());
153+
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
154+
//! [calchist_for_histo3d]
155+
//! [slide_bar_for_thresh]
156+
namedWindow("Image");
157+
imshow("Image",img);
158+
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
159+
waitKey(30);
160+
//! [slide_bar_for_thresh]
161+
//! [manage_viz_imshow_window]
162+
h.fen3D = new viz::Viz3d("3D Histogram");
163+
h.nbWidget=0;
164+
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
165+
DrawHistogram3D(h);
166+
while (h.code!=27)
167+
{
168+
h.fen3D->spinOnce(1);
169+
if (h.status)
170+
DrawHistogram3D(h);
171+
if (h.code!=27)
172+
h.code= waitKey(30);
173+
}
174+
//! [manage_viz_imshow_window]
175+
return 0;
176+
}
177+
#else
178+
179+
int main(int argc, char **argv)
180+
{
181+
cout << " you need VIZ module\n";
182+
return 0;
183+
}
184+
#endif

0 commit comments

Comments
 (0)