Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExamplePtGreyHiresColor.cpp

Example for a OpenCV Gui with pointgrey hires camera

Author
MIP
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** @example ExamplePtGreyHiresColor.cpp
@relates VideoSource_DCAM
@ingroup g_examples
@ingroup g_videosource
@brief Example for a OpenCV Gui with pointgrey hires camera
@author MIP
*/
#include <Image/Camera.hh>
#include <Base/Image/ImageConvert.hh>
#include <Base/ImageUtils/ImageDraw.hh>
#include <Base/Image/ImageIO.hh>
#include <VideoSource/VideoSource_DCAM.hh>
#include <Gui/GuiCV.hh>
#ifdef WIN32
# include<Base/Common/getopt_W32.h>
#else
# include <unistd.h>
# include <getopt.h>
#endif
using namespace BIAS;
using namespace std;
#define StorageType unsigned char
#include <Base/Debug/TimeMeasure.hh>
float rgain=-31.0/32.0, bgain=31.0/32.0;
float gain = 0.0;
unsigned savecounter=0;
void mainloop(VideoSource_DCAM *cam)
{
GuiCV CamWin;
bool done = false;
char c;
char title[100];
struct timeval now;
float fps;
int i;
long start=0, end;
bool color, pause, slowconversion=false, autosave=false;
ostringstream os;
TimeMeasure t1,t2;
sprintf(title,"MyWindow");
CamWin.SetTitle(title);
cam->InitImage(pic);
cout<<"Init image with: "<<pic.GetWidth()<<","<<pic.GetHeight()<<","<<pic.GetChannelCount()<<endl;
pic2.Init(pic.GetWidth(), pic.GetHeight(),3);
pause = false;
color = true;
/* //Set but not used
unsigned int line1start[2],line1end[2];
unsigned int line2start[2],line2end[2];
line1start[0] = 0; line1end[0] = pic2.GetWidth()-1;
line1start[1] = line1end[1] = 403;
// line1start[1] = line1end[1] = pic2.GetHeight()/2;
line2start[1] = 0; line2end[1] = pic2.GetHeight()-1;
line2start[0] = line2end[0] = 492;
// line2start[0] = line2end[0] = pic2.GetWidth()/2;
*/
i = 1;
bool save;
//pic2.SetDebugLevel(D_CONVERT);
while (!done){
save = false;
gettimeofday(&now, NULL);
end = start;
start = (now.tv_sec % 1000) * 1000 + (now.tv_usec / 1000);
fps = 1.0f / (float)(start-end) * 1000.0f;
c = CamWin.CheckForKeyEvent();
switch (c) {
case 'q':
case 27:
done = true;
break;
case 'g':
cam->AutoMode(DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_AUTO);
cerr << "\nauto gain on"<<endl;
break;
// case '1':
// cout <<endl<<"Gammamode: "<<cam->GetFeature(FEATURE_GAMMA)<<endl;
// cam->SetFeature(FEATURE_GAMMA, 128);
// break;
// case '2':
// cam->SetFeature(FEATURE_GAMMA, 129);
// break;
// case '3':
// cam->SetFeature(FEATURE_GAMMA, 130);
// break;
case '1':
cout <<endl<<"Shutter: "<<cam->GetShutter()<<endl;
// cam->SetShutter(0.01);
break;
case 'w':
cam->OnePushAuto(DC1394_FEATURE_WHITE_BALANCE);
cerr << "\nwhite balance"<<endl;
break;
case 'S':
case ' ':
save = true;
break;
case 's':
autosave=!autosave;
if (autosave)
cerr << "\nstarted saving sequence at frame "<<i<<" into frame"
<<setfill('0')<<setw(5)<<savecounter<<".mip"<<endl;
else
cerr << "\nstopped saving sequence at frame "<<i<<" into frame"
<<setfill('0')<<setw(5)<<savecounter<<".mip"<<endl;
break;
case 'p':
pause = !pause;
break;
case 'c':
color = !color;
if (color) cerr << "\nshowing color image" << endl;
else cerr << "\nshowing grey image" << endl;
break;
case 'G':
cam->AutoMode(DC1394_FEATURE_GAIN,DC1394_FEATURE_MODE_MANUAL);
cerr << "\nauto gain off"<<endl;
break;
case 'e':
slowconversion=!slowconversion;
if (slowconversion) CamWin.SetTitle("doing slow conversion");
else CamWin.SetTitle("doing fast conversion");
break;
case '7':
bgain-=1.0/32.0;
if (cam->SetWhiteBalance(rgain, bgain)!=0){
BIASERR("error setting white balance");}
cerr <<"\nblue gain = "<<bgain<<endl;
break;
case '8':
bgain+=1.0/32.0;
if (cam->SetWhiteBalance(rgain, bgain)!=0){
BIASERR("error setting white balance");}
cerr <<"\nblue gain = "<<bgain<<endl;
break;
case '4':
rgain-=1.0/32.0;
if (cam->SetWhiteBalance(rgain, bgain)!=0){
BIASERR("error setting white balance");}
cerr <<"\nred gain = "<<rgain<<endl;
break;
case '5':
rgain+=1.0/32.0;
if (cam->SetWhiteBalance(rgain, bgain)!=0){
BIASERR("error setting white balance");}
cerr <<"\nred gain = "<<rgain<<endl;
break;
case '[':
gain -= 1.0/32.0;
cam->SetGain(gain);
cout<<"Overall gain :"<<gain<<endl;
break;
case ']':
gain += 1.0/32.0;
cam->SetGain(gain);
cout<<"Overall gain :"<<gain<<endl;
break;
default:
if (c!=-1) cerr <<"unknown key "<<c<<endl;
break;
}
if (!pause) {
cout<<"GrabNextImage"<<endl;
cam->GrabSingle(pic);
cout<<"GrabNextImage done"<<endl;
if (color) {
cout<<"Color"<<endl;
if (slowconversion){
if (pic2.GetWidth()!=pic.GetWidth() ||
pic2.GetHeight()!=pic.GetHeight() ||
pic2.GetChannelCount() !=3 ){
if (!pic2.IsEmpty()) pic2.Release();
pic2.Init(pic.GetWidth(), pic.GetHeight(), 3);
}
ImageConvert::ToRGB(pic, pic2);
} else {
ImageConvert::ToRGB(pic, pic2);
}
}
else
{
cout<<"B/W"<<endl;
}
//if (i==10)
// pic2.ExportImage("Cam.pnm",MIP_pnm);
if (save) {
os.str("");
os << "frame" << setfill('0') << setw(5) << savecounter;
//if (ImageIO::Save(os.str(), pic2, true)!=0){
if (ImageIO::Save(os.str(),
pic2,
true)!=0){
BIASERR("error saving frame "<<i<<" at file "<<os.str()<<".mip");
}
cerr << "\nsaved frame "<<i<<" in "<<os.str()<<endl;
save = false;
savecounter++;
}
if (autosave){
os.str("");
os << "frame" << setfill('0') << setw(5) << savecounter;
//if (ImageIO::Save(os.str(), pic2, true)!=0){
if (ImageIO::Save(os.str(),
pic2,
true)!=0){
BIASERR("error saving frame "<<i<<" at file "<<os.str()<<".mip");
}
savecounter++;
}
CamWin.ShowImage(pic2);
cout <<"Frame: "<<i<<" Showing fps: "<<fps<<"\r";
cout.flush();
i++;
} // if (!pause)
}
}
int main(int argc, char *argv[])
{
int res = -1;
// prepare camera
cam = new VideoSource_DCAM;
cam->SetSize(1024,768,1);
//cam->SetSize(640,480,1);
// cam->SetFormat7(0,1024,1);
cam->SetDebugLevel(D_DCAM |D_DCAM_F7);
res = cam->OpenDevice() ;
cout <<"Openresult: "<<res<<endl;
// cam->ExternalTrigger(true);
if (res <0) {
cerr<<" Can not open camera. res="<<res<<endl;
exit(1);
}
cam->SetShutter(1.0f/50.0f);
cam->PreGrab();
cout <<" g : Auto Gain ON\n";
cout <<" G : Auto Gain OFF\n";
cout <<"123 : Gamma mode"<<endl;
cout <<" w : OnePushWhitebalance (not for PointGrey DragonFly)\n";
cout <<" 7/8: decr/incr blue gain (for white balancing manual)\n";
cout <<" 4/5: decr/incr red gain (for white balancing manual)\n";
cout <<" c : Color/Grey \n";
cout <<"SPC : save an image \n";
cout <<" s : save sequence\n";
cout <<" p : Stop/Resume\n";
cout <<" e : toggle between fast and normal conversion\n";
cout <<" q : Quit \n";
cam->SetDebugLevel(0);
// cam->SetFeature(FEATURE_SHARPNESS,0);
mainloop(cam );
cam->PostGrab();
cam->CloseDevice();
delete cam;
return 0;
}