OpenCV  4.10.0
Open Source Computer Vision
Loading...
Searching...
No Matches
Creating a 3D histogram

Prev Tutorial: Creating Widgets

Goal

In this tutorial you will learn how to

  • Create your own callback keyboard function for viz window.
  • Show your 3D histogram in a viz window.

Code

You can download the code from here.

#include <opencv2/core.hpp>
#include <iostream>
using namespace std;
using namespace cv;
#ifdef HAVE_OPENCV_VIZ
#include <opencv2/viz.hpp>
const String keys =
"{Aide h usage ? help | | print this message }"
"{@arg1 | | Full path to color imag (3 channels)}"
;
struct Histo3DData {
Mat histogram;
int seuil;
double threshold;
int nbWidget;
bool status;
double maxH;
int code;
};
void DrawHistogram3D(Histo3DData &);
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int valeurDefaut, int *sliderVal, void(*f)(int, void *), void *r);
void UpdateThreshold(int , void * r);
void KeyboardViz3d(const viz::KeyboardEvent &w, void *t);
void DrawHistogram3D(Histo3DData &h)
{
int planSize = (int)h.histogram.step1(0);
int cols = (int)h.histogram.step1(1);
int rows = (int)planSize / cols;
int plans = (int)h.histogram.total() / planSize;
h.fen3D->removeAllWidgets();
h.nbWidget=0;
if (h.nbWidget==0)
h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));
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);
}
}
}
}
h.status = false;
}
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);
}
}
void AddSlidebar(String sliderName, String windowName, int sliderMin, int sliderMax, int defaultSlider, int *sliderVal, void(*f)(int, void *), void *r)
{
createTrackbar(sliderName, windowName, sliderVal, 1, f, r);
setTrackbarMin(sliderName, windowName, sliderMin);
setTrackbarMax(sliderName, windowName, sliderMax);
setTrackbarPos(sliderName, windowName, defaultSlider);
}
void UpdateThreshold(int , void * r)
{
Histo3DData *h = (Histo3DData *)r;
h->status=true;
h->threshold = h->seuil/1000000.0;
cout<<"Widget : "<<h->nbWidget<<","<< h->threshold<<"\n";
}
int main (int argc,char **argv)
{
CommandLineParser parser(argc, argv, keys);
if (parser.has("help"))
{
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;
}
}
else
{
img = Mat(512,512,CV_8UC3);
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));
}
Histo3DData h;
h.status=true;
h.seuil=90;
h.threshold= h.seuil/1000000.0;
float hRange[] = { 0, 256 };
const float* etendu[] = { hRange, hRange,hRange };
int hBins = 32;
int histSize[] = { hBins, hBins , hBins };
int channel[] = { 2, 1,0 };
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());
minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);
namedWindow("Image");
imshow("Image",img);
AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
waitKey(30);
h.fen3D = makePtr<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);
}
return 0;
}
#else
int main(int argc, char **argv)
{
cout << " you need VIZ module\n";
return 0;
}
#endif
Designed for command line parsing.
Definition utility.hpp:820
n-dimensional dense array class
Definition mat.hpp:812
Template class for 3D points specified by its coordinates x, y and z.
Definition types.hpp:255
Random Number Generator.
Definition core.hpp:2889
void fill(InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false)
Fills arrays with random numbers.
Template class for 2D rectangles.
Definition types.hpp:444
Template class for short numerical vectors, a partial case of Matx.
Definition matx.hpp:369
This class represents color in BGR order.
Definition types.hpp:64
This class represents a keyboard event.
Definition types.hpp:288
String symbol
Definition types.hpp:303
Action action
Definition types.hpp:302
unsigned char code
Definition types.hpp:304
Compound widgets.
Definition widgets.hpp:514
This 3D Widget defines a cube.
Definition widgets.hpp:373
void minMaxIdx(InputArray src, double *minVal, double *maxVal=0, int *minIdx=0, int *maxIdx=0, InputArray mask=noArray())
Finds the global minimum and maximum in an array.
void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
Normalizes the norm or value range of an array.
std::string String
Definition cvstd.hpp:151
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
#define CV_8UC3
Definition interface.h:90
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void setTrackbarPos(const String &trackbarname, const String &winname, int pos)
Sets the trackbar position.
void setTrackbarMax(const String &trackbarname, const String &winname, int maxval)
Sets the trackbar maximum position.
void setTrackbarMin(const String &trackbarname, const String &winname, int minval)
Sets the trackbar minimum position.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void calcHist(const Mat *images, int nimages, const int *channels, InputArray mask, OutputArray hist, int dims, const int *histSize, const float **ranges, bool uniform=true, bool accumulate=false)
Calculates a histogram of a set of arrays.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102
STL namespace.

Explanation

Here is the general structure of the program:

  • You can give full path to an image in command line

    CommandLineParser parser(argc, argv, keys);
    if (parser.has("help"))
    {
    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;
    }
    }

    or without path, a synthetic image is generated with pixel values are a gaussian distribution cv::RNG::fill 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.

    else
    {
    img = Mat(512,512,CV_8UC3);
    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));
    }

    Image tridimensional histogram is calculated using opencv cv::calcHist and cv::normalize between 0 and 100.

    Histo3DData h;
    h.status=true;
    h.seuil=90;
    h.threshold= h.seuil/1000000.0;
    float hRange[] = { 0, 256 };
    const float* etendu[] = { hRange, hRange,hRange };
    int hBins = 32;
    int histSize[] = { hBins, hBins , hBins };
    int channel[] = { 2, 1,0 };
    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());
    minMaxIdx(h.histogram,NULL,&h.maxH,NULL,NULL);

    channel are 2, 1 and 0 to synchronise color with Viz axis color in objetc cv::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.

    namedWindow("Image");
    imshow("Image",img);
    AddSlidebar("threshold","Image",0,100,h.seuil,&h.seuil, UpdateThreshold,&h);
    waitKey(30);

    We are ready to open a viz window with a callback function to capture keyboard event in viz window. Using cv::viz::Viz3d::spinOnce enable keyboard event to be capture in cv::imshow window too.

    h.fen3D = makePtr<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);
    }

    The function DrawHistogram3D processes histogram Mat to display it in a Viz window. Number of plan, row and column in three dimensional Mat can be found using this code :

    int planSize = (int)h.histogram.step1(0);
    int cols = (int)h.histogram.step1(1);
    int rows = (int)planSize / cols;
    int plans = (int)h.histogram.total() / planSize;
    h.fen3D->removeAllWidgets();
    h.nbWidget=0;
    if (h.nbWidget==0)
    h.fen3D->showWidget("Axis", viz::WCoordinateSystem(10));

    To get histogram value at a specific location we use 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.

    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);
    }
    }
    }
    }
  • Callback function Principle are as mouse callback function. Key code pressed is in field code of class cv::viz::KeyboardEvent.
    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);
    }
    }

Results

Here is the result of the program with no argument and threshold equal to 50.