CameraImageLoader.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #ifndef CV_VERSION_EPOCH
11  #define CV_VERSION_EPOCH CV_VERSION_MAJOR
12 #endif
13 #if CV_VERSION_EPOCH > 3
14 #include <opencv2/imgcodecs/imgcodecs.hpp>
15 #else
16 #include <opencv2/highgui/highgui.hpp>
17 #endif
18 #include "CameraImageLoader.h"
19 
20 // Module specification
21 // <rtc-template block="module_spec">
22 static const char* spec[] =
23  {
24  "implementation_id", "CameraImageLoader",
25  "type_name", "CameraImageLoader",
26  "description", "camera image loader",
27  "version", HRPSYS_PACKAGE_VERSION,
28  "vendor", "AIST",
29  "category", "example",
30  "activity_type", "DataFlowComponent",
31  "max_instance", "10",
32  "language", "C++",
33  "lang_type", "compile",
34  // Configuration variables
35 
36  ""
37  };
38 // </rtc-template>
39 
41  : RTC::DataFlowComponentBase(manager),
42  // <rtc-template block="initializer">
43  m_imageOut("image", m_image),
44  // </rtc-template>
45  dummy(0)
46 {
47 }
48 
50 {
51 }
52 
53 
54 
56 {
57  //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
58  // <rtc-template block="bind_config">
59  // Bind variables and configuration variable
60 
61  // </rtc-template>
62 
63  // Registration: InPort/OutPort/Service
64  // <rtc-template block="registration">
65  // Set InPort buffers
66 
67  // Set OutPort buffer
68  addOutPort("image", m_imageOut);
69 
70  // Set service provider to Ports
71 
72  // Set service consumers to Ports
73 
74  // Set CORBA Service Ports
75 
76  // </rtc-template>
77 
79 
80  return RTC::RTC_OK;
81 }
82 
83 
84 
85 /*
86 RTC::ReturnCode_t CameraImageLoader::onFinalize()
87 {
88  return RTC::RTC_OK;
89 }
90 */
91 
92 /*
93 RTC::ReturnCode_t CameraImageLoader::onStartup(RTC::UniqueId ec_id)
94 {
95  return RTC::RTC_OK;
96 }
97 */
98 
99 /*
100 RTC::ReturnCode_t CameraImageLoader::onShutdown(RTC::UniqueId ec_id)
101 {
102  return RTC::RTC_OK;
103 }
104 */
105 
107 {
108  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
109  return RTC::RTC_OK;
110 }
111 
113 {
114  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
115  return RTC::RTC_OK;
116 }
117 
119 {
120  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
121  std::cerr << "image filename: " << std::flush;
122  std::string filename;
123  std::cin >> filename;
124 
125  cv::Mat image = cv::imread(filename.c_str(), cv::IMREAD_COLOR);
126  if (image.empty()) {
127  std::cerr << m_profile.instance_name << ": failed to load("
128  << filename << ")" << std::endl;
129  return RTC::RTC_OK;
130  }
131 
132  m_image.data.image.width = image.size().width;
133  m_image.data.image.height = image.size().height;
134  m_image.data.image.raw_data.length(image.size().area());
135  switch(image.channels()){
136  case 3:
137  m_image.data.image.format = Img::CF_RGB;
138  {
139  // BGR -> RGB
140  unsigned char *src;
141  unsigned char *dst = m_image.data.image.raw_data.get_buffer();
142  for (int i=0; i<image.size().height; i++){
143  for (int j=0; j<image.size().width; j++){
144  src = image.data + image.step * i + j * 3;
145  dst[2] = src[0];
146  dst[1] = src[1];
147  dst[0] = src[2];
148  dst += 3;
149  }
150  }
151  break;
152  }
153  case 1:
154  m_image.data.image.format = Img::CF_GRAY;
155  memcpy(m_image.data.image.raw_data.get_buffer(),
156  image.data,
157  m_image.data.image.raw_data.length());
158  break;
159  default:
160  break;
161  }
162 
163  m_imageOut.write();
164 
165  return RTC::RTC_OK;
166 }
167 
168 /*
169 RTC::ReturnCode_t CameraImageLoader::onAborting(RTC::UniqueId ec_id)
170 {
171  return RTC::RTC_OK;
172 }
173 */
174 
175 /*
176 RTC::ReturnCode_t CameraImageLoader::onError(RTC::UniqueId ec_id)
177 {
178  return RTC::RTC_OK;
179 }
180 */
181 
182 /*
183 RTC::ReturnCode_t CameraImageLoader::onReset(RTC::UniqueId ec_id)
184 {
185  return RTC::RTC_OK;
186 }
187 */
188 
189 /*
190 RTC::ReturnCode_t CameraImageLoader::onStateUpdate(RTC::UniqueId ec_id)
191 {
192  return RTC::RTC_OK;
193 }
194 */
195 
196 /*
197 RTC::ReturnCode_t CameraImageLoader::onRateChanged(RTC::UniqueId ec_id)
198 {
199  return RTC::RTC_OK;
200 }
201 */
202 
203 
204 
205 extern "C"
206 {
207 
209  {
211  manager->registerFactory(profile,
212  RTC::Create<CameraImageLoader>,
213  RTC::Delete<CameraImageLoader>);
214  }
215 
216 };
217 
218 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
void CameraImageLoaderInit(RTC::Manager *manager)
char * filename
static const char * spec[]
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
png_uint_32 i
virtual ~CameraImageLoader()
Destructor.
coil::Properties & getProperties()
png_bytepp image
bool addOutPort(const char *name, OutPortBase &outport)
camera image loader
ExecutionContextHandle_t UniqueId
CameraImageLoader(RTC::Manager *manager)
Constructor.
OutPort< Img::TimedCameraImage > m_imageOut
virtual RTC::ReturnCode_t onInitialize()
prop
virtual bool write(DataType &value)
Img::TimedCameraImage m_image
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)


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