PLearn 0.1
Public Member Functions | Protected Attributes
PLearn::RGBImage Class Reference

uses top left coordinate system Pixel (i,j) is at row i, column j More...

#include <RGBImage.h>

Collaboration diagram for PLearn::RGBImage:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 RGBImage (int the_height=0, int the_width=0)
 RGBImage (Mat r, Mat g, Mat b)
 r g and b must have values in range [0-255]
 ~RGBImage ()
int width () const
int height () const
void resize (int newheight, int newwidth)
RGB getPixel (int i, int j) const
 return RGB value of Pixel of row i and column j
void setPixel (int i, int j, RGB value)
 sets RGB value of pixel in row i and column j
void fill (RGB color)
void clear ()
void loadPPM (const char *filename)
void savePPM (const char *filename) const
void loadJPEG (const char *filename, int scale=1)
void display () const
void displayAndWait () const
void removeBorders (int top_border, int bottom_border, int left_border, int right_border)
 remove borders around the image
void removeBorders (int border)
 remove borders of thickness "border" from image (in-place), on all sides
void shrinkToIntersection (int &i, int &j, int &h, int &w) const
 shrinks the box that starts at position (i,j) (in this image) and of size (h,w) to its intersection with this image's box (0,0,this->height,this->width_)
void blit (int desti, int destj, RGBImage srcim)
 blits srcim at position (desti,destj) in this image
RGB computeAverage () const
Vec computeHistogram (int r_bins=16, int g_bins=16, int b_bins=16, bool do_normalize=false)
 Returns a vector of size r_bins*g_bins*b_bins which contains the number of image pixels whose colors fell in the corresponding RGB range.

Protected Attributes

int height_
int width_
PP< Storage< RGB > > storage
char tmpfilename [100]
 temporary file in which the image may be saved when displayed

Detailed Description

uses top left coordinate system Pixel (i,j) is at row i, column j

Definition at line 103 of file RGBImage.h.


Constructor & Destructor Documentation

PLearn::RGBImage::RGBImage ( int  the_height = 0,
int  the_width = 0 
)

Definition at line 64 of file RGBImage.cc.

References clear(), height_, storage, tmpfilename, and width_.

  :height_(the_height), width_(the_width)
{
  tmpnam(tmpfilename);
  if(height_>0 && width_>0)
    {
      storage = new Storage<RGB>(height_*width_);
      clear();
    }
}

Here is the call graph for this function:

PLearn::RGBImage::RGBImage ( Mat  r,
Mat  g,
Mat  b 
)

r g and b must have values in range [0-255]

Definition at line 75 of file RGBImage.cc.

References b, g, height_, i, j, setPixel(), storage, tmpfilename, and width_.

  :height_(r.length()), width_(r.width())
{
  tmpnam(tmpfilename);
  storage = new Storage<RGB>(height_*width_);
  for(int i=0; i<height_; i++)
    for(int j=0; j<width_; j++)
      setPixel(i,j,RGB((unsigned char)r(i,j), (unsigned char)g(i,j), (unsigned char)b(i,j)));
}

Here is the call graph for this function:

PLearn::RGBImage::~RGBImage ( )

Definition at line 86 of file RGBImage.cc.

References tmpfilename.

{
  unlink(tmpfilename);
}

Member Function Documentation

void PLearn::RGBImage::blit ( int  desti,
int  destj,
RGBImage  srcim 
)

blits srcim at position (desti,destj) in this image

Definition at line 224 of file RGBImage.cc.

References getPixel(), height_, i, j, setPixel(), shrinkToIntersection(), and width_.

{
  int blit_i = desti;
  int blit_j = destj;
  int blit_h = srcim.height_;
  int blit_w = srcim.width_;

  shrinkToIntersection(blit_i,blit_j,blit_h,blit_w);
  if(blit_h==0 || blit_w==0)
    return;

  int src_i = blit_i-desti;
  int src_j = blit_j-destj;

  for(int i=0; i<blit_h; i++)
    for(int j=0; j<blit_w; j++)
      setPixel(blit_i+i, blit_j+j, srcim.getPixel(src_i+i,src_j+j));
}

Here is the call graph for this function:

void PLearn::RGBImage::clear ( ) [inline]

Definition at line 124 of file RGBImage.h.

Referenced by RGBImage().

{ fill(RGB(255,255,255)); }

Here is the caller graph for this function:

RGB PLearn::RGBImage::computeAverage ( ) const

Definition at line 244 of file RGBImage.cc.

References b, PLearn::RGB::b, g, PLearn::RGB::g, height_, PLERROR, PLearn::RGB::r, storage, and width_.

