RGBImage.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00048
#ifndef RGBIMAGE_INC
00049
#define RGBIMAGE_INC
00050
00051
#include <plearn/base/Array.h>
00052
00053
#include <plearn/base/Object.h>
00054
#include <plearn/base/Storage.h>
00055
#include <plearn/math/TMat.h>
00056
00057
00058
namespace PLearn {
00059
using namespace std;
00060
00061
00062
class HSV;
00063
00064 class RGB
00065 {
00066
public:
00067 unsigned char r;
00068 unsigned char g;
00069 unsigned char b;
00070
00071 RGB() {}
00072 RGB(
unsigned char the_r,
unsigned char the_g,
unsigned char the_b)
00073 :
r(the_r),
g(the_g),
b(the_b) {}
00074
00076
00077
public:
00078
static const RGB BLACK;
00079
static const RGB WHITE;
00080
static const RGB RED;
00081
static const RGB GREEN;
00082
static const RGB BLUE;
00083
static const RGB DISCOCOLORS[];
00084 };
00085
00086
00087 class HSV
00088 {
00089
public:
00090 unsigned char h;
00091 unsigned char s;
00092 unsigned char v;
00093
00094 HSV() {}
00095 HSV(
unsigned char the_h,
unsigned char the_s,
unsigned char the_v)
00096 :
h(the_h),
s(the_s),
v(the_v) {}
00097
00099 };
00100
00101
00103 class RGBImage
00104 {
00105
protected:
00106 int height_;
00107 int width_;
00108
00109 PP< Storage<RGB> >
storage;
00110 char tmpfilename[100];
00111
00112
public:
00113
RGBImage(
int the_height=0,
int the_width=0);
00114
RGBImage(
Mat r,
Mat g,
Mat b);
00115
~RGBImage();
00116
00117 int width()
const {
return width_; }
00118 int height()
const {
return height_; }
00119
void resize(
int newheight,
int newwidth);
00120
00121
inline RGB getPixel(
int i,
int j)
const;
00122
inline void setPixel(
int i,
int j,
RGB value);
00123
void fill(
RGB color);
00124 void clear() { fill(
RGB(255,255,255)); }
00125
00126
void loadPPM(
const char* filename);
00127
void savePPM(
const char* filename)
const;
00128
void loadJPEG(
const char* filename,
int scale=1);
00129
void display() const;
00130
void displayAndWait() const;
00131
00133
void removeBorders(
int top_border,
int bottom_border,
00134
int left_border,
int right_border);
00135
00137 void removeBorders(
int border) {
removeBorders(border,border,border,border); }
00138
00141
void shrinkToIntersection(
int& i,
int& j,
int& h,
int& w)
const;
00142
00144
void blit(
int desti,
int destj,
RGBImage srcim);
00145
RGB computeAverage() const;
00146
00149
Vec computeHistogram(
int r_bins=16,
int g_bins=16,
int b_bins=16,
bool do_normalize=false);
00150 };
00151
00152 inline
RGB
00153 RGBImage::getPixel(
int i,
int j)
const
00154
{
00155
#ifdef BOUNDCHECK
00156
if(!
storage)
00157
PLERROR(
"In RGBImage::getPixeel(int i, int j) EMPTY IMAGE!!!");
00158
if(i<0 || j<0 || i>=
height_ || j>=
width_)
00159
PLERROR(
"In RGBImage::getPixel(int i, int j) OUT OF IMAGE BOUNDS");
00160
#endif
00161
return storage->data[i*
width_+j];
00162 }
00163
00164
inline void
00165 RGBImage::setPixel(
int i,
int j,
RGB value)
00166 {
00167
#ifdef BOUNDCHECK
00168
if(!
storage)
00169
PLERROR(
"In RGBImage::getPixeel(int i, int j) EMPTY IMAGE!!!");
00170
if(i<0 || j<0 || i>=
height_ || j>=
width_)
00171
PLERROR(
"In RGBImage::getPixel(int i, int j) OUT OF IMAGE BOUNDS");
00172
#endif
00173
storage->data[i*
width_+j] = value;
00174 }
00175
00176 class RGBImageDB
00177 {
00178
protected:
00179 Array<char*> filenames;
00180 Array<RGBImage*> images;
00181 int subsample_factor;
00182 int remove_border;
00183 int max_n_images_in_memory;
00184 int n_images_in_memory;
00185
00186
int append(
char* filename);
00187
void load(
char* dbfile);
00188
00189
RGBImageDB(
int the_subsample_factor=1,
int remove_border=0,
int the_max_n_images_in_memory=10);
00190
00191
public:
00194 Mat imageIdAndClass;
00195
00196
RGBImageDB(
char* dbfilename,
int the_subsample_factor=1,
int remove_border=0,
int the_max_n_images_in_memory=10);
00197
00198
RGBImage getImage(
int imageid);
00199
00202
Mat computeHistogramRepresentation(
int r_bins=16,
int g_bins=16,
int b_bins=16,
bool do_normalize=
false);
00203
00204
~RGBImageDB();
00205 };
00206
00207
00208
class RGBImagesVMatrix;
00209
00222 class RGBImageVMatrix:
public Object
00223 {
00224
friend class RGBImagesVMatrix;
00225
00226
protected:
00227 RGBImage image;
00228 Vec delta_row;
00229 Vec delta_column;
00230 real scale;
00231 real offset_;
00232 int width_;
00233 int max_delta_row,
max_delta_column;
00234 int first_row;
00235 int first_column;
00236 int bottom_border_row;
00237 int right_border_col;
00238 int n_cols;
00239 int current_i,
current_j;
00240
00241
public:
00249
RGBImageVMatrix(
RGBImage image,
00250
const Vec& delta_row,
const Vec& delta_col,
00251
real scale=1.0,
00252
real offset_=0.0);
00253
virtual int width();
00254
virtual int length();
00255
virtual void reset();
00256
virtual void sample(
Vec& samplevec);
00257
virtual void seek(
int position);
00258
virtual int position();
00259
00261 virtual bool first()
00262 {
return (
current_i==
first_row) && (
current_j==
first_column); }
00263
00265
void setImage(
RGBImage new_image);
00266 };
00267
00268
00274 class RGBImagesVMatrix:
public Object
00275 {
00276
protected:
00277 RGBImageVMatrix image_distr;
00278 RGBImageDB&
images_db;
00279 int current_image;
00280 bool append_class;
00281 int length_;
00282 Vec pixelsAndClass;
00283 Vec pixels;
00284 Vec image_start;
00285 int width_;
00286
00287
public:
00288
RGBImagesVMatrix(
RGBImageDB& images_db,
00289
const Vec& delta_row,
00290
const Vec& delta_col,
00291
bool append_class,
00292
real scale=1.0,
00293
real offset_=0.0);
00294
00295
virtual int width();
00296
virtual int length();
00297
virtual void reset();
00298
virtual void sample(
Vec& samplevec);
00299
virtual void seek(
int position);
00300
virtual int position();
00301
virtual bool firstSampleOfObject();
00302
virtual int nSamplesOfObject();
00303
virtual int nObjects();
00304 };
00305
00338 }
00339
00340
#endif
00341
Generated on Tue Aug 17 16:04:12 2004 for PLearn by
1.3.7