ColorExtractor.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <highgui.h>
11 #include "ColorExtractor.h"
12 #include "hrpsys/util/VectorConvert.h"
13 
14 // Module specification
15 // <rtc-template block="module_spec">
16 static const char* spec[] =
17  {
18  "implementation_id", "ColorExtractor",
19  "type_name", "ColorExtractor",
20  "description", "rotate image component",
21  "version", HRPSYS_PACKAGE_VERSION,
22  "vendor", "AIST",
23  "category", "example",
24  "activity_type", "DataFlowComponent",
25  "max_instance", "10",
26  "language", "C++",
27  "lang_type", "compile",
28  // Configuration variables
29  "conf.default.minPixels", "0",
30  "conf.default.rgbRegion", "0,0,0,0,0,0",
31 
32  ""
33  };
34 // </rtc-template>
35 
37  : RTC::DataFlowComponentBase(manager),
38  // <rtc-template block="initializer">
39  m_originalIn("original", m_original),
40  m_resultOut("result", m_result),
41  m_posOut("pos", m_pos),
42  // </rtc-template>
43  m_img(NULL),
44  dummy(0)
45 {
46 }
47 
49 {
50  if (m_img) cvReleaseImage(&m_img);
51 
52 }
53 
54 
55 
56 RTC::ReturnCode_t ColorExtractor::onInitialize()
57 {
58  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
59  // <rtc-template block="bind_config">
60  // Bind variables and configuration variable
61  bindParameter("minPixels", m_minPixels, "0");
62  bindParameter("rgbRegion", m_rgbRegion, "0,0,0,0,0,0");
63 
64  // </rtc-template>
65 
66  // Registration: InPort/OutPort/Service
67  // <rtc-template block="registration">
68  // Set InPort buffers
69  addInPort("original", m_originalIn);
70 
71  // Set OutPort buffer
72  addOutPort("result", m_resultOut);
73 
74  // Set service provider to Ports
75 
76  // Set service consumers to Ports
77 
78  // Set CORBA Service Ports
79 
80  // </rtc-template>
81 
82  //RTC::Properties& prop = getProperties();
83 
84  return RTC::RTC_OK;
85 }
86 
87 
88 
89 /*
90 RTC::ReturnCode_t ColorExtractor::onFinalize()
91 {
92  return RTC::RTC_OK;
93 }
94 */
95 
96 /*
97 RTC::ReturnCode_t ColorExtractor::onStartup(RTC::UniqueId ec_id)
98 {
99  return RTC::RTC_OK;
100 }
101 */
102 
103 /*
104 RTC::ReturnCode_t ColorExtractor::onShutdown(RTC::UniqueId ec_id)
105 {
106  return RTC::RTC_OK;
107 }
108 */
109 
111 {
112  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
113  return RTC::RTC_OK;
114 }
115 
117 {
118  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
119  return RTC::RTC_OK;
120 }
121 
122 RTC::ReturnCode_t ColorExtractor::onExecute(RTC::UniqueId ec_id)
123 {
124  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
125  if (m_originalIn.isNew()){
126  m_originalIn.read();
127 
128  Img::ImageData& idat = m_original.data.image;
129 
130  if (m_img && (m_img->width != idat.width
131  || m_img->height != idat.height)){
132  cvReleaseImage(&m_img);
133  m_img = NULL;
134  }
135  if (!m_img){
136  m_img = cvCreateImage(cvSize(idat.width, idat.height),
137  IPL_DEPTH_8U, 3);
138  m_result.data.image.width = idat.width;
139  m_result.data.image.height = idat.height;
140  m_result.data.image.format = idat.format;
141  m_result.data.image.raw_data.length(idat.width*idat.height*3);
142  }
143 
144  // RGB -> BGR
145  unsigned char *rtm=idat.raw_data.get_buffer();
146  char *cv;
147  for (int i=0; i<idat.height; i++){
148  for (int j=0; j<idat.width; j++){
149  cv = m_img->imageData + m_img->widthStep*i + j*3;
150  cv[0] = rtm[2];
151  cv[1] = rtm[1];
152  cv[2] = rtm[0];
153  rtm += 3;
154  }
155  }
156 
157  // processing start
158  int npixel=0, cx=0, cy=0;
159  unsigned char b,g,r;
160  for (int i=0; i<idat.height; i++){
161  for (int j=0; j<idat.width; j++){
162  cv = m_img->imageData + m_img->widthStep*i + j*3;
163  b = cv[0]; g = cv[1]; r = cv[2];
164  if (r > m_rgbRegion[0] && g > m_rgbRegion[1] && b > m_rgbRegion[2]
165  && r < m_rgbRegion[3] && g < m_rgbRegion[4] && b < m_rgbRegion[5]){
166  cx += j;
167  cy += i;
168  npixel++;
169  }else{
170  }
171  }
172  }
173  if (npixel > 10){
174  cx /= npixel;
175  cy /= npixel;
176  //printf("cx=%d, cy=%d, npixel=%d\n", cx, cy, npixel);
177  cvCircle(m_img, cvPoint(cx, cy), sqrt(npixel), CV_RGB(0,0,255), 6, 8, 0);
178  m_pos.tm = m_original.tm;
179  m_pos.data.x = cx;
180  m_pos.data.y = cy;
181  m_posOut.write();
182  }
183  // processing end
184 
185  // BGR -> RGB
186  rtm = m_result.data.image.raw_data.get_buffer();
187  for (int i=0; i<idat.height; i++){
188  for (int j=0; j<idat.width; j++){
189  cv = m_img->imageData + m_img->widthStep*i + j*3;
190  rtm[2] = cv[0];
191  rtm[1] = cv[1];
192  rtm[0] = cv[2];
193  rtm += 3;
194  }
195  }
196 
197  m_resultOut.write();
198  }
199  return RTC::RTC_OK;
200 }
201 
202 /*
203 RTC::ReturnCode_t ColorExtractor::onAborting(RTC::UniqueId ec_id)
204 {
205  return RTC::RTC_OK;
206 }
207 */
208 
209 /*
210 RTC::ReturnCode_t ColorExtractor::onError(RTC::UniqueId ec_id)
211 {
212  return RTC::RTC_OK;
213 }
214 */
215 
216 /*
217 RTC::ReturnCode_t ColorExtractor::onReset(RTC::UniqueId ec_id)
218 {
219  return RTC::RTC_OK;
220 }
221 */
222 
223 /*
224 RTC::ReturnCode_t ColorExtractor::onStateUpdate(RTC::UniqueId ec_id)
225 {
226  return RTC::RTC_OK;
227 }
228 */
229 
230 /*
231 RTC::ReturnCode_t ColorExtractor::onRateChanged(RTC::UniqueId ec_id)
232 {
233  return RTC::RTC_OK;
234 }
235 */
236 
237 
238 
239 extern "C"
240 {
241 
243  {
245  manager->registerFactory(profile,
246  RTC::Create<ColorExtractor>,
247  RTC::Delete<ColorExtractor>);
248  }
249 
250 };
251 
252 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
Img::TimedCameraImage m_original
rotate image component
InPort< Img::TimedCameraImage > m_originalIn
OutPort< Img::TimedCameraImage > m_resultOut
OutPort< RTC::TimedPoint2D > m_posOut
Img::TimedCameraImage m_result
png_uint_32 i
bool addOutPort(const char *name, OutPortBase &outport)
RTC::TimedPoint2D m_pos
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
def j(str, encoding="cp932")
std::vector< int > m_rgbRegion
void ColorExtractorInit(RTC::Manager *manager)
ColorExtractor(RTC::Manager *manager)
Constructor.
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onInitialize()
virtual bool isNew()
virtual bool write(DataType &value)
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
bool addInPort(const char *name, InPortBase &inport)
Definition: jython/rtm.py:1
static const char * spec[]
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
virtual ~ColorExtractor()
Destructor.
IplImage * m_img


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:49