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

Revision 1003, 7.5 KB checked in by dave, 9 years ago (diff)

added crop option

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(1);
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    static int crop_x=0;
123    static int crop_y=0;
124    static int crop_w=camera.m_Image->width;
125    static int crop_h=camera.m_Image->height;
126
127        switch (key)
128        {
129    case 't': viewthresh=!viewthresh; break;
130    case 'q': t--; break;
131    case 'w': t++; break;
132    case 'e': t-=20; break;
133    case 'r': t+=20; break;
134    case 'o': off=!off; break;
135    case 'p': spirit++; break;
136    case 'z': crop_x+=10; break;   
137    case 'x': crop_x-=10; break;   
138    case 'c': crop_y+=10; break;   
139    case 'v': crop_y-=10; break;   
140    case 'b': crop_w+=10; break;   
141    case 'n': crop_w-=10; break;   
142    case 'm': crop_h+=10; break;   
143    case ',': crop_h-=10; break;   
144        }                       
145
146    if (crop_x<0) crop_x=0;
147    if (crop_x>=camera.m_Image->width) crop_x=camera.m_Image->width;
148    if (crop_y<0) crop_x=0;
149    if (crop_y>=camera.m_Image->width) crop_x=camera.m_Image->width;
150    if (crop_w+crop_x>camera.m_Image->width)
151    {
152        crop_w=camera.m_Image->width-crop_x;
153    }
154    if (crop_h+crop_y>camera.m_Image->height)
155    {
156        crop_h=camera.m_Image->height-crop_y;
157    }
158
159    if (off)
160    {
161        sleep(1);
162        cerr<<"off..."<<endl;
163        return;
164    }
165
166    Image thresh=camera.RGB2GRAY().SubImage(crop_x,crop_y,crop_w,crop_h);
167    cvThreshold(thresh.m_Image,thresh.m_Image,t,255,CV_THRESH_BINARY);
168    // copy the threshold into a colour image
169    Image tofill=thresh.GRAY2RGB();
170    cvFloodFill(tofill.m_Image,cvPoint(10,10), CV_RGB(0,255,0),cvScalar(0),cvScalar(255));
171   
172    CBlobResult blobs;
173    blobs = CBlobResult( thresh.m_Image, NULL, 255 );
174    // exclude the ones smaller than param2 value
175    blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 100);
176
177    CBlob *currentBlob;
178    Image *out=NULL;
179
180    if (key=='s')
181    {
182        // add the alpha channel
183        Image src=camera.SubImage(crop_x,crop_y,crop_w,crop_h);
184        out = new Image(src.m_Image->width,
185                        src.m_Image->height, 8, 4);   
186       
187        for(int y=0; y<src.m_Image->height; y++)
188        {
189
190            for(int x=0; x<src.m_Image->width; x++)
191            {
192                CvScalar col = cvGet2D(src.m_Image,y,x);
193                CvScalar alpha = cvGet2D(tofill.m_Image,y,x);
194                if (alpha.val[0]==0 &&
195                    alpha.val[1]==255 &&
196                    alpha.val[2]==0)
197                    col.val[3]=0;
198                else
199                    col.val[3]=255;
200                cvSet2D(out->m_Image,y,x,col);
201            }
202        }   
203    }
204
205    if (key=='s')
206    {
207        cerr<<"deleting old images in islands/"<<endl;
208        int r=system("rm islands/*");
209    }
210   
211    for (int i = 0; i < blobs.GetNumBlobs(); i++ )
212    {
213        currentBlob = blobs.GetBlob(i);
214        //currentBlob->FillBlob( camera.m_Image, CV_RGB(255,0,0));
215
216        CvRect rect = currentBlob->GetBoundingBox();
217
218        if (key=='s')
219        {
220            Image island = out->SubImage(rect.x,rect.y,
221                                         rect.width,rect.height);
222           
223            char buf[256];
224            sprintf(buf,"islands/island-%d-%d-%d.png",i,
225                    rect.x+rect.width/2,
226                    rect.y+rect.height/2);
227            cerr<<"saving "<<buf<<endl;
228            island.Save(buf);
229        }
230        else
231        {
232            char buf[256];
233            sprintf(buf,"%d",currentBlob->GetID());
234            cvPutText(camera.m_Image, buf, cvPoint(crop_x+rect.x+rect.width/2,
235                                                   crop_y+rect.y+rect.height/2),
236                      &m_Font, colors[0]);
237           
238            cvRectangle(camera.m_Image,
239                        cvPoint(crop_x+rect.x,crop_y+rect.y),
240                        cvPoint(crop_x+rect.x+rect.width,
241                                crop_y+rect.y+rect.height),
242                        colors[1]);
243        }
244    }
245
246    if (key=='s')
247    {
248        cerr<<"copying images to server"<<endl;
249        //int r=system("scp -r islands garden@t0.fo.am:/home/garden/GerminationX/oak/");
250        string path("/home/dave/code/lirec/scenarios/GerminationX/oak/public/");
251        path+=string(spirits[spirit%3]);
252        string command=string("rm ")+path+string("/*.*");
253        int r=system(command.c_str());
254        string command2=string("cp islands/* ")+path;
255        r=system(command2.c_str());
256        //cerr<<"finished copying...("<<r<<")"<<endl;
257    }
258
259    if (viewthresh) camera=tofill;
260
261    char buf[256];
262    sprintf(buf,"spirit: %s thresh: %d", spirits[spirit%3], t);
263    cvPutText(camera.m_Image, buf, cvPoint(10,10),
264              &m_Font, colors[0]);
265
266    cvRectangle(camera.m_Image,
267                cvPoint(crop_x,crop_y),
268                cvPoint(crop_x+crop_w,crop_y+crop_h),
269                colors[2]);
270
271    if (out!=NULL) delete out;
272}
273
Note: See TracBrowser for help on using the repository browser.