source: libs/magicsquares/examples/harvest/src/App.cpp @ 783

Revision 783, 4.7 KB checked in by dave, 10 years ago (diff)

added harvest

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                m_Capture = cvCaptureFromCAM(0);
44        }
45        else
46        {
47        cerr<<"loading from "<<filename<<endl;
48                //m_Capture = cvCaptureFromFile(filename.c_str());
49        m_Frame = cvLoadImage(filename.c_str());
50        m_SingleImage=true;
51        }
52       
53        cvInitFont( &m_Font, CV_FONT_HERSHEY_PLAIN, 0.5, 0.5, 0, 1, CV_AA );
54        cvInitFont( &m_LargeFont, CV_FONT_HERSHEY_PLAIN, 5, 5, 0, 10, CV_AA );   
55        cvNamedWindow( "harvest", 1 );
56}
57
58App::~App()
59{
60}
61
62static CvScalar colors[] =
63    {
64        {{0,0,255}},
65        {{0,0,0}},
66        {{0,255,255}},
67        {{0,255,0}},
68        {{255,128,0}},
69        {{255,255,0}},
70        {{255,0,0}},
71        {{255,0,255}},
72    };
73
74void App::Run()
75{
76    if( !m_SingleImage )
77        {
78        m_Frame = cvQueryFrame( m_Capture );
79        }
80       
81        if( !m_FrameCopy )
82        m_FrameCopy = cvCreateImage( cvSize(m_Frame->width,m_Frame->height),
83                                    IPL_DEPTH_8U, m_Frame->nChannels );
84    if( m_Frame->origin == IPL_ORIGIN_TL )
85        cvCopy( m_Frame, m_FrameCopy, 0 );
86    else
87        cvFlip( m_Frame, m_FrameCopy, 0 );
88       
89        Image camera(m_FrameCopy);
90        Update(camera);
91       
92        m_FrameNum++;
93#ifdef SAVE_FRAMES
94        char name[256];
95        sprintf(name,"out-%0.4d.jpg",m_FrameNum);
96        cerr<<"saving "<<name<<endl;
97        cvSaveImage(name,camera.m_Image);
98#endif
99
100        cvShowImage("harvest", camera.m_Image);
101}
102
103void App::Update(Image &camera)
104{       
105        ///////////////////////////////////
106        // dispatch from input
107
108        int key=cvWaitKey(10);
109       
110        static int t=200;
111    bool viewthresh=false;
112
113        switch (key)
114        {
115    case 'q': t--; viewthresh=true; break;
116    case 'w': t++; viewthresh=true; break;
117        }                       
118
119    Image thresh=camera.RGB2GRAY();
120    cvThreshold(thresh.m_Image,thresh.m_Image,t,255,CV_THRESH_BINARY);
121    //cvFloodFill(thresh.m_Image,cvPoint(10,10), cvScalar(255),cvScalar(0),cvScalar(255));
122   
123    CBlobResult blobs;
124    blobs = CBlobResult( thresh.m_Image, NULL, 255 );
125    // exclude the ones smaller than param2 value
126    blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 10);
127
128    if (viewthresh) camera=thresh;
129
130    CBlob *currentBlob;
131    Image *out=NULL;
132
133    if (key=='s')
134    {
135        // add the alpha channel
136        out = new Image(camera.m_Image->width,
137                        camera.m_Image->height, 8, 4);   
138       
139        for(int y=0; y<camera.m_Image->height; y++)
140        {
141
142            for(int x=0; x<camera.m_Image->width; x++)
143            {
144                CvScalar col = cvGet2D(camera.m_Image,y,x);
145                CvScalar alpha = cvGet2D(thresh.m_Image,y,x);
146                col.val[3]=256-alpha.val[0];
147                cvSet2D(out->m_Image,y,x,col);
148            }
149        }   
150    }
151   
152    for (int i = 0; i < blobs.GetNumBlobs(); i++ )
153    {
154        currentBlob = blobs.GetBlob(i);
155        //currentBlob->FillBlob( camera.m_Image, CV_RGB(255,0,0));
156
157        CvRect rect = currentBlob->GetBoundingBox();
158
159        if (key=='s')
160        {
161            Image island = out->SubImage(rect.x,rect.y,
162                                         rect.width,rect.height);
163           
164            char buf[256];
165            sprintf(buf,"islands/island-%d-%d-%d.png",i,
166                    rect.x+rect.width/2,
167                    rect.y+rect.height/2);
168            cerr<<"saving "<<buf<<endl;
169            island.Save(buf);
170        }
171        else
172        {
173            char buf[256];
174            sprintf(buf,"%d",currentBlob->GetID());
175            cvPutText(camera.m_Image, buf, cvPoint(rect.x,rect.y), &m_Font, colors[0]);
176           
177            cvRectangle(camera.m_Image,
178                        cvPoint(rect.x,rect.y),
179                        cvPoint(rect.x+rect.width,rect.y+rect.height),
180                        colors[1]);
181        }
182    }
183
184    if (out!=NULL) delete out;
185}
186
Note: See TracBrowser for help on using the repository browser.