CvTestbed.cpp
Go to the documentation of this file.
2 
4  cap=NULL;
5  running=false;
6  videocallback=NULL;
7  keycallback=NULL;
8  images.clear();
9 }
10 
12  for (size_t i=0; i<images.size(); i++) {
13  if (images[i].release_at_exit) {
14  cvReleaseImage(&(images[i].ipl));
15  }
16  }
17  images.clear();
18 }
19 
21  // TODO: Skip frames if we are too slow? Impossible using OpenCV?
22  /*
23  static bool semaphore=false;
24  if (semaphore) return;
25  semaphore = true;
26  */
27 
30  }
32 
33  //semaphore = false;
34 }
35 
37  running=true;
38  static bool pause = false;
39  while (running) {
40  if (cap) {
41  IplImage *frame = cap->captureImage();
42  if (frame) {
43  default_videocallback(frame);
44  if (wintitle.size() > 0) {
45  cvShowImage(wintitle.c_str(), frame);
46  }
47  }
48  }
49  int key;
50 #ifdef WIN32
51  if( (key = cvWaitKey(1)) >= 0 ) {
52 #else
53  if( (key = cvWaitKey(20)) >= 0 ) {
54 #endif
55  if (keycallback) {
56  key = keycallback(key);
57  }
58  // If 'keycallback' didn't handle key - Use default handling
59  // By default 'C' for calibration
60  if (key == 'C') {
61  if (cap) {
63  }
64  }
65  // By default '0'-'9' toggles visible images
66  else if ((key >= '0') && (key <= '9')) {
67  int index=key-'0';
68  ToggleImageVisible(index);
69  }
70  else if(key == 'p') {
71  pause = !pause;
72  }
73  else if (key > 0) {
74  running=false;
75  }
76  }
77  }
78 }
79 
81  for (size_t i=0; i<images.size(); i++) {
82  if (images[i].visible) {
83  cvShowImage(images[i].title.c_str(), images[i].ipl);
84  }
85  }
86 }
87 
89  static CvTestbed obj;
90  return obj;
91 }
92 
93 void CvTestbed::SetVideoCallback(void (*_videocallback)(IplImage *image)) {
94  videocallback=_videocallback;
95 }
96 
97 void CvTestbed::SetKeyCallback(int (*_keycallback)(int key)) {
98  keycallback=_keycallback;
99 }
100 
101 bool CvTestbed::StartVideo(Capture *_cap, const char *_wintitle) {
102  bool clean=false;
103  cap = _cap;
104  if (cap == NULL) {
105  CaptureFactory::CaptureDeviceVector vec = CaptureFactory::instance()->enumerateDevices();
106  if (vec.size() < 1) return false;
107  cap = CaptureFactory::instance()->createCapture(vec[0]);
108  if (!cap->start()) {
109  delete cap;
110  return false;
111  }
112  clean=true;
113  }
114  if (_wintitle) {
115  wintitle = _wintitle;
116  cvNamedWindow(_wintitle, 1);
117  }
118  WaitKeys(); // Call the main loop
119  if (clean) {
120  cap->stop();
121  delete cap;
122  }
123  return true;
124 }
125 
126 size_t CvTestbed::SetImage(const char *title, IplImage *ipl, bool release_at_exit /* =false */) {
127  size_t index = GetImageIndex(title);
128  if (index == -1) {
129  // If the title wasn't found create new
130  Image i(ipl, title, false, release_at_exit);
131  images.push_back(i);
132  return (images.size()-1);
133  }
134  // If the title was found replace the image
135  if (images[index].release_at_exit) {
136  cvReleaseImage(&(images[index].ipl));
137  }
138  images[index].ipl = ipl;
139  images[index].release_at_exit = release_at_exit;
140  return index;
141 }
142 
143 IplImage *CvTestbed::CreateImage(const char *title, CvSize size, int depth, int channels ) {
144  IplImage *ipl=cvCreateImage(size, depth, channels);
145  if (!ipl) return NULL;
146  SetImage(title, ipl, true);
147  return ipl;
148 }
149 
150 IplImage *CvTestbed::CreateImageWithProto(const char *title, IplImage *proto, int depth /* =0 */, int channels /* =0 */) {
151  if (depth == 0) depth = proto->depth;
152  if (channels == 0) channels = proto->nChannels;
153  IplImage *ipl= cvCreateImage(cvSize(proto->width, proto->height), depth, channels);
154  if (!ipl) return NULL;
155  ipl->origin = proto->origin;
156  SetImage(title, ipl, true);
157  return ipl;
158 }
159 
160 IplImage *CvTestbed::GetImage(size_t index) {
161  if (index < 0) return NULL;
162  if (index >= images.size()) return NULL;
163  return images[index].ipl;
164 }
165 
166 size_t CvTestbed::GetImageIndex(const char *title) {
167  std::string s(title);
168  for (size_t i=0; i<images.size(); i++) {
169  if (s.compare(images[i].title) == 0) {
170  return i;
171  }
172  }
173  return (size_t)-1;
174 }
175 
176 IplImage *CvTestbed::GetImage(const char *title) {
177  return GetImage(GetImageIndex(title));
178 }
179 
180 bool CvTestbed::ToggleImageVisible(size_t index, int flags) {
181  if (index >= images.size()) return false;
182  if (images[index].visible == false) {
183  images[index].visible=true;
184  cvNamedWindow(images[index].title.c_str(), flags);
185  return true;
186  }
187  else {
188  images[index].visible=false;
189  cvDestroyWindow(images[index].title.c_str());
190  return false;
191  }
192 }
Capture * cap
Definition: CvTestbed.h:68
Image structure to store the images internally.
Definition: CvTestbed.h:89
static CvTestbed & Instance()
The one and only instance of CvTestbed is accessed using CvTestbed::Instance()
Definition: CvTestbed.cpp:88
std::vector< Image > images
Vector of images stored internally.
Definition: CvTestbed.h:98
static void default_videocallback(IplImage *image)
Video callback called for every frame. This calls user-defined videocallback if one exists...
Definition: CvTestbed.cpp:20
virtual void stop()=0
Stops the camera capture.
virtual bool start()=0
Starts the camera capture.
bool running
Boolean indicating are we still running. We exit from the WaitKeys when this is false.
Definition: CvTestbed.h:78
XmlRpcServer s
void SetVideoCallback(void(*_videocallback)(IplImage *image))
Set the videocallback function that will be called for every frame.
Definition: CvTestbed.cpp:93
IplImage * CreateImage(const char *title, CvSize size, int depth, int channels)
Creates an image with given size, depth and channels and stores it with a given &#39;title&#39; (see CvTestbe...
Definition: CvTestbed.cpp:143
IplImage * CreateImageWithProto(const char *title, IplImage *proto, int depth=0, int channels=0)
Creates an image based on the given prototype and stores it with a given &#39;title&#39; (see CvTestbed::SetI...
Definition: CvTestbed.cpp:150
unsigned char * image
Definition: GlutViewer.cpp:155
ROSCPP_DECL std::string clean(const std::string &name)
IplImage * GetImage(size_t index)
Get a pointer for the stored image based on index number.
Definition: CvTestbed.cpp:160
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
std::string wintitle
The window title for the video view.
Definition: CvTestbed.h:85
bool StartVideo(Capture *_cap, const char *_wintitle=0)
Start video input from given capture device.
Definition: CvTestbed.cpp:101
size_t GetImageIndex(const char *title)
Get an index number of the stored image based on title.
Definition: CvTestbed.cpp:166
void SetKeyCallback(int(*_keycallback)(int key))
Sets the keyboard callback function that will be called when keyboard is pressed. ...
Definition: CvTestbed.cpp:97
virtual bool showSettingsDialog()=0
Show the settings dialog of the camera.
void(* videocallback)(IplImage *image)
Pointer for the user-defined videocallback.
Definition: CvTestbed.h:81
int(* keycallback)(int key)
Pointer for the user-defined KEYcallback.
Definition: CvTestbed.h:83
bool ToggleImageVisible(size_t index, int flags=1)
Toggle the visibility of the stored image.
Definition: CvTestbed.cpp:180
virtual IplImage * captureImage()=0
Capture one image from the camera.
Capture interface that plugins must implement.
Definition: Capture.h:46
size_t SetImage(const char *title, IplImage *ipl, bool release_at_exit=false)
Sets an existing IplImage to be stored with the given title.
Definition: CvTestbed.cpp:126
void WaitKeys()
WaitKeys contains the main loop.
Definition: CvTestbed.cpp:36
void ShowVisibleImages()
ShowVisibleImages is called from the videocallback. This shows the internally stored images which hav...
Definition: CvTestbed.cpp:80
CvTestbed()
Hidden constructor for Singleton.
Definition: CvTestbed.cpp:3
~CvTestbed()
Hidden destructor for Singleton.
Definition: CvTestbed.cpp:11
CvTestbed is a class for making quick OpenCV test applications
Definition: CvTestbed.h:66


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04