CameraImageSaver.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <opencv2/core/core_c.h>
11 #include <opencv2/highgui/highgui_c.h>
12 #ifndef CV_VERSION_EPOCH
13  #define CV_VERSION_EPOCH CV_VERSION_MAJOR
14 #endif
15 #if CV_VERSION_EPOCH > 3
16 #include <opencv2/imgcodecs/imgcodecs.hpp>
17 #else
18 #include <opencv2/highgui/highgui.hpp>
19 #endif
20 #include "CameraImageSaver.h"
21 
22 // Module specification
23 // <rtc-template block="module_spec">
24 static const char* spec[] =
25  {
26  "implementation_id", "CameraImageSaver",
27  "type_name", "CameraImageSaver",
28  "description", "camera image saver",
29  "version", HRPSYS_PACKAGE_VERSION,
30  "vendor", "AIST",
31  "category", "example",
32  "activity_type", "DataFlowComponent",
33  "max_instance", "10",
34  "language", "C++",
35  "lang_type", "compile",
36  // Configuration variables
37  "conf.defalt.basename", "image",
38 
39  ""
40  };
41 // </rtc-template>
42 
44  : RTC::DataFlowComponentBase(manager),
45  // <rtc-template block="initializer">
46  m_imageIn("image", m_image),
47  // </rtc-template>
48  m_count(0),
49  dummy(0)
50 {
51 }
52 
54 {
55 }
56 
57 
58 
59 RTC::ReturnCode_t CameraImageSaver::onInitialize()
60 {
61  //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
62  // <rtc-template block="bind_config">
63  // Bind variables and configuration variable
64  bindParameter("basename", m_basename, "image");
65 
66  // </rtc-template>
67 
68  // Registration: InPort/OutPort/Service
69  // <rtc-template block="registration">
70  // Set InPort buffers
71  addInPort("image", m_imageIn);
72 
73  // Set OutPort buffer
74 
75  // Set service provider to Ports
76 
77  // Set service consumers to Ports
78 
79  // Set CORBA Service Ports
80 
81  // </rtc-template>
82 
84 
85  return RTC::RTC_OK;
86 }
87 
88 
89 
90 /*
91 RTC::ReturnCode_t CameraImageSaver::onFinalize()
92 {
93  return RTC::RTC_OK;
94 }
95 */
96 
97 /*
98 RTC::ReturnCode_t CameraImageSaver::onStartup(RTC::UniqueId ec_id)
99 {
100  return RTC::RTC_OK;
101 }
102 */
103 
104 /*
105 RTC::ReturnCode_t CameraImageSaver::onShutdown(RTC::UniqueId ec_id)
106 {
107  return RTC::RTC_OK;
108 }
109 */
110 
112 {
113  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
114  return RTC::RTC_OK;
115 }
116 
118 {
119  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
120  return RTC::RTC_OK;
121 }
122 
124 {
125  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
126  if (m_imageIn.isNew()){
127  m_imageIn.read();
128 
129  IplImage* cvImage;
130  switch (m_image.data.image.format){
131  case Img::CF_RGB:
132  cvImage = cvCreateImage(cvSize(m_image.data.image.width,
133  m_image.data.image.height),
134  IPL_DEPTH_8U, 3);
135  break;
136  case Img::CF_GRAY:
137  cvImage = cvCreateImage(cvSize(m_image.data.image.width,
138  m_image.data.image.height),
139  IPL_DEPTH_8U, 1);
140  break;
141  default:
142  std::cerr << "unsupported color format("
143  << m_image.data.image.format << ")" << std::endl;
144  return RTC::RTC_ERROR;
145  }
146  switch(m_image.data.image.format){
147  case Img::CF_RGB:
148  {
149  // RGB -> BGR
150  char *dst = cvImage->imageData;
151  for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
152  dst[i ] = m_image.data.image.raw_data[i+2];
153  dst[i+1] = m_image.data.image.raw_data[i+1];
154  dst[i+2] = m_image.data.image.raw_data[i ];
155  }
156  break;
157  }
158  case Img::CF_GRAY:
159  memcpy(cvImage->imageData,
160  m_image.data.image.raw_data.get_buffer(),
161  m_image.data.image.raw_data.length());
162  break;
163  default:
164  break;
165  }
166 
167  char fname[256];
168  sprintf(fname, "%s%04d.png", m_basename.c_str(), m_count++);
169  cv::Mat image = cv::cvarrToMat(cvImage);
170  cv::imwrite(fname, image);
171 
172  cvReleaseImage(&cvImage);
173  }
174 
175  return RTC::RTC_OK;
176 }
177 
178 /*
179 RTC::ReturnCode_t CameraImageSaver::onAborting(RTC::UniqueId ec_id)
180 {
181  return RTC::RTC_OK;
182 }
183 */
184 
185 /*
186 RTC::ReturnCode_t CameraImageSaver::onError(RTC::UniqueId ec_id)
187 {
188  return RTC::RTC_OK;
189 }
190 */
191 
192 /*
193 RTC::ReturnCode_t CameraImageSaver::onReset(RTC::UniqueId ec_id)
194 {
195  return RTC::RTC_OK;
196 }
197 */
198 
199 /*
200 RTC::ReturnCode_t CameraImageSaver::onStateUpdate(RTC::UniqueId ec_id)
201 {
202  return RTC::RTC_OK;
203 }
204 */
205 
206 /*
207 RTC::ReturnCode_t CameraImageSaver::onRateChanged(RTC::UniqueId ec_id)
208 {
209  return RTC::RTC_OK;
210 }
211 */
212 
213 
214 
215 extern "C"
216 {
217 
219  {
221  manager->registerFactory(profile,
222  RTC::Create<CameraImageSaver>,
223  RTC::Delete<CameraImageSaver>);
224  }
225 
226 };
227 
228 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual ~CameraImageSaver()
Destructor.
Img::TimedCameraImage m_image
void CameraImageSaverInit(RTC::Manager *manager)
png_uint_32 i
coil::Properties & getProperties()
png_bytepp image
std::string m_basename
ExecutionContextHandle_t UniqueId
CameraImageSaver(RTC::Manager *manager)
Constructor.
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
static const char * spec[]
InPort< Img::TimedCameraImage > m_imageIn
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
prop
std::string sprintf(char const *__restrict fmt,...)
virtual bool isNew()
virtual RTC::ReturnCode_t onInitialize()
bool addInPort(const char *name, InPortBase &inport)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
camera image saver
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Sat Dec 17 2022 03:52:20