bazar  1.3.1
pyrimage.cpp
Go to the documentation of this file.
1 /*
2 Copyright 2005, 2006 Computer Vision Lab,
3 Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
4 All rights reserved.
5 
6 This file is part of BazAR.
7 
8 BazAR is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 BazAR is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 BazAR; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21 #include <iostream>
22 #include <highgui.h>
23 #include "pyrimage.h"
24 
25 using namespace std;
26 
27 PyrImage::PyrImage(IplImage *im, int nblev) : nbLev(nblev)
28 {
29  images = new IplImage *[nblev];
30 
31  images[0] = im;
32 
33  for (int i=1; i<nbLev; ++i) {
34  images[i] = cvCreateImage(cvSize(images[i-1]->width/2, images[i-1]->height/2),
35  im->depth, im->nChannels);
36  }
37 }
38 
40  for (int i=0; i<nbLev; ++i)
41  cvReleaseImage(&images[i]);
42  delete [] images;
43 }
44 
46 {
47  for (int i=1; i<nbLev; ++i)
48  cvPyrDown(images[i-1], images[i]);
49 }
50 
51 PyrImage *PyrImage::load(int level, const char *filename, int color, bool fatal) {
52 
53  IplImage *im = cvLoadImage(filename, color);
54 
55  if (im == 0) {
56  cerr << filename << ": unable to load image.\n";
57  if (fatal)
58  exit(-1);
59  return 0;
60  }
61 
62  PyrImage *r= new PyrImage(im, level);
63  r->build();
64  return r;
65 }
66 
67 int PyrImage::convCoord(int x, int from, int to, unsigned max)
68 {
69  if (max == 2)
70  return (convCoord(x, from, to, 0) + convCoord(x, from, to, 1)) >> 1;
71  if (to==from)
72  return x;
73  if (to<from) {
74  if (max == 1) {
75  int r=x;
76  for (int i=from; i<to; ++i) {
77  r = r*2 + 1;
78  }
79  return r;
80  }
81  return x << ( from - to );
82  }
83  return x >> ( to - from );
84 }
85 
86 float PyrImage::convCoordf(float x, int from, int to)
87 {
88  if (to == from)
89  return x;
90 
91  if (to<from)
92  return x * float(1 << (from-to));
93 
94  return x / float(1 << (to-from));
95 }
96 
97 void PyrImage::setPixel(unsigned x, unsigned y, CvScalar &val)
98 {
99  if (( x >= (unsigned)images[0]->width ) || ( y >= (unsigned)images[0]->height ))
100  return;
101 
102  for (int i=0; i<nbLev; ++i) {
103  cvSet2D(images[i],
104  convCoord((int)y, 0, i),
105  convCoord((int)x, 0, i),
106  val);
107  }
108 }
109 
110 void PyrImage::set(CvScalar val)
111 {
112  for (int i=0; i<nbLev; ++i)
113  cvSet(images[i], val);
114 }
115 
117 {
118  PyrImage *p = new PyrImage(cvCloneImage(images[0]), nbLev);
119  for (int i=1; i<nbLev; ++i)
120  cvCopy(images[i], p->images[i]);
121  return p;
122 }
123 
124 void PyrImage::setImageROI(CvRect rect)
125 {
126  CvRect r;
127  for (int i=0; i<nbLev; ++i) {
128  r.x = convCoord(rect.x, 0, i);
129  r.y = convCoord(rect.y, 0, i);
130  r.width = convCoord(rect.width, 0, i);
131  r.height = convCoord(rect.height, 0, i);
132  cvSetImageROI(images[i], r);
133  }
134 }
135 
137 {
138  for (int i=0; i<nbLev; ++i) {
139  cvResetImageROI(images[i]);
140  }
141 }
142