mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Use @snippet
This commit is contained in:
parent
421c5dee12
commit
0a19b07055
@ -21,133 +21,27 @@ Explanation
|
|||||||
Here is the general structure of the program:
|
Here is the general structure of the program:
|
||||||
|
|
||||||
- You can give full path to an image in command line
|
- You can give full path to an image in command line
|
||||||
@code{.cpp}
|
@snippet histo3D.cpp command_line_parser
|
||||||
CommandLineParser parser(argc, argv, keys);
|
|
||||||
|
|
||||||
if (parser.has("help"))
|
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,
|
||||||
{
|
|
||||||
parser.printMessage();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
String nomFic = parser.get<String>(0);
|
|
||||||
Mat img;
|
|
||||||
if (nomFic.length() != 0)
|
|
||||||
{
|
|
||||||
img = imread(nomFic, IMREAD_COLOR);
|
|
||||||
if (img.empty())
|
|
||||||
{
|
|
||||||
cout << "Image does not exist!";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@endcode
|
|
||||||
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,
|
|
||||||
(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.
|
(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.
|
||||||
@code{.cpp}
|
@snippet histo3D.cpp synthetic_image
|
||||||
else
|
Image tridimensional histogram is calculated using opencv @ref cv::calcHist and @ref cv::normalize between 0 and 100.
|
||||||
{
|
@snippet histo3D.cpp calchist_for_histo3d
|
||||||
img = Mat(512,512,CV_8UC3);
|
channel are 2, 1 and 0 to synchronise color with Viz axis color in objetc cv::viz::WCoordinateSystem.
|
||||||
parser.printMessage();
|
|
||||||
RNG r;
|
|
||||||
r.fill(img(Rect(0, 0, 256, 256)), RNG::NORMAL, Vec3b(60, 40, 50), Vec3b(10, 5, 20));
|
|
||||||
r.fill(img(Rect(256, 0, 256, 256)), RNG::NORMAL, Vec3b(160, 10, 50), Vec3b(20, 5, 10));
|
|
||||||
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
|
|
||||||
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
|
|
||||||
}
|
|
||||||
@endcode
|
|
||||||
Image tridimensional histogram is calculated using opencv calcHist and normalize between 0 and 100.
|
|
||||||
@code{.cpp}
|
|
||||||
float hRange[] = { 0, 256 };
|
|
||||||
const float* etendu[] = { hRange, hRange,hRange };
|
|
||||||
int hBins = 32;
|
|
||||||
int tailleHist[] = { hBins, hBins , hBins };
|
|
||||||
int canaux[] = { 2, 1,0 };
|
|
||||||
calcHist(&img, 1, canaux, Mat(), h.histogram, 3, tailleHist, etendu, true, false);
|
|
||||||
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, cv::NormTypes::NORM_MINMAX, -1, cv::Mat());
|
|
||||||
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
|
|
||||||
@endcode
|
|
||||||
channel are 2, 1 and 0 to synchronise color with Viz axis color in objetc viz::WCoordinateSystem.
|
|
||||||
|
|
||||||
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.
|
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.
|
||||||
@code{.cpp}
|
@snippet histo3D.cpp slide_bar_for_thresh
|
||||||
namedWindow("Image");
|
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.
|
||||||
imshow("Image",img);
|
@snippet histo3D.cpp manage_viz_imshow_window
|
||||||
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
|
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 :
|
||||||
waitKey(30);
|
@snippet histo3D.cpp get_cube_size
|
||||||
@endcode
|
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.
|
||||||
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.
|
@snippet histo3D.cpp get_cube_values
|
||||||
@code{.cpp}
|
|
||||||
h.fen3D = new viz::Viz3d("3D Histogram");
|
|
||||||
h.nbWidget=0;
|
|
||||||
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
|
|
||||||
DrawHistogram3D(h);
|
|
||||||
while (h.code!=27)
|
|
||||||
{
|
|
||||||
h.fen3D->spinOnce(1);
|
|
||||||
if (h.status)
|
|
||||||
DrawHistogram3D(h);
|
|
||||||
if (h.code!=27)
|
|
||||||
h.code= waitKey(30);
|
|
||||||
}
|
|
||||||
@endcode
|
|
||||||
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 :
|
|
||||||
@code{.cpp}
|
|
||||||
int planSize = h.histogram.step1(0);
|
|
||||||
int cols = h.histogram.step1(1);
|
|
||||||
int rows = planSize / cols;
|
|
||||||
int plans = h.histogram.total() / planSize;
|
|
||||||
h.fen3D->removeAllWidgets();
|
|
||||||
h.nbWidget=0;
|
|
||||||
if (h.nbWidget==0)
|
|
||||||
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
|
|
||||||
@endcode
|
|
||||||
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.
|
|
||||||
@code{.cpp}
|
|
||||||
for (int k = 0; k < plans; k++)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < rows; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < cols; j++)
|
|
||||||
{
|
|
||||||
double x = h.histogram.at<float>(k, i, j);
|
|
||||||
if (x >= h.threshold)
|
|
||||||
{
|
|
||||||
double r=std::max(x/h.maxH,0.1);
|
|
||||||
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));
|
|
||||||
h.fen3D->showWidget(format("I3d%d", h.nbWidget++), s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@endcode
|
|
||||||
|
|
||||||
- Callback function
|
- Callback function
|
||||||
Principle are as mouse callback function. Key code pressed is in field code of class viz::KeyboardEvent.
|
Principle are as mouse callback function. Key code pressed is in field code of class @ref cv::viz::KeyboardEvent.
|
||||||
@code{.cpp}
|
@snippet histo3D.cpp viz_keyboard_callback
|
||||||
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
|
|
||||||
{
|
|
||||||
Histo3DData *x=(Histo3DData *)t;
|
|
||||||
if (w.action)
|
|
||||||
cout << "you pressed "<< w.symbol<< " in viz window "<<x->fen3D->getWindowName()<<"\n";
|
|
||||||
x->code= w.code;
|
|
||||||
switch (w.code) {
|
|
||||||
case '/':
|
|
||||||
x->status=true;
|
|
||||||
x->threshold *= 0.9;
|
|
||||||
break;
|
|
||||||
case '*':
|
|
||||||
x->status = true;
|
|
||||||
x->threshold *= 1.1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
if (x->status)
|
|
||||||
{
|
|
||||||
cout << x->threshold << "\n";
|
|
||||||
DrawHistogram3D(*x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@endcode
|
|
||||||
|
|
||||||
Results
|
Results
|
||||||
-------
|
-------
|
||||||
|
@ -31,6 +31,7 @@ void KeyboardViz3d(const viz::KeyboardEvent &w, void *t);
|
|||||||
|
|
||||||
void DrawHistogram3D(Histo3DData &h)
|
void DrawHistogram3D(Histo3DData &h)
|
||||||
{
|
{
|
||||||
|
//! [get_cube_size]
|
||||||
int planSize = h.histogram.step1(0);
|
int planSize = h.histogram.step1(0);
|
||||||
int cols = h.histogram.step1(1);
|
int cols = h.histogram.step1(1);
|
||||||
int rows = planSize / cols;
|
int rows = planSize / cols;
|
||||||
@ -39,6 +40,8 @@ void DrawHistogram3D(Histo3DData &h)
|
|||||||
h.nbWidget=0;
|
h.nbWidget=0;
|
||||||
if (h.nbWidget==0)
|
if (h.nbWidget==0)
|
||||||
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
|
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
|
||||||
|
//! [get_cube_size]
|
||||||
|
//! [get_cube_values]
|
||||||
for (int k = 0; k < plans; k++)
|
for (int k = 0; k < plans; k++)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < rows; i++)
|
for (int i = 0; i < rows; i++)
|
||||||
@ -55,9 +58,10 @@ void DrawHistogram3D(Histo3DData &h)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//! [get_cube_values]
|
||||||
h.status = false;
|
h.status = false;
|
||||||
}
|
}
|
||||||
|
//! [viz_keyboard_callback]
|
||||||
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
|
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
|
||||||
{
|
{
|
||||||
Histo3DData *x=(Histo3DData *)t;
|
Histo3DData *x=(Histo3DData *)t;
|
||||||
@ -80,6 +84,7 @@ void KeyboardViz3d(const viz::KeyboardEvent &w, void *t)
|
|||||||
DrawHistogram3D(*x);
|
DrawHistogram3D(*x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//! [viz_keyboard_callback]
|
||||||
|
|
||||||
|
|
||||||
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int defaultSlider, int *sliderVal, void(*f)(int, void *), void *r)
|
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)
|
|||||||
|
|
||||||
int main (int argc,char **argv)
|
int main (int argc,char **argv)
|
||||||
{
|
{
|
||||||
|
//! [command_line_parser]
|
||||||
CommandLineParser parser(argc, argv, keys);
|
CommandLineParser parser(argc, argv, keys);
|
||||||
|
|
||||||
if (parser.has("help"))
|
if (parser.has("help"))
|
||||||
@ -119,6 +125,8 @@ int main (int argc,char **argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//! [command_line_parser]
|
||||||
|
//! [synthetic_image]
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
img = Mat(512,512,CV_8UC3);
|
img = Mat(512,512,CV_8UC3);
|
||||||
@ -129,6 +137,8 @@ int main (int argc,char **argv)
|
|||||||
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
|
r.fill(img(Rect(0, 256, 256, 256)), RNG::NORMAL, Vec3b(90, 100, 50), Vec3b(10, 20, 20));
|
||||||
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
|
r.fill(img(Rect(256, 256, 256, 256)), RNG::NORMAL, Vec3b(100, 10, 150), Vec3b(10, 5, 40));
|
||||||
}
|
}
|
||||||
|
//! [synthetic_image]
|
||||||
|
//! [calchist_for_histo3d]
|
||||||
Histo3DData h;
|
Histo3DData h;
|
||||||
h.status=true;
|
h.status=true;
|
||||||
h.seuil=90;
|
h.seuil=90;
|
||||||
@ -141,11 +151,14 @@ int main (int argc,char **argv)
|
|||||||
calcHist(&img, 1, channel, Mat(), h.histogram, 3, histSize, etendu, true, false);
|
calcHist(&img, 1, channel, Mat(), h.histogram, 3, histSize, etendu, true, false);
|
||||||
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, NORM_MINMAX, -1, Mat());
|
normalize(h.histogram, h.histogram, 100.0/(img.total()), 0, NORM_MINMAX, -1, Mat());
|
||||||
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
|
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
|
||||||
|
//! [calchist_for_histo3d]
|
||||||
|
//! [slide_bar_for_thresh]
|
||||||
namedWindow("Image");
|
namedWindow("Image");
|
||||||
imshow("Image",img);
|
imshow("Image",img);
|
||||||
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
|
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
|
||||||
waitKey(30);
|
waitKey(30);
|
||||||
|
//! [slide_bar_for_thresh]
|
||||||
|
//! [manage_viz_imshow_window]
|
||||||
h.fen3D = new viz::Viz3d("3D Histogram");
|
h.fen3D = new viz::Viz3d("3D Histogram");
|
||||||
h.nbWidget=0;
|
h.nbWidget=0;
|
||||||
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
|
h.fen3D->registerKeyboardCallback(KeyboardViz3d,&h);
|
||||||
@ -158,6 +171,7 @@ int main (int argc,char **argv)
|
|||||||
if (h.code!=27)
|
if (h.code!=27)
|
||||||
h.code= waitKey(30);
|
h.code= waitKey(30);
|
||||||
}
|
}
|
||||||
|
//! [manage_viz_imshow_window]
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
Loading…
Reference in New Issue
Block a user