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

Revision 1020, 8.5 KB checked in by dave, 9 years ago (diff)

changes for (and during) pixelache public event

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 <list>
19#include "App.h"
20#include "FileTools.h"
21#include "PCA.h"
22#include "Blob.h"
23#include "BlobResult.h"
24
25//#define SAVE_FRAMES
26
27using namespace std;
28
29//int w=50;
30//int h=80;
31
32int w=20;
33int h=30;
34
35App::App(const string &filename) :
36m_Capture(NULL),
37m_Frame(NULL),
38m_FrameCopy(NULL),
39m_FrameNum(0),
40m_SingleImage(false)
41{
42        if (filename=="")
43        {
44
45        //int ncams = cvcamGetCamerasCount( );    //returns the number of available cameras in the system
46        //int* out; int nselected = cvcamSelectCamera(&out);
47
48                m_Capture = cvCaptureFromCAM(1);
49        }
50        else
51        {
52        cerr<<"loading from "<<filename<<endl;
53                //m_Capture = cvCaptureFromFile(filename.c_str());
54        m_Frame = cvLoadImage(filename.c_str());
55        m_SingleImage=true;
56        }
57       
58        cvInitFont( &m_Font, CV_FONT_HERSHEY_PLAIN, 1, 1, 0, 1, CV_AA );
59        cvInitFont( &m_LargeFont, CV_FONT_HERSHEY_PLAIN, 5, 5, 0, 10, CV_AA );   
60        cvNamedWindow( "harvest", 1 );
61}
62
63App::~App()
64{
65}
66
67static CvScalar colors[] =
68    {
69        {{0,0,255}},
70        {{0,0,0}},
71        {{0,255,255}},
72        {{0,255,0}},
73        {{255,128,0}},
74        {{255,255,0}},
75        {{255,0,0}},
76        {{255,0,255}},
77    };
78
79void App::Run()
80{
81    if( !m_SingleImage )
82        {
83        m_Frame = cvQueryFrame( m_Capture );
84        }
85       
86        if( !m_FrameCopy )
87        m_FrameCopy = cvCreateImage( cvSize(m_Frame->width,m_Frame->height),
88                                    IPL_DEPTH_8U, m_Frame->nChannels );
89    if( m_Frame->origin == IPL_ORIGIN_TL )
90        cvCopy( m_Frame, m_FrameCopy, 0 );
91    else
92        cvFlip( m_Frame, m_FrameCopy, 0 );
93       
94        Image camera(m_FrameCopy);
95        Update(camera);
96       
97        m_FrameNum++;
98#ifdef SAVE_FRAMES
99        char name[256];
100        sprintf(name,"out-%0.4d.jpg",m_FrameNum);
101        cerr<<"saving "<<name<<endl;
102        cvSaveImage(name,camera.m_Image);
103#endif
104
105        cvShowImage("harvest", camera.m_Image);
106}
107
108char *spirits[]={"TreeSpirit","ShrubSpirit","CoverSpirit"};
109
110bool PointInside(int x, int y, CvRect b)
111{
112    return (x>b.x && x<b.x+b.width &&
113            y>b.y && y<b.y+b.height);
114}
115
116bool Inside(CvRect a, CvRect b)
117{
118    return PointInside(a.x,a.y,b) ||
119        PointInside(a.x+a.width,a.y,b) ||
120        PointInside(a.x,a.y+a.height,b) ||
121        PointInside(a.x+a.width,
122                    a.y+a.height,b);
123}
124
125void App::Update(Image &camera)
126{       
127    camera=camera.Scale(camera.m_Image->width/2,
128                        camera.m_Image->height/2);
129
130    cvFlip(camera.m_Image, NULL, 0);
131
132        ///////////////////////////////////
133        // dispatch from input
134
135        int key=cvWaitKey(10);
136
137//    usleep(500);
138       
139        static int t=150;
140    static bool viewthresh=false;
141    static bool off=false;
142    static int spirit=0;
143    static int crop_x=0;
144    static int crop_y=0;
145    static int crop_w=camera.m_Image->width;
146    static int crop_h=camera.m_Image->height;
147
148        switch (key)
149        {
150    case 't': viewthresh=!viewthresh; break;
151    case 'q': t--; break;
152    case 'w': t++; break;
153    case 'e': t-=20; break;
154    case 'r': t+=20; break;
155    case 'o': off=!off; break;
156    case 'p': spirit++; break;
157    case 'z': crop_x+=10; break;   
158    case 'x': crop_x-=10; break;   
159    case 'c': crop_y+=10; break;   
160    case 'v': crop_y-=10; break;   
161    case 'b': crop_w+=10; break;   
162    case 'n': crop_w-=10; break;   
163    case 'm': crop_h+=10; break;   
164    case ',': crop_h-=10; break;   
165        }                       
166
167    if (crop_x<0) crop_x=0;
168    if (crop_x>=camera.m_Image->width) crop_x=camera.m_Image->width-1;
169    if (crop_y<0) crop_x=0;
170    if (crop_y>=camera.m_Image->height) crop_y=camera.m_Image->height-1;
171    if (crop_w+crop_x>camera.m_Image->width)
172    {
173        crop_w=camera.m_Image->width-crop_x;
174    }
175    if (crop_h+crop_y>camera.m_Image->height)
176    {
177        crop_h=camera.m_Image->height-crop_y;
178    }
179
180    if (off)
181    {
182        sleep(1);
183        cerr<<"off..."<<endl;
184        return;
185    }
186
187    Image thresh=camera.RGB2GRAY().SubImage(crop_x,crop_y,crop_w,crop_h);
188    cvThreshold(thresh.m_Image,thresh.m_Image,t,255,CV_THRESH_BINARY);
189    // copy the threshold into a colour image
190    Image tofill=thresh.GRAY2RGB();
191    cvFloodFill(tofill.m_Image,cvPoint(10,10), CV_RGB(0,255,0),cvScalar(0),cvScalar(255));
192   
193    CBlobResult blobs;
194    blobs = CBlobResult( thresh.m_Image, NULL, 255 );
195    // exclude the ones smaller than param2 value
196    blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 10);
197
198    CBlob *currentBlob;
199    Image *out=NULL;
200
201    if (key=='s')
202    {
203        // add the alpha channel
204        Image src=camera.SubImage(crop_x,crop_y,crop_w,crop_h);
205        out = new Image(src.m_Image->width,
206                        src.m_Image->height, 8, 4);   
207       
208        for(int y=0; y<src.m_Image->height; y++)
209        {
210
211            for(int x=0; x<src.m_Image->width; x++)
212            {
213                CvScalar col = cvGet2D(src.m_Image,y,x);
214                CvScalar alpha = cvGet2D(tofill.m_Image,y,x);
215                if (alpha.val[0]==0 &&
216                    alpha.val[1]==255 &&
217                    alpha.val[2]==0)
218                    col.val[3]=0;
219                else
220                    col.val[3]=255;
221                cvSet2D(out->m_Image,y,x,col);
222            }
223        }   
224    }
225
226    if (key=='s')
227    {
228        cerr<<"deleting old images in islands/"<<endl;
229        int r=system("rm islands/*");
230    }
231   
232    list<CvRect> allrects;
233
234    for (int i = 0; i < blobs.GetNumBlobs(); i++ )
235    {
236        currentBlob = blobs.GetBlob(i);
237        allrects.push_back(currentBlob->GetBoundingBox());
238    }
239
240    list<CvRect> filteredrects=allrects;
241
242    /* for (list<CvRect>::iterator i=allrects.begin();
243         i!=allrects.end(); ++i)
244    {
245        bool in=false;
246        for (list<CvRect>::iterator j=allrects.begin();
247             j!=allrects.end(); ++j)
248        {
249            if (Inside(*i,*j)) in=true;
250        }
251        if (!in) filteredrects.push_back(*i);   
252        }*/
253
254    unsigned int instance = rand();
255
256    unsigned int count=0;
257    for (list<CvRect>::iterator i=filteredrects.begin();
258         i!=filteredrects.end(); ++i)
259    {
260        CvRect rect = *i;
261
262        if (key=='s')
263        {
264            Image island = out->SubImage(rect.x,rect.y,
265                                         rect.width,rect.height);
266           
267            char buf[256];
268            sprintf(buf,"islands/island-%d-%d-%d.png",count,
269                    rect.x+rect.width/2,
270                    rect.y+rect.height/2);
271            cerr<<"saving "<<buf<<endl;
272            island.Save(buf);
273
274            sprintf(buf,"dump/island-%d-%d-%d-%d.png",
275                    instance,
276                    count,
277                    rect.x+rect.width/2,
278                    rect.y+rect.height/2);
279            cerr<<"saving "<<buf<<endl;
280            island.Save(buf);
281
282        }
283        else
284        {
285            cvRectangle(camera.m_Image,
286                        cvPoint(crop_x+rect.x,crop_y+rect.y),
287                        cvPoint(crop_x+rect.x+rect.width,
288                                crop_y+rect.y+rect.height),
289                        colors[1]);
290        }
291        count++;
292    }
293
294    if (key=='s')
295    {
296        cerr<<"copying images to server"<<endl;
297        //int r=system("scp -r islands garden@t0.fo.am:/home/garden/GerminationX/oak/");
298        string path("/home/dave/code/lirec/scenarios/GerminationX/oak/public/");
299        path+=string(spirits[spirit%3]);
300        string command=string("rm ")+path+string("/*.*");
301        int r=system(command.c_str());
302        string command2=string("cp islands/* ")+path;
303        r=system(command2.c_str());
304        //cerr<<"finished copying...("<<r<<")"<<endl;
305    }
306
307    if (viewthresh) camera=tofill;
308
309    char buf[256];
310    sprintf(buf,"spirit: %s thresh: %d", spirits[spirit%3], t);
311    cvPutText(camera.m_Image, buf, cvPoint(10,20),
312              &m_Font, colors[0]);
313
314    cvRectangle(camera.m_Image,
315                cvPoint(crop_x,crop_y),
316                cvPoint(crop_x+crop_w,crop_y+crop_h),
317                colors[2]);
318
319    if (out!=NULL) delete out;
320}
321
Note: See TracBrowser for help on using the repository browser.