|
| 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