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