Expand|Select|Wrap|Line Numbers
- #define MIN(a,b) (a<b?a:b)
- #define MAX(a,b) (a>b?a:b)
- #include "Video.h"
- #define NO_HUE -1
- // CS added.
- #include <math.h>
- #include <fstream>
- #include <iostream>
- #include "stdafx.h"
- int Video :: RGBtoHue(int r, int g, int b)
- {
- int hue, delta, max, min;
- max = MAX(r, MAX(g,b));
- min = MIN(r, MIN(g,b));
- delta = max - min;
- hue =0;
- //----------------------------------------------------------------------------------------------------------
- /* initialise hue*/
- if (2 * delta <= max) {hue = NO_HUE;}
- else {
- if (r == max) hue = 42 + 42*(g-b) / delta; /* 1*42 */
- else if (g == max) hue = 126 + 42*(b-r) / delta; /* 3*42 */
- else if (b == max) hue = 210 + 42*(r-g) / delta; /* 5*42 */
- /* now: hue is in range [0..252] */
- }
- return hue;
- }
- //-------------------------------------------------------------------------------------------------------------
- Video::Video(int x, int y, int width, int height) : Fl_Window(x, y, width *2, height*2, "RA_LAB5 iVideoCap"){
- setUpDone = false;
- display = new Fl_Box(x,y,width,height); // Raw Image Display
- this->add(display);
- this->end();
- this->show();
- Fl::add_timeout(1.0, callback, this);
- };
- // Video Deconstructor
- Video::~Video(){
- VI.stopDevice(0);
- if (setUpDone) {delete [] pic;}
- };
- // Setup Cammera
- void Video::setUpCam(){
- if(!setUpDone){
- VI.setupDevice(0,640,480);
- this->resize(this->x(),this->y(),VI.getWidth(0),VI.getHeight(0));
- pic = new uchar[3*VI.getWidth(0)*VI.getHeight(0)];
- display->resize(0,0,VI.getWidth(0),VI.getHeight(0));
- rgbImg = new Fl_RGB_Image(pic,VI.getWidth(0),VI.getHeight(0));
- display->image(rgbImg);
- setUpDone = true;
- bb=0 ;
- for(int i=0 ; i <16 ; i++ )
- medi[i]=0;
- }
- }
- //--------------------------------------------------------------------------------------------------------------------
- void Video::goahead(){
- int modo;
- int i;
- int j;
- int limit;
- int huecalc,max,maxi,rows,columns;
- //typedef uchar colimage[640][480][3];
- //colimage j_newPic;
- max=0;
- //int count[640];
- //....
- if(camConnected()){
- this->setUpCam();
- if( VI.isFrameNew(0)){
- modo = VI.getWidth(0) *3 ;
- int r,g,b;
- uchar *newPic = grabFrame();
- //j_newPic = const_cast<uchar* >(pic);
- //*j_newPic = grabFrame();// + VI.getWidth(0)*VI.getHeight(0)*3 ;
- /*uchar *newPicc*/m_newPicc = const_cast<uchar* >(pic);
- //j_newPic = const_cast<uchar* >(pic);
- int average = 0 ; // to keep the average of the picture
- int histo[641]; // horisontal histogram
- int vertical_histo[481]; // vertical histogram
- int vh=481; // used to trace the vertical histogram
- for(i=0;i<640;i++)
- histo[i]=0;
- for(i=0;i<481;i++)
- vertical_histo[i]=0;
- int x; //
- int y =1 ; // temporary , only for check , every one frame one hueToscreen
- int heuToscreen = 0 ; // is used to show the output of the heucalc function
- int intensity = 0 ;
- //------------------------------------------------------------------------------------------------------------
- for( i = VI.getWidth(0)*(VI.getHeight(0)-1)*3; i >= 0; i-=modo){
- limit = i+ modo;
- x = 0 ; //
- vh--; // [ 480 -> 0 ]
- for (j = i; j <limit;) {
- m_newPicc[j++] = *(newPic++);
- r=(int)m_newPicc[j];
- m_newPicc[j++] = *(newPic++);
- g=(int)m_newPicc[j];
- m_newPicc[j++] = *(newPic++);
- b=(int)m_newPicc[j];
- huecalc = RGBtoHue(b,r,g);
- intensity = (r+g+b)/3 ;
- //------------------------ code to specify the heucalc -------------------
- // if(j%50==0 ){
- // printf(" \n\n\n hue : %d \n",huecalc);
- //printf(" intensity is : %d ",intensity);
- // }
- //if(y++%(2000)==0){
- //cout <<huecalc<<"\n";y=0;}
- //---------------------------------------------------------------------------
- x++;// 0 - 640 horizontal histogram index
- //if( 48<=huecalc && huecalc<=53 && intensity < 150) //labcam red noon indoor lab/evening [43.. 45]
- //if( 43<=huecalc && huecalc<=45 && intensity < 150) //labcam red noon outdoor cloude/sun [43.. 45]
- //if( 73<=huecalc && huecalc<=76)// && intensity < 150) //labcam yellow noon outdoor cloude/sun [72.. 76]
- //if( 68<=huecalc && huecalc<=73)// && intensity < 150) //mycam yellow night indoor [68.. 73]
- //if( 76<=huecalc && huecalc<=88)// && intensity < 150) //mycam yellow noon indoor cloudy/sun/lamp [76.. 88]
- if( 84<=huecalc && huecalc<=87)// && intensity < 150) // yellow/outdoor/dull [84:87]
- {
- histo[x]++; // horizontal histo
- //cout <<vh<<"\n";
- vertical_histo[vh]++; // vertical histo
- }
- } }
- //---------------------------------------------------------------------------------
- // left max of the horisontal histo
- int max1=-1;int centre=-1;
- for(i=0;i<641;i++)
- if(histo[i]>max1)
- {
- max1=histo[i];
- }
- for(i=0;i<641;i++)
- if(max1==histo[i])
- {centre=i;
- break;}
- // ------ left max of the vertical histo ----------------------------------------
- int max2=-1;
- int centre2=-1;
- for(i=0;i<481;i++)
- if(vertical_histo[i]>max2)
- {
- max2=vertical_histo[i];
- }
- for(i=0;i<481;i++)
- if(max2==vertical_histo[i])
- {centre2=i;
- break;}
- //-----------------------------------------------------------------------------------
- // calculating the average of medians
- if (bb<15)
- {
- bb++;
- medi[bb]=centre;
- }
- if(bb==15)
- {
- //every 15 itteration b [1..15] pick the 5 meadianvalues , then average them
- bb=0;
- for(i = 1;i<=15;i++) // first sort the array
- for (j=i;j<=15;j++)
- if (medi[i]<medi[j])
- {mid=medi[i];
- medi[i]=medi[j];
- medi[j]=mid;}
- mid=0;
- for(i=7;i<=9;i++)
- mid+=medi[i];
- //printf("%d \n",mid);}
- //cout << "\n\n\n the avgerage of 3 medeian number is : " << ;
- int avg =mid/3;
- //cout << "\n\n\n the avgerage of 3 medeian number is : " << avg << "\n";
- //ploting a vertical line in the center of the picture :
- if ( max1>6 )
- for( i = avg*3 ; i <=VI.getWidth(0)*(VI.getHeight(0)-1)*3 ; i+=modo)
- {
- m_newPicc[i] = 0;
- m_newPicc[i+1] = 0;
- m_newPicc[i+2] = 0;
- }
- //cout << "\n centre2 is : " <<centre2 ;
- //cout << centre2 <<"\n";
- //ploting a vertical line in the center of the picture :
- if (centre2 != -1 && centre2 != 0 && max2>6) {
- for( i =(centre2-1)*(640*3) ; i <=(centre2)*(640*3) ; i++)
- {
- m_newPicc[i] = 0;
- }
- for( i =(centre2)*(640*3) ; i <=(centre2+1)*(640*3) ; i++)
- {
- m_newPicc[i] = 0;
- }}
- if(centre<200 && centre>0 && max1>6 && max2>6){angle=1;}
- if(centre>440 && centre<640 && max1>6 && max2>6){angle=2;}
- if(centre>200 && centre<440 && max1>6 && max2>6){angle=3;}
- if(centre==0 || max1<=6 || max2<=6){angle=0;}
- }// end of : if(bb==15)
- //------------------------------------------------
- // ofstream out("documentation",ios::app) ;
- // while(!out.is_open()){;}
- //if (centre < 10) cout << "NO OBJECT \n";
- //if (260<centre<340) cout << "OBJECT IN THE MIDDLE \n";
- //if ( centre<200) cout << "OBJECT IN THE LEFT \n";
- //if (400<centre) cout << "OBJECT IN THE RIGHT \n";
- //printf("%d , ",centre); // to read the center of the object
- // out << "\t center of the object : " << centre ;
- // out.close();
- //printf("%d , ",heuToscreen); // is used to rea the correc heu
- //cout << "\t\n average of the picture is :"<<average ;
- //-----------------------------------------------------------------------------------------------
- //int error=320-centre;
- //angle=error/8;
- //------------------------------------------------------------------------------------------------
- rgbImg->uncache();
- display->redraw();
- }
- }
- else{
- display->copy_label("NO CAMERA CONNECTED");
- setUpDone = false;
- }
- Fl::repeat_timeout(0.003, callback,this);
- };
- //---------------------------------- END ----------------------------------------------------------