Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GuiCV.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 
27 #ifdef WIN32
28 # define _CRT_SECURE_NO_WARNINGS
29 #endif
30 #include <Base/Common/BIASpragma.hh> // required for strcpy deprecated warnings
31 
32 #include "GuiCV.hh"
33 #include <sstream>
34 
35 #ifdef BIAS_HAVE_OPENCV
36 # include <cv.h>
37 # include <highgui.h>
38 #endif
39 
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 GuiCV::GuiCV(bool ShowMousePos) : GuiBase(ShowMousePos)
45 {
46  IplImage_ = NULL;
47 }
48 
49 GuiCV::GuiCV(const string& Title, bool ShowMousePos) : GuiBase(Title, ShowMousePos)
50 {
51  IplImage_ = NULL;
52 }
53 
54 GuiCV::GuiCV(const GuiCV& gui) : GuiBase(gui)
55 {
56  IplImage_ = NULL;
57 }
58 
60 {
61  BIASDOUT(D_GUI_DESTRUCT, "GuiCV destructor started");
62  if (WindowCreated_){
63  DestroyWindow();
64  }
65  BIASDOUT(D_GUI_DESTRUCT, "GuiCV destructor finished");
66 
67  if (IplImage_ != NULL)
68  {
69  delete IplImage_;
70  }
71 }
72 
73 void GuiCV::SetTitle(string const& Title)
74 {
75  if (WindowCreated_)
76  {
77  // OpenCV does not support changing the title,
78  // so create a new window.
79  DestroyWindow();
80  Title_ = Title;
82  }
83  else
84  {
85  Title_ = Title;
86  }
87 }
88 
90 {
91  cvDestroyWindow(Title_.c_str());
92  WindowCreated_ = false;
93  return 0;
94 }
95 
97 {
98  const int key = cvWaitKey(1);
99  return (char)key;
100 }
101 
103 {
104  int key = cvWaitKey();
105  return (char)key;
106 }
107 
108 void GuiCV::WaitForMouseButtonEvent(bool& Pressed, int& Button,
109  int& Xpos, int& Ypos)
110 {
111  while (WindowCreated_ && MouseButtonPressed_ == -1 &&
112  MouseButtonReleased_ == -1) {
113  cvWaitKey(100);
114  }
115  if (MouseButtonPressed_ != -1){
116  Pressed = true;
117  Button = MouseButtonPressed_;
118  Xpos = MouseButtonPressX_;
119  Ypos = MouseButtonPressY_;
121  MouseButtonPressed_ = -1;
122  BIASCDOUT(D_GUI_MOUSE_BUTTON, "pressed at ("<<Xpos<<", "<<Ypos<<")"<<endl);
123  } else {
124  Pressed = false;
125  Button = MouseButtonReleased_;
126  Xpos = MouseButtonReleaseX_;
127  Ypos = MouseButtonReleaseY_;
130  BIASCDOUT(D_GUI_MOUSE_BUTTON, "released at ("<<Xpos<<", "<<Ypos<<")"<<endl);
131  }
132  BIAS_MUTEX_UNLOCK(&MouseButtonMutex_);
133 }
134 
136 {
137  if (!WindowCreated_)
138  {
139  cvNamedWindow(Title_.c_str(), 0);
140  cvSetMouseCallback(Title_.c_str(), GuiCV::StaticMouseCallback, this);
141  WindowCreated_ = true;
142  }
143 
144  if (image.GetWidth() % 4 == 0)
145  {
146  CurrentImage_ = image;
147  }
148  else
149  {
150  int newWidth = (image.GetWidth() / 4) * 4 + 4;
151  int newHeight = image.GetHeight();
153  CurrentImage_.Init(newWidth, newHeight, image.GetChannelCount());
154  CurrentImage_.FillImageWithConstValue((unsigned char)0);
155  for (unsigned int y = 0; y < image.GetHeight(); y++)
156  {
157  for (unsigned int x = 0; x < image.GetWidth(); x++)
158  {
159  for (unsigned short c = 0; c < (unsigned short)image.GetChannelCount(); c++)
160  {
161  CurrentImage_.SetPixel(image.PixelValue(x, y, c), x, y, c );
162  }
163  }
164  }
165  }
166 
168  cvShowImage(Title_.c_str(), IplImage_);
169  cvWaitKey(2);
170 
171  return 0;
172 }
173 
174 void GuiCV::MouseCallback(int event, int x, int y, int flags)
175 {
176  switch (event)
177  {
178  case CV_EVENT_MOUSEMOVE:
179  MousePosX_ = x;
180  MousePosY_ = y;
181  break;
182  case CV_EVENT_LBUTTONDOWN:
184  MouseButtonPressX_ = x;
185  MouseButtonPressY_ = y;
186  break;
187  case CV_EVENT_RBUTTONDOWN:
189  MouseButtonPressX_ = x;
190  MouseButtonPressY_ = y;
191  break;
192  case CV_EVENT_MBUTTONDOWN:
194  MouseButtonPressX_ = x;
195  MouseButtonPressY_ = y;
196  break;
197  case CV_EVENT_LBUTTONUP:
201  break;
202  case CV_EVENT_RBUTTONUP:
206  break;
207  case CV_EVENT_MBUTTONUP:
211  break;
212  }
213 }
214 
215 void GuiCV::StaticMouseCallback(int event, int x, int y,
216  int flags, void* param)
217 {
218  static_cast<GuiCV*>(param)->MouseCallback(event, x, y, flags);
219 }
220 
221 int GuiCV::CreateIplImageShared(const BIAS::ImageBase* p_src, IplImage* &p_dest)
222 {
223  if (p_src==NULL){
224  BIASERR("ImageBase ptr p_src is NULL.");
225  return -1;
226  };
227  BIASASSERT(p_src!=NULL);
228  if (p_dest==NULL) {
229  p_dest=new IplImage;
230  //if (p_imgIpl==p_dest) p_imgIplDestroyable = true;
231  };
232  BIASASSERT(p_dest!=NULL);
233 
234 #ifdef BIAS_DEBUG
235  if (!p_src->IsInterleaved() || !((p_src->GetWidth() % 4) == 0) ) {
236  BIASERR("Only interleaved images with modulo 4 with are currently supported, sorry.");
237  BIASBREAK;
238  }
239 #endif // BIAS_DEBUG
240 
241 
242  // OpenCV supports only interleaved IPL_DATA_ORDER_PIXEL:
243  BIASASSERT( p_src->IsInterleaved() );
244 
245  // OpenCV supports only size modulo 4 image width
246  // to have pixel boundaries on 32bit aligned memory
247  // we would need stride, otherwise
248 #ifdef BIAS_DEBUG
249  if ( p_src->GetWidth() % 4 != 0 ) {
250  BIASERR("Image width is not multiple of 4 :"<< p_src->GetWidth()
251  <<"x"<< p_src->GetHeight());
252  }
253 
254 #endif
255  BIASASSERT( p_src->GetWidth() % 4 == 0 );
256 
257  // OK
258  // initialize only the header without data area:
259  CvSize imgSize;
260  imgSize.width = p_src->GetWidth();
261  imgSize.height = p_src->GetHeight();
262  // pixel depth in bits
263  int depth=0;
264  switch (p_src->GetStorageType()) {
266  depth=IPL_DEPTH_8S; break;
268  depth=IPL_DEPTH_8U; break;
270  depth=IPL_DEPTH_32F; break;
272  depth=IPL_DEPTH_64F; break;
274  depth=IPL_DEPTH_16S; break;
276  depth=IPL_DEPTH_16U; break;
278  depth=IPL_DEPTH_32S; break;
279 
281  // unsupported (?)
282  BIASERR("you try to wrap a BIAS unsigned int Image with OpenCV. This format may be unsupported by OpenCV! (JW)");
283  depth=IPL_DEPTH_32S|IPL_DEPTH_SIGN;
284  default:
285  BIASERR("unsupported Storagetype in BIAS image for BIAs->ipl image wrapper. GetStorageType="<<p_src->GetStorageType() );
286  BIASBREAK; // DBG
287  return -1;
288  }
289 
290  // fill header struct with useful values:
291  cvInitImageHeader( p_dest,
292  imgSize,
293  depth,
294  p_src->GetChannelCount(),
295  IPL_ORIGIN_TL, // origin top left
296  4 // alignment
297  );
298 
299  // share the data area: (pointer to aligned image data) do NOT copy
300  p_dest->imageData = (char*)(p_src->GetImageData());
301 
302 
303  // get the color model and channel seq. right:
304  switch (p_src->GetColorModel()){
306  // tested, works on Win32
307  strcpy( p_dest->colorModel, "RGB");
308  strcpy( p_dest->channelSeq, "RGB");
309  break;
311  // untested but should work because it's OpenCv native format.
312  strcpy( p_dest->colorModel, "RGB");
313  strcpy( p_dest->channelSeq, "BGR");
314  break;
316  // untested.
317  strcpy( p_dest->colorModel, "RGBA");
318  strcpy( p_dest->channelSeq, "BGRA");
319  break;
321  // untested.
322  strcpy( p_dest->colorModel, "RGBA");
323  strcpy( p_dest->channelSeq, "RGBA");
324  break;
326  // untested.
327  strcpy( p_dest->colorModel, "GREY");
328  strcpy( p_dest->channelSeq, "GREY");
329  break;
330  default:
331  BIASERR("unsupported color model for OpenCV wrapping. ChannelSeq may be wrong.");
332  return -2;
333  };
334 
335  return 0;
336 }
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
static pthread_mutex_t MouseButtonMutex_
Definition: GuiBase.hh:207
IplImage * IplImage_
Definition: GuiCV.hh:70
int CreateIplImageShared(const BIAS::ImageBase *p_src, IplImage *&p_dest)
Definition: GuiCV.cpp:221
virtual void SetTitle(std::string const &Title)
Definition: GuiCV.cpp:73
gray values, 1 channel
Definition: ImageBase.hh:130
static void StaticMouseCallback(int event, int x, int y, int flags, void *param)
Definition: GuiCV.cpp:215
bool IsInterleaved() const
Definition: ImageBase.hh:491
Gui is a simple windowing environment...
Definition: GuiBase.hh:75
(8bit) signed char image storage type
Definition: ImageBase.hh:113
virtual int DestroyWindow()
stops the main loop and closes the window
Definition: GuiCV.cpp:89
float image storage type
Definition: ImageBase.hh:118
virtual char WaitForKeyEvent()
waits until a key is pressed and returns it tested
Definition: GuiCV.cpp:102
Gui based on OpenCV is a simple windowing environment...
Definition: GuiCV.hh:53
GuiCV(bool ShowMousePos=true)
Definition: GuiCV.cpp:44
double image storage type
Definition: ImageBase.hh:119
int MouseButtonReleaseY_
Definition: GuiBase.hh:196
bool WindowCreated_
Definition: GuiBase.hh:180
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int MouseButtonReleaseX_
Definition: GuiBase.hh:195
StorageType PixelValue(const unsigned int x, const unsigned int y, const unsigned short int channel=0) const
Returns value of pixel at specific position, using specific channel as offset.
Definition: Image.hh:91
(16bit) signed integer image storage type
Definition: ImageBase.hh:115
color values, 3 channels, order: blue,green,red
Definition: ImageBase.hh:132
const void * GetImageData() const
Definition: ImageBase.hh:280
int MousePosX_
Definition: GuiBase.hh:189
int MouseButtonPressX_
Definition: GuiBase.hh:192
std::string Title_
Definition: GuiBase.hh:201
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
int MouseButtonPressed_
Definition: GuiBase.hh:191
void MouseCallback(int event, int x, int y, int flags)
Definition: GuiCV.cpp:174
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void FillImageWithConstValue(StorageType Value)
fill grey images
Definition: Image.cpp:456
void SetPixel(const StorageType &value, const unsigned int &x, const unsigned int &y, const unsigned short int channel=0)
Set the value of a given pixel (x,y) in channel to value.
Definition: Image.hh:171
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
RGBA, 4 channels, order: red,green,blue,alpha.
Definition: ImageBase.hh:141
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
Image< unsigned char > CurrentImage_
Definition: GuiCV.hh:71
(32bit) signed integer image storage type
Definition: ImageBase.hh:117
virtual int ShowConvertedImage_(Image< unsigned char > &image)
Does the real update of the image shown in window. Must be overloaded.
Definition: GuiCV.cpp:135
virtual ~GuiCV()
Definition: GuiCV.cpp:59
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int MousePosY_
Definition: GuiBase.hh:190
int MouseButtonPressY_
Definition: GuiBase.hh:193
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
virtual char CheckForKeyEvent()
checks if any key is pressed and returns it returns -1 if no key is pressed
Definition: GuiCV.cpp:96
int MouseButtonReleased_
Definition: GuiBase.hh:194
BGRA color values, 4 channels, order: blue,green,red,alpha.
Definition: ImageBase.hh:150
(32bit) unsigned integer image storage type
Definition: ImageBase.hh:116
virtual void WaitForMouseButtonEvent(bool &Pressed, int &Button, int &Xpos, int &Ypos)
waits until any mouse button is pressed or released
Definition: GuiCV.cpp:108