source: libs/magicsquares/applications/harvest/src/App.cpp @ 998

Revision 998, 6.3 KB checked in by dave, 10 years ago (diff)

select spirit, opencv updated to fix camera

Line 
1// Copyright (C) 2009 foam
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation; either version 2 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17#include <assert.h>
18#include "App.h"
19#include "FileTools.h"
20#include "PCA.h"
21#include "Blob.h"
22#include "BlobResult.h"
23
24//#define SAVE_FRAMES
25
26using namespace std;
27
28//int w=50;
29//int h=80;
30
31int w=20;
32int h=30;
33
34App::App(const string &filename) :
35m_Capture(NULL),
36m_Frame(NULL),
37m_FrameCopy(NULL),
38m_FrameNum(0),
39m_SingleImage(false)
40{
41        if (filename=="")
42        {
43
44        //int ncams = cvcamGetCamerasCount( );    //returns the number of available cameras in the system
45        //int* out; int nselected = cvcamSelectCamera(&out);
46
47                m_Capture = cvCaptureFromCAM(0);
48        }
49        else
50        {
51        cerr<<"loading from "<<filename<<endl;
52                //m_Capture = cvCaptureFromFile(filename.c_str());
53        m_Frame = cvLoadImage(filename.c_str());
54        m_SingleImage=true;
55        }
56       
57        cvInitFont( &m_Font, CV_FONT_HERSHEY_PLAIN, 0.5, 0.5, 0, 1, CV_AA );
58        cvInitFont( &m_LargeFont, CV_FONT_HERSHEY_PLAIN, 5, 5, 0, 10, CV_AA );   
59        cvNamedWindow( "harvest", 1 );
60}
61
62App::~App()
63{
64}
65
66static CvScalar colors[] =
67    {
68        {{0,0,255}},
69        {{0,0,0}},
70        {{0,255,255}},
71        {{0,255,0}},
72        {{255,128,0}},
73        {{255,255,0}},
74        {{255,0,0}},
75        {{255,0,255}},
76    };
77
78void App::Run()
79{
80    if( !m_SingleImage )
81        {
82        m_Frame = cvQueryFrame( m_Capture );
83        }
84       
85        if( !m_FrameCopy )
86        m_FrameCopy = cvCreateImage( cvSize(m_Frame->width,m_Frame->height),
87                                    IPL_DEPTH_8U, m_Frame->nChannels );
88    if( m_Frame->origin == IPL_ORIGIN_TL )
89        cvCopy( m_Frame, m_FrameCopy, 0 );
90    else
91        cvFlip( m_Frame, m_FrameCopy, 0 );
92       
93        Image camera(m_FrameCopy);
94        Update(camera);
95       
96        m_FrameNum++;
97#ifdef SAVE_FRAMES
98        char name[256];
99        sprintf(name,"out-%0.4d.jpg",m_FrameNum);
100        cerr<<"saving "<<name<<endl;
101        cvSaveImage(name,camera.m_Image);
102#endif
103
104        cvShowImage("harvest", camera.m_Image);
105}
106
107char *spirits[]={"CanopySpirit","VerticalSpirit","CoverSpirit"};
108
109void App::Update(Image &camera)
110{       
111        ///////////////////////////////////
112        // dispatch from input
113
114        int key=cvWaitKey(10);
115
116//    usleep(500);
117       
118        static int t=150;
119    static bool viewthresh=false;
120    static bool off=false;
121    static int spirit=0;
122
123        switch (key)
124        {
125    case 't': viewthresh=!viewthresh; break;
126    case 'q': t--; break;
127    case 'w': t++; break;
128    case 'o': off=!off; break;
129    case 'p': spirit++; break;
130        }                       
131
132    if (off)
133    {
134        sleep(1);
135        cerr<<"off..."<<endl;
136        return;
137    }
138
139    Image thresh=camera.RGB2GRAY();
140    cvThreshold(thresh.m_Image,thresh.m_Image,t,255,CV_THRESH_BINARY);
141    // copy the threshold into a colour image
142    Image tofill=thresh.GRAY2RGB();
143    cvFloodFill(tofill.m_Image,cvPoint(10,10), CV_RGB(0,255,0),cvScalar(0),cvScalar(255));
144   
145    CBlobResult blobs;
146    blobs = CBlobResult( thresh.m_Image, NULL, 255 );
147    // exclude the ones smaller than param2 value
148    blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 100);
149
150    CBlob *currentBlob;
151    Image *out=NULL;
152
153    if (key=='s')
154    {
155        // add the alpha channel
156        out = new Image(camera.m_Image->width,
157                        camera.m_Image->height, 8, 4);   
158       
159        for(int y=0; y<camera.m_Image->height; y++)
160        {
161
162            for(int x=0; x<camera.m_Image->width; x++)
163            {
164                CvScalar col = cvGet2D(camera.m_Image,y,x);
165                CvScalar alpha = cvGet2D(tofill.m_Image,y,x);
166                if (alpha.val[0]==0 &&
167                    alpha.val[1]==255 &&
168                    alpha.val[2]==0)
169                    col.val[3]=0;
170                else
171                    col.val[3]=255;
172                cvSet2D(out->m_Image,y,x,col);
173            }
174        }   
175    }
176
177    if (key=='s')
178    {
179        cerr<<"deleting old images in islands/"<<endl;
180        int r=system("rm islands/*");
181    }
182   
183    for (int i = 0; i < blobs.GetNumBlobs(); i++ )
184    {
185        currentBlob = blobs.GetBlob(i);
186        //currentBlob->FillBlob( camera.m_Image, CV_RGB(255,0,0));
187
188        CvRect rect = currentBlob->GetBoundingBox();
189
190        if (key=='s')
191        {
192            Image island = out->SubImage(rect.x,rect.y,
193                                         rect.width,rect.height);
194           
195            char buf[256];
196            sprintf(buf,"islands/island-%d-%d-%d.png",i,
197                    rect.x+rect.width/2,
198                    rect.y+rect.height/2);
199            cerr<<"saving "<<buf<<endl;
200            island.Save(buf);
201        }
202        else
203        {
204            char buf[256];
205            sprintf(buf,"%d",currentBlob->GetID());
206            cvPutText(camera.m_Image, buf, cvPoint(rect.x+rect.width/2,
207                                                   rect.y+rect.height/2),
208                      &m_Font, colors[0]);
209           
210            cvRectangle(camera.m_Image,
211                        cvPoint(rect.x,rect.y),
212                        cvPoint(rect.x+rect.width,rect.y+rect.height),
213                        colors[1]);
214        }
215    }
216
217    if (key=='s')
218    {
219        cerr<<"copying images to server"<<endl;
220        //int r=system("scp -r islands garden@t0.fo.am:/home/garden/GerminationX/oak/");
221        string path("/home/dave/code/lirec/scenarios/GerminationX/oak/public/");
222        path+=string(spirits[spirit%3]);
223        string command=string("rm ")+path+string("/*.*");
224        int r=system(command.c_str());
225        string command2=string("cp islands/* ")+path;
226        r=system(command2.c_str());
227        //cerr<<"finished copying...("<<r<<")"<<endl;
228    }
229
230    if (viewthresh) camera=tofill;
231
232    char buf[256];
233    sprintf(buf,"spirit: %s thresh: %d", spirits[spirit%3], t);
234    cvPutText(camera.m_Image, buf, cvPoint(10,10),
235              &m_Font, colors[0]);
236
237
238    if (out!=NULL) delete out;
239}
240
Note: See TracBrowser for help on using the repository browser.