ColorExtractor.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <highgui.h>
00011 #include "ColorExtractor.h"
00012 #include "hrpsys/util/VectorConvert.h"
00013 
00014 // Module specification
00015 // <rtc-template block="module_spec">
00016 static const char* spec[] =
00017   {
00018     "implementation_id", "ColorExtractor",
00019     "type_name",         "ColorExtractor",
00020     "description",       "rotate image component",
00021     "version",           HRPSYS_PACKAGE_VERSION,
00022     "vendor",            "AIST",
00023     "category",          "example",
00024     "activity_type",     "DataFlowComponent",
00025     "max_instance",      "10",
00026     "language",          "C++",
00027     "lang_type",         "compile",
00028     // Configuration variables
00029     "conf.default.minPixels", "0",
00030     "conf.default.rgbRegion", "0,0,0,0,0,0",
00031 
00032     ""
00033   };
00034 // </rtc-template>
00035 
00036 ColorExtractor::ColorExtractor(RTC::Manager* manager)
00037   : RTC::DataFlowComponentBase(manager),
00038     // <rtc-template block="initializer">
00039     m_originalIn("original",  m_original),
00040     m_resultOut("result", m_result),
00041     m_posOut("pos", m_pos),
00042     // </rtc-template>
00043     m_img(NULL),
00044     dummy(0)
00045 {
00046 }
00047 
00048 ColorExtractor::~ColorExtractor()
00049 {
00050   if (m_img) cvReleaseImage(&m_img);
00051 
00052 }
00053 
00054 
00055 
00056 RTC::ReturnCode_t ColorExtractor::onInitialize()
00057 {
00058   std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00059   // <rtc-template block="bind_config">
00060   // Bind variables and configuration variable
00061   bindParameter("minPixels", m_minPixels, "0");
00062   bindParameter("rgbRegion", m_rgbRegion, "0,0,0,0,0,0");
00063   
00064   // </rtc-template>
00065 
00066   // Registration: InPort/OutPort/Service
00067   // <rtc-template block="registration">
00068   // Set InPort buffers
00069   addInPort("original", m_originalIn);
00070 
00071   // Set OutPort buffer
00072   addOutPort("result", m_resultOut);
00073   
00074   // Set service provider to Ports
00075   
00076   // Set service consumers to Ports
00077   
00078   // Set CORBA Service Ports
00079   
00080   // </rtc-template>
00081 
00082   //RTC::Properties& prop = getProperties();
00083 
00084   return RTC::RTC_OK;
00085 }
00086 
00087 
00088 
00089 /*
00090 RTC::ReturnCode_t ColorExtractor::onFinalize()
00091 {
00092   return RTC::RTC_OK;
00093 }
00094 */
00095 
00096 /*
00097 RTC::ReturnCode_t ColorExtractor::onStartup(RTC::UniqueId ec_id)
00098 {
00099   return RTC::RTC_OK;
00100 }
00101 */
00102 
00103 /*
00104 RTC::ReturnCode_t ColorExtractor::onShutdown(RTC::UniqueId ec_id)
00105 {
00106   return RTC::RTC_OK;
00107 }
00108 */
00109 
00110 RTC::ReturnCode_t ColorExtractor::onActivated(RTC::UniqueId ec_id)
00111 {
00112   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00113   return RTC::RTC_OK;
00114 }
00115 
00116 RTC::ReturnCode_t ColorExtractor::onDeactivated(RTC::UniqueId ec_id)
00117 {
00118   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00119   return RTC::RTC_OK;
00120 }
00121 
00122 RTC::ReturnCode_t ColorExtractor::onExecute(RTC::UniqueId ec_id)
00123 {
00124     //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00125   if (m_originalIn.isNew()){
00126       m_originalIn.read();
00127 
00128       Img::ImageData& idat = m_original.data.image;
00129 
00130       if (m_img && (m_img->width != idat.width
00131                     || m_img->height != idat.height)){
00132         cvReleaseImage(&m_img);
00133         m_img = NULL;
00134       }
00135       if (!m_img){
00136         m_img = cvCreateImage(cvSize(idat.width, idat.height), 
00137                               IPL_DEPTH_8U, 3);
00138         m_result.data.image.width  = idat.width;
00139         m_result.data.image.height = idat.height;
00140         m_result.data.image.format = idat.format;
00141         m_result.data.image.raw_data.length(idat.width*idat.height*3);
00142       }
00143 
00144       // RGB -> BGR
00145       unsigned char *rtm=idat.raw_data.get_buffer();
00146       char *cv;
00147       for (int i=0; i<idat.height; i++){
00148         for (int j=0; j<idat.width; j++){
00149           cv = m_img->imageData + m_img->widthStep*i + j*3;
00150           cv[0] = rtm[2];
00151           cv[1] = rtm[1];
00152           cv[2] = rtm[0];
00153           rtm += 3;
00154         }
00155       }
00156       
00157       // processing start
00158       int npixel=0, cx=0, cy=0;
00159       unsigned char b,g,r;
00160       for (int i=0; i<idat.height; i++){
00161         for (int j=0; j<idat.width; j++){
00162           cv = m_img->imageData + m_img->widthStep*i + j*3;
00163           b = cv[0]; g = cv[1]; r = cv[2];
00164           if (r > m_rgbRegion[0] && g > m_rgbRegion[1] && b > m_rgbRegion[2]
00165               && r < m_rgbRegion[3] && g < m_rgbRegion[4] && b < m_rgbRegion[5]){
00166             cx += j;
00167             cy += i;
00168             npixel++;
00169           }else{
00170           }
00171         }
00172       }
00173       if (npixel > 10){
00174         cx /= npixel;
00175         cy /= npixel;
00176         //printf("cx=%d, cy=%d, npixel=%d\n", cx, cy, npixel);
00177         cvCircle(m_img, cvPoint(cx, cy), sqrt(npixel), CV_RGB(0,0,255), 6, 8, 0);
00178         m_pos.tm = m_original.tm;
00179         m_pos.data.x = cx;
00180         m_pos.data.y = cy;
00181         m_posOut.write();
00182       }
00183       // processing end
00184       
00185       // BGR -> RGB
00186       rtm = m_result.data.image.raw_data.get_buffer();
00187       for (int i=0; i<idat.height; i++){
00188         for (int j=0; j<idat.width; j++){
00189           cv = m_img->imageData + m_img->widthStep*i + j*3;
00190           rtm[2] = cv[0]; 
00191           rtm[1] = cv[1]; 
00192           rtm[0] = cv[2];
00193           rtm += 3;
00194         }
00195       }
00196 
00197       m_resultOut.write();
00198   }
00199   return RTC::RTC_OK;
00200 }
00201 
00202 /*
00203 RTC::ReturnCode_t ColorExtractor::onAborting(RTC::UniqueId ec_id)
00204 {
00205   return RTC::RTC_OK;
00206 }
00207 */
00208 
00209 /*
00210 RTC::ReturnCode_t ColorExtractor::onError(RTC::UniqueId ec_id)
00211 {
00212   return RTC::RTC_OK;
00213 }
00214 */
00215 
00216 /*
00217 RTC::ReturnCode_t ColorExtractor::onReset(RTC::UniqueId ec_id)
00218 {
00219   return RTC::RTC_OK;
00220 }
00221 */
00222 
00223 /*
00224 RTC::ReturnCode_t ColorExtractor::onStateUpdate(RTC::UniqueId ec_id)
00225 {
00226   return RTC::RTC_OK;
00227 }
00228 */
00229 
00230 /*
00231 RTC::ReturnCode_t ColorExtractor::onRateChanged(RTC::UniqueId ec_id)
00232 {
00233   return RTC::RTC_OK;
00234 }
00235 */
00236 
00237 
00238 
00239 extern "C"
00240 {
00241 
00242   void ColorExtractorInit(RTC::Manager* manager)
00243   {
00244     RTC::Properties profile(spec);
00245     manager->registerFactory(profile,
00246                              RTC::Create<ColorExtractor>,
00247                              RTC::Delete<ColorExtractor>);
00248   }
00249 
00250 };
00251 
00252 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:17