{
  if(!storage)
    PLERROR("Empty RGBImage");
  RGB* data = storage->data;
  int r = 0;
  int g = 0;
  int b = 0;
  int npixels = height_*width_;
  for(int pos=0; pos<npixels; pos++)
    {
      RGB& pixl = data[pos];
      r += pixl.r;
      g += pixl.g;
      b += pixl.b;
    }
  return RGB(r/npixels, g/npixels, b/npixels);
}
Vec PLearn::RGBImage::computeHistogram ( int  r_bins = 16,
int  g_bins = 16,
int  b_bins = 16,
bool  do_normalize = false 
)

Returns a vector of size r_bins*g_bins*b_bins which contains the number of image pixels whose colors fell in the corresponding RGB range.

Definition at line 264 of file RGBImage.cc.

References PLearn::RGB::b, PLearn::RGB::g, getPixel(), height_, i, j, PLearn::RGB::r, and width_.

Referenced by PLearn::RGBImageDB::computeHistogramRepresentation().

{
  Vec histo(r_bins*g_bins*b_bins);
  for(int i=0; i<height_; i++)
    for(int j=0; j<width_; j++)
      {
        RGB pix = getPixel(i,j);
        int r_bin = pix.r*r_bins/256;
        int g_bin = pix.g*g_bins/256;
        int b_bin = pix.b*b_bins/256;
        int pos =  r_bin*g_bins*b_bins + g_bin*b_bins  + b_bin;
        histo[pos]++;
      }
  if(do_normalize)
    histo /= height_*width_;
  return histo;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RGBImage::display ( ) const

Definition at line 180 of file RGBImage.cc.

References savePPM(), and tmpfilename.

{
  savePPM(tmpfilename);
  char command[1000];
  sprintf(command,"xv %s &",tmpfilename);
  system(command);
}

Here is the call graph for this function:

void PLearn::RGBImage::displayAndWait ( ) const

Definition at line 200 of file RGBImage.cc.

References savePPM(), and tmpfilename.

{
  savePPM(tmpfilename);
  char command[1000];
  sprintf(command,"xv %s",tmpfilename);
  system(command);
  unlink(tmpfilename);
}

Here is the call graph for this function:

void PLearn::RGBImage::fill ( RGB  color)

Definition at line 128 of file RGBImage.cc.

References height_, PLERROR, storage, and width_.

{
  if(!storage)
    PLERROR("Empty RGBImage");
  RGB* data = storage->data;
  for(int pos=0; pos<height_*width_; pos++)
    data[pos] = color;
}
RGB PLearn::RGBImage::getPixel ( int  i,
int  j 
) const [inline]

return RGB value of Pixel of row i and column j

Definition at line 153 of file RGBImage.h.

References j, and PLERROR.

Referenced by blit(), computeHistogram(), and PLearn::RGBImageVMatrix::sample().

{
#ifdef BOUNDCHECK
  if(!storage)
    PLERROR("In RGBImage::getPixeel(int i, int j) EMPTY IMAGE!!!");
  if(i<0 || j<0 || i>=height_ || j>=width_)
    PLERROR("In RGBImage::getPixel(int i, int j) OUT OF IMAGE BOUNDS");
#endif
  return storage->data[i*width_+j];
}

Here is the caller graph for this function:

int PLearn::RGBImage::height ( ) const [inline]

Definition at line 118 of file RGBImage.h.

Referenced by PLearn::RGBImageVMatrix::RGBImageVMatrix(), and PLearn::RGBImageVMatrix::setImage().

{ return height_; }

Here is the caller graph for this function:

void PLearn::RGBImage::loadJPEG ( const char *  filename,
int  scale = 1 
)

Definition at line 189 of file RGBImage.cc.

References PLearn::endl(), loadPPM(), and tmpfilename.

Referenced by PLearn::RGBImageDB::getImage().

{
  char command[1000];
  sprintf(command,"djpeg -pnm -scale 1/%d %s > %s", scale, filename, tmpfilename);
  system(command);
  cout << command << endl;
  loadPPM(tmpfilename);
  unlink(tmpfilename);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RGBImage::loadPPM ( const char *  filename)

Definition at line 138 of file RGBImage.cc.

References height_, in, PLERROR, PLWARNING, resize(), storage, and width_.

Referenced by loadJPEG().

{
  int ncolors;
  ifstream in(filename);
  if(!in)
    PLERROR("In RGBImage::loadPPM could not open file %s for reading",filename);

  char imagetype[20];
  int newheight, newwidth;
  in >> imagetype >> newwidth >> newheight >> ncolors;
  char tmp[100];
  in.getline(tmp,99); // skip until end of line
  if(strcmp(imagetype,"P6")!=0)
    PLERROR("In RGBImage::loadPPM unsupported format (not a P6 PPM)");
  resize(newheight, newwidth);
  RGB* data = storage->data;
  PLWARNING("In RGBImage::loadPPM - Code has been changed to compile, but hasn't been tested (remove this warning if it works)");
  // The following line:
  //  in.read((unsigned char*)data,height_*width_*3);
  // has been replaced by:
  in.read((char*)data,height_*width_*3);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RGBImage::removeBorders ( int  border) [inline]

remove borders of thickness "border" from image (in-place), on all sides

Definition at line 137 of file RGBImage.h.

References removeBorders().

Referenced by removeBorders().

{ removeBorders(border,border,border,border); }

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RGBImage::removeBorders ( int  top_border,
int  bottom_border,
int  left_border,
int  right_border 
)

remove borders around the image

Definition at line 103 of file RGBImage.cc.

References c, d, height_, PLERROR, storage, and width_.

Referenced by PLearn::RGBImageDB::getImage().

{
  if(!storage)
    PLERROR("Empty RGBImage");
  int newwidth = width_ - (left_border + right_border);
  int newheight = height_ - (top_border + bottom_border);
  if (newheight < 1)
    PLERROR("RGBImage::removeBorders(%d,%d,%d,%d): can't remove more than height_(%d)",
          top_border,bottom_border,left_border,right_border,height_);
  if (newwidth < 1)
    PLERROR("RGBImage::removeBorders(%d,%d,%d,%d): can't remove more than width_(%d)",
          top_border,bottom_border,left_border,right_border,width_);
  PP< Storage<RGB> > newstorage = new Storage<RGB>(newheight*newwidth);
  RGB* d= newstorage->data;
  RGB* data = storage->data;
  for (int r=0;r<newheight;r++)
    for (int c=0;c<newwidth;c++,d++)
      *d = data[(top_border+r)*width_+(left_border+c)];
  height_=newheight;
  width_=newwidth;
  storage = newstorage;
}

Here is the caller graph for this function:

void PLearn::RGBImage::resize ( int  newheight,
int  newwidth 
)

Definition at line 92 of file RGBImage.cc.

References height_, storage, and width_.

Referenced by loadPPM().

{
  if(storage)
    storage->resize(newheight*newwidth);
  else
    storage = new Storage<RGB>(newheight*newwidth);
  height_ = newheight;
  width_ = newwidth;
}

Here is the caller graph for this function:

void PLearn::RGBImage::savePPM ( const char *  filename) const

Definition at line 162 of file RGBImage.cc.

References PLearn::endl(), height_, PLERROR, PLWARNING, storage, and width_.

Referenced by display(), and displayAndWait().

{
  if(!storage)
    PLERROR("Empty RGBImage");
  RGB* data = storage->data;
  ofstream out(filename);
  if(!out)
    PLERROR("In RGBImage::savePPM could not open file for writing");
  out << "P6\n" << width_ << ' ' << height_ << ' ' << 255 << endl;
  PLWARNING("In RGBImage::loadPPM - Code has been changed to compile, but hasn't been tested (remove this warning if it works)");
  // The following line:
  //  out.write((unsigned char*)data, height_*width_*3);
  // has been replaced by:
  out.write((char*)data, height_*width_*3);
  out.flush();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::RGBImage::setPixel ( int  i,
int  j,
RGB  value 
) [inline]

sets RGB value of pixel in row i and column j

Definition at line 165 of file RGBImage.h.

References j, and PLERROR.

Referenced by blit(), and RGBImage().

{
#ifdef BOUNDCHECK
  if(!storage)
    PLERROR("In RGBImage::getPixeel(int i, int j) EMPTY IMAGE!!!");
  if(i<0 || j<0 || i>=height_ || j>=width_)
    PLERROR("In RGBImage::getPixel(int i, int j) OUT OF IMAGE BOUNDS");
#endif
  storage->data[i*width_+j] = value;
}

Here is the caller graph for this function:

void PLearn::RGBImage::shrinkToIntersection ( int i,
int j,
int h,
int w 
) const

shrinks the box that starts at position (i,j) (in this image) and of size (h,w) to its intersection with this image's box (0,0,this->height,this->width_)

Definition at line 210 of file RGBImage.cc.

References height_, MAX, MIN, and width_.

Referenced by blit().

{
  int istart = MAX(0,i);
  int iend = MIN(i+h,height_);
  int jstart = MAX(0,j);
  int jend = MIN(j+w,width_);

  i = istart;
  j = jstart;
  h = MAX(iend-istart,0);
  w = MAX(jend-jstart,0);
}

Here is the caller graph for this function:

int PLearn::RGBImage::width ( ) const [inline]

Definition at line 117 of file RGBImage.h.

Referenced by PLearn::RGBImageVMatrix::RGBImageVMatrix(), and PLearn::RGBImageVMatrix::setImage().

{ return width_; }

Here is the caller graph for this function:


Member Data Documentation

Definition at line 109 of file RGBImage.h.

Referenced by computeAverage(), fill(), loadPPM(), removeBorders(), resize(), RGBImage(), and savePPM().

char PLearn::RGBImage::tmpfilename[100] [protected]

temporary file in which the image may be saved when displayed

Definition at line 110 of file RGBImage.h.

Referenced by display(), displayAndWait(), loadJPEG(), RGBImage(), and ~RGBImage().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines