rtcImage.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Robert Bosch LLC
00004  * Research and Technology Center North America
00005  * Palo Alto, California
00006  *
00007  * All rights reserved.
00008  *
00009  *------------------------------------------------------------------------------
00010  * project ....: PUMA: Probablistic Unsupervised Model Acquisition
00011  * file .......: Image.cpp
00012  * authors ....: Benjamin Pitzer
00013  * organization: Robert Bosch LLC
00014  * creation ...: 10/13/2006
00015  * modified ...: $Date: 2009-03-11 10:01:02 -0700 (Wed, 11 Mar 2009) $
00016  * changed by .: $Author: wg75pal $
00017  * revision ...: $Revision: 825 $
00018  */
00019 
00020 //== INCLUDES ==================================================================
00021 //#include <opencv/cv.h>
00022 //#include <opencv/highgui.h> //for cvLoadImage()
00023 #include "rtc/rtcImage.h"
00024 
00025 //== NAMESPACES ================================================================
00026 namespace rtc {
00027 
00028 //== IMPLEMENTATION ============================================================
00029 template <>
00030 bool Image<Vec3uc>::readFromFile(const char* filename)
00031 {
00032   cv::Mat img = cv::imread(filename);
00033   if(img.empty()) return false;
00034   int w = img.cols;
00035   int h = img.rows;
00036   setSize(h,w);
00037   for(int r=0;r<h;++r) 
00038   {
00039     for(int c=0;c<w;++c) 
00040     {
00041       x[r*w+c][0] = img.at<Vec3b> (r, c)[2]; // R
00042       x[r*w+c][1] = img.at<Vec3b> (r, c)[1]; // G
00043       x[r*w+c][2] = img.at<Vec3b> (r, c)[0]; // B
00044     }
00045   }
00046   return true;
00047 }
00048 
00049 template <>
00050 bool Image<Vec4uc>::readFromFile(const char* filename)
00051 {
00052   cv::Mat img = cv::imread(filename);
00053   if(img.empty()) return false;
00054   int w = img.cols;
00055   int h = img.rows;
00056   setSize(h,w);
00057   for(int r=0;r<h;++r) 
00058   {
00059     for(int c=0;c<w;++c) 
00060     {
00061       x[r*w+c][0] = img.at<Vec3b> (r, c)[2]; // R
00062       x[r*w+c][1] = img.at<Vec3b> (r, c)[1]; // G
00063       x[r*w+c][2] = img.at<Vec3b> (r, c)[0]; // B
00064       x[r*w+c][3] = (uchar)(255);
00065     }
00066   }
00067   return true;
00068 }
00069 
00070 template <>
00071 bool Image<float>::readFromFile(const char* filename)
00072 {
00073   cv::Mat img = cv::imread(filename);
00074   if(img.empty()) return false;
00075   int w = img.cols;
00076   int h = img.rows;
00077   setSize(h,w);
00078   for(int r=0;r<h;++r) 
00079   {
00080     for(int c=0;c<w;++c) 
00081     {
00082       uchar R = img.at<Vec3b> (r, c)[2]; // R
00083       uchar G = img.at<Vec3b> (r, c)[1]; // G
00084       uchar B = img.at<Vec3b> (r, c)[0]; // B
00085       x[r*w+c] = (R*0.299+G*0.587+B*0.114)/256.0;
00086     }
00087   }
00088   return true;
00089 }
00090 
00091 template <>
00092 bool Image<unsigned char>::readFromFile(const char* filename)
00093 {
00094   cv::Mat img = cv::imread(filename);
00095   if(img.empty()) return false;
00096   int w = img.cols;
00097   int h = img.rows;
00098   setSize(h,w);
00099   for(int r=0;r<h;++r) 
00100   {
00101     for(int c=0;c<w;++c) 
00102     {
00103       uchar R = img.at<Vec3b> (r, c)[2]; // R
00104       uchar G = img.at<Vec3b> (r, c)[1]; // G
00105       uchar B = img.at<Vec3b> (r, c)[0]; // B
00106       x[r*w+c] = R*0.299+G*0.587+B*0.114;
00107     }
00108   }
00109   return true;
00110 }
00111 
00112 template <>
00113 bool Image<Vec3uc>::writeToFile(const char* filename) const
00114 {
00115   int w = columns();
00116   int h = rows();
00117   cv::Mat image (h, w, CV_8UC3);
00118   for(int r=0;r<h;++r) 
00119   {
00120     for(int c=0;c<w;++c) 
00121     {
00122       image.at<unsigned char> (r, 3*c+2) = x[r*w+c][0]; // R
00123       image.at<unsigned char> (r, 3*c+1) = x[r*w+c][1]; // G
00124       image.at<unsigned char> (r, 3*c) = x[r*w+c][2]; // B
00125     }
00126   }
00127   cv::imwrite(filename, image);
00128   return true;
00129 }
00130 
00131 template <>
00132 bool Image<Vec4uc>::writeToFile(const char* filename) const
00133 {
00134   int w = columns();
00135   int h = rows();
00136   //CvSize size = {w,h};
00137   //cv::Mat img (size,IPL_DEPTH_8U,4);
00138   cv::Mat img (h, w, CV_8UC4);
00139   for(int r=0;r<h;++r) 
00140   {
00141     for(int c=0;c<w;++c) 
00142     {
00143       img.at<Vec3b> (r, c)[2] = x[r*w+c][0]; // R
00144       img.at<Vec3b> (r, c)[1] = x[r*w+c][1]; // G
00145       img.at<Vec3b> (r, c)[0] = x[r*w+c][2]; // B
00146       img.at<Vec3b> (r, c)[4] = x[r*w+c][3]; // B
00147     }
00148   }
00149   cv::imwrite(filename, img);
00150   return true;
00151 }
00152 
00153 template <>
00154 bool Image<float>::writeToFile(const char* filename) const
00155 {
00156   int w = columns();
00157   int h = rows();
00158   CvSize size = {w,h};
00159   cv::Mat img (size,IPL_DEPTH_8U,1);
00160   for(int r=0;r<h;++r) 
00161   {
00162     for(int c=0;c<w;++c) 
00163     {
00164       img.at<float> (r, c) = rtc_clamp<int>(at(r,c)*256,0,255);
00165     }
00166   }
00167   return true;
00168 }
00169 
00170 template <>
00171 bool Image<unsigned char>::writeToFile(const char* filename) const
00172 {
00173   int w = columns();
00174   int h = rows();
00175   CvSize size = {w,h};
00176   cv::Mat img (size,IPL_DEPTH_8U,1);
00177   for(int r=0;r<h;++r) 
00178   {
00179     for(int c=0;c<w;++c) 
00180     {
00181       img.at<unsigned char> (r, c) = at(r,c);
00182     }
00183   }
00184   return true;
00185 }
00190 template <>
00191 Image<Vec3uc>::Pixel Image<Vec3uc>::interpolate(const float row, const float col) const
00192 {
00193   const int truncR = rtc_clamp(static_cast<int>(row),0,rows()-1);
00194   const int truncR1 = rtc_clamp(truncR+1,0,rows()-1);
00195   const float fractR = row - static_cast<float>(truncR);
00196   const int truncC = rtc_clamp(static_cast<int>(col),0,columns()-1);
00197   const int truncC1 = rtc_clamp(truncC+1,0,columns()-1);
00198   const float fractC = col - static_cast<float>(truncC);
00199 
00200   // do the interpolation
00201   const Vec3f syx = at(truncR,truncC);
00202   const Vec3f syx1 = at(truncR,truncC1);
00203   const Vec3f sy1x = at(truncR1,truncC);
00204   const Vec3f sy1x1 = at(truncR1,truncC1);
00205   // normal interpolation within range
00206   const Vec3f tmp1 = syx  + (syx1-syx)*fractC;
00207   const Vec3f tmp2 = sy1x + (sy1x1-sy1x)*fractC;
00208   return (tmp1 + (tmp2-tmp1)*fractR);
00209 }
00210 
00211 template <>
00212 Image<Vec4uc>::Pixel Image<Vec4uc>::interpolate(const float row, const float col) const
00213 {
00214   const int truncR = rtc_clamp(static_cast<int>(row),0,rows()-1);
00215   const int truncR1 = rtc_clamp(truncR+1,0,rows()-1);
00216   const float fractR = row - static_cast<float>(truncR);
00217   const int truncC = rtc_clamp(static_cast<int>(col),0,columns()-1);
00218   const int truncC1 = rtc_clamp(truncC+1,0,columns()-1);
00219   const float fractC = col - static_cast<float>(truncC);
00220 
00221   // do the interpolation
00222   const Vec4f syx = at(truncR,truncC);
00223   const Vec4f syx1 = at(truncR,truncC1);
00224   const Vec4f sy1x = at(truncR1,truncC);
00225   const Vec4f sy1x1 = at(truncR1,truncC1);
00226   // normal interpolation within range
00227   const Vec4f tmp1 = syx  + (syx1-syx)*fractC;
00228   const Vec4f tmp2 = sy1x + (sy1x1-sy1x)*fractC;
00229   return (tmp1 + (tmp2-tmp1)*fractR);
00230 }
00231 
00232 template <>
00233 Image<float>::Pixel Image<float>::interpolate(const float row, const float col) const
00234 {
00235   const int truncR = rtc_clamp(static_cast<int>(row),0,rows()-1);
00236   const int truncR1 = rtc_clamp(truncR+1,0,rows()-1);
00237   const float fractR = row - static_cast<float>(truncR);
00238   const int truncC = rtc_clamp(static_cast<int>(col),0,columns()-1);
00239   const int truncC1 = rtc_clamp(truncC+1,0,columns()-1);
00240   const float fractC = col - static_cast<float>(truncC);
00241 
00242   // do the interpolation
00243   const float syx = at(truncR,truncC);
00244   const float syx1 = at(truncR,truncC1);
00245   const float sy1x = at(truncR1,truncC);
00246   const float sy1x1 = at(truncR1,truncC1);
00247   // normal interpolation within range
00248   const float tmp1 = syx  + (syx1-syx)*fractC;
00249   const float tmp2 = sy1x + (sy1x1-sy1x)*fractC;
00250   return (tmp1 + (tmp2-tmp1)*fractR);
00251 }
00252 
00253 template <>
00254 Image<unsigned char>::Pixel Image<unsigned char>::interpolate(const float row, const float col) const
00255 {
00256   const int truncR = rtc_clamp(static_cast<int>(row),0,rows()-1);
00257   const int truncR1 = rtc_clamp(truncR+1,0,rows()-1);
00258   const float fractR = row - static_cast<float>(truncR);
00259   const int truncC = rtc_clamp(static_cast<int>(col),0,columns()-1);
00260   const int truncC1 = rtc_clamp(truncC+1,0,columns()-1);
00261   const float fractC = col - static_cast<float>(truncC);
00262 
00263   // do the interpolation
00264   const float syx = at(truncR,truncC);
00265   const float syx1 = at(truncR,truncC1);
00266   const float sy1x = at(truncR1,truncC);
00267   const float sy1x1 = at(truncR1,truncC1);
00268   // normal interpolation within range
00269   const float tmp1 = syx  + (syx1-syx)*fractC;
00270   const float tmp2 = sy1x + (sy1x1-sy1x)*fractC;
00271   return (unsigned char)(tmp1 + (tmp2-tmp1)*fractR);
00272 }
00273 
00274 template <>
00275 bool Image<Vec3uc>::fromOpenCV(const cv::Mat& image)
00276 {
00277   int w = image.cols;
00278   int h = image.rows;
00279   setSize(h,w);
00280   for(int r=0;r<h;++r) 
00281   {
00282     for(int c=0;c<w;++c) 
00283     {
00284       x[r*w+c][0] = image.at<Vec3b> (r, c)[2]; // R
00285       x[r*w+c][1] = image.at<Vec3b> (r, c)[1]; // G
00286       x[r*w+c][2] = image.at<Vec3b> (r, c)[0]; // B
00287     }
00288   }
00289   return true;
00290 }
00291 
00292 template <>
00293 bool Image<Vec4uc>::fromOpenCV(const cv::Mat& image)
00294 {
00295   int w = image.cols;
00296   int h = image.rows;
00297   setSize(h,w);
00298   for(int r=0;r<h;++r) 
00299   {
00300     for(int c=0;c<w;++c) 
00301     {
00302       x[r*w+c][0] = image.at<Vec3b> (r, c)[2]; // R
00303       x[r*w+c][1] = image.at<Vec3b> (r, c)[1]; // G
00304       x[r*w+c][2] = image.at<Vec3b> (r, c)[0]; // B
00305       x[r*w+c][3] = (uchar)(255);
00306     }
00307   }
00308   return true;
00309 }
00310 
00311   //specialization class
00312 template <>
00313 bool Image<Vec3uc>::toOpenCV(cv::Mat& image) const
00314 {
00315   int w = columns();
00316   int h = rows();
00317   image.create(h, w, CV_8UC3);
00318   for(int r=0;r<h;++r) 
00319   {
00320     for(int c=0;c<w;++c) 
00321     {
00322       image.at<unsigned char> (r, 3*c+2) = x[r*w+c][0]; // R
00323       image.at<unsigned char> (r, 3*c+1) = x[r*w+c][1]; // G
00324       image.at<unsigned char> (r, 3*c) = x[r*w+c][2]; // B
00325     }
00326   }
00327   return true;
00328 }
00329 //==============================================================================
00330 } // namespace rtc
00331 //==============================================================================


rtc
Author(s): Benjamin Pitzer
autogenerated on Mon Oct 6 2014 10:07:34