UndistortImage.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "UndistortImage.h"
11 
12 #include <opencv2/calib3d/calib3d.hpp>
13 #include <opencv2/core/types_c.h>
14 #include <opencv2/imgproc/imgproc.hpp>
15 
16 // Module specification
17 // <rtc-template block="module_spec">
18 static const char* cameraimageviewercomponent_spec[] =
19 {
20  "implementation_id", "UndistortImage",
21  "type_name", "UndistortImage",
22  "description", "camera image undistortion component",
23  "version", HRPSYS_PACKAGE_VERSION,
24  "vendor", "AIST",
25  "category", "example",
26  "activity_type", "DataFlowComponent",
27  "max_instance", "10",
28  "language", "C++",
29  "lang_type", "compile",
30  // Configuration variables
31  "conf.default.calibFile", "camera.xml",
32 
33  ""
34 };
35 // </rtc-template>
36 
38  : RTC::DataFlowComponentBase(manager),
39  // <rtc-template block="initializer">
40  m_imageIn("imageIn", m_image),
41  m_imageOut("imageOut", m_image),
42  // </rtc-template>
43  m_cvImage(NULL),
44  dummy(0)
45 {
46 }
47 
49 {
50 }
51 
52 
53 
54 RTC::ReturnCode_t UndistortImage::onInitialize()
55 {
56  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
57  // <rtc-template block="bind_config">
58  // Bind variables and configuration variable
59  bindParameter("calibFile", m_calibFile, "camera.xml");
60 
61  // </rtc-template>
62 
63  // Registration: InPort/OutPort/Service
64  // <rtc-template block="registration">
65  // Set InPort buffers
66  addInPort("imageIn", m_imageIn);
67 
68  // Set OutPort buffer
69  addOutPort("imageOut", m_imageOut);
70 
71  // Set service provider to Ports
72 
73  // Set service consumers to Ports
74 
75  // Set CORBA Service Ports
76 
77  // </rtc-template>
78 
80 
81  return RTC::RTC_OK;
82 }
83 
84 
85 
86 /*
87  RTC::ReturnCode_t UndistortImage::onFinalize()
88  {
89  return RTC::RTC_OK;
90  }
91 */
92 
93 /*
94  RTC::ReturnCode_t UndistortImage::onStartup(RTC::UniqueId ec_id)
95  {
96  return RTC::RTC_OK;
97  }
98 */
99 
100 /*
101  RTC::ReturnCode_t UndistortImage::onShutdown(RTC::UniqueId ec_id)
102  {
103  return RTC::RTC_OK;
104  }
105 */
106 
108 {
109  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
110 
111  cv::FileStorage fs(m_calibFile.c_str(), cv::FileStorage::READ);
112  if (!fs.isOpened()){
113  std::cerr << m_profile.instance_name << ": can't open "
114  << m_calibFile << std::endl;
115  return RTC::RTC_ERROR;
116  }
117  cv::FileNode param = fs["intrinsic"];
118  param >> m_intrinsic;
119  param = fs["distortion"];
120  param >> m_distortion;
121 
122  return RTC::RTC_OK;
123 }
124 
126 {
127  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
128  if (m_cvImage) {
129  cvReleaseImage(&m_cvImage);
130  m_cvImage = NULL;
131  }
132 
133  return RTC::RTC_OK;
134 }
135 
136 RTC::ReturnCode_t UndistortImage::onExecute(RTC::UniqueId ec_id)
137 {
138  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
139 
140  if (!m_imageIn.isNew()) return RTC::RTC_OK;
141 
142  m_imageIn.read();
143 
144  if (m_cvImage && (m_image.data.image.width != m_cvImage->width
145  || m_image.data.image.height != m_cvImage->height)){
146  cvReleaseImage(&m_cvImage);
147  m_cvImage = NULL;
148  }
149 
150  if (!m_cvImage){
151  switch (m_image.data.image.format){
152  case Img::CF_RGB:
153  m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
154  m_image.data.image.height),
155  IPL_DEPTH_8U, 3);
156  break;
157  case Img::CF_GRAY:
158  m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
159  m_image.data.image.height),
160  IPL_DEPTH_8U, 1);
161  break;
162  default:
163  std::cerr << "unsupported color format("
164  << m_image.data.image.format << ")" << std::endl;
165  return RTC::RTC_ERROR;
166  }
167  }
168  switch(m_image.data.image.format){
169  case Img::CF_RGB:
170  {
171  // RGB -> BGR
172  char *dst = m_cvImage->imageData;
173  for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
174  dst[i ] = m_image.data.image.raw_data[i+2];
175  dst[i+1] = m_image.data.image.raw_data[i+1];
176  dst[i+2] = m_image.data.image.raw_data[i ];
177  }
178  break;
179  }
180  case Img::CF_GRAY:
181  memcpy(m_cvImage->imageData,
182  m_image.data.image.raw_data.get_buffer(),
183  m_image.data.image.raw_data.length());
184  break;
185  default:
186  break;
187  }
188 
189 
190  cv::Mat src_img = cv::cvarrToMat(m_cvImage);
191  cv::Mat dst_img;
192  src_img.copyTo(dst_img);
193  cv::undistort (src_img, dst_img, m_intrinsic, m_distortion);
194 
195  switch(m_image.data.image.format){
196  case Img::CF_RGB:
197  {
198  // BGR -> RGB
199  unsigned char *src = dst_img.data;
200  for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
201  m_image.data.image.raw_data[i+2] = src[i ];
202  m_image.data.image.raw_data[i+1] = src[i+1];
203  m_image.data.image.raw_data[i ] = src[i+2];
204  }
205  break;
206  }
207  case Img::CF_GRAY:
208  memcpy(m_image.data.image.raw_data.get_buffer(),
209  dst_img.data,
210  m_image.data.image.raw_data.length());
211  break;
212  default:
213  break;
214  }
215 
216  m_imageOut.write();
217 
218  return RTC::RTC_OK;
219 }
220 
221 /*
222  RTC::ReturnCode_t UndistortImage::onAborting(RTC::UniqueId ec_id)
223  {
224  return RTC::RTC_OK;
225  }
226 */
227 
228 /*
229  RTC::ReturnCode_t UndistortImage::onError(RTC::UniqueId ec_id)
230  {
231  return RTC::RTC_OK;
232  }
233 */
234 
235 /*
236  RTC::ReturnCode_t UndistortImage::onReset(RTC::UniqueId ec_id)
237  {
238  return RTC::RTC_OK;
239  }
240 */
241 
242 /*
243  RTC::ReturnCode_t UndistortImage::onStateUpdate(RTC::UniqueId ec_id)
244  {
245  return RTC::RTC_OK;
246  }
247 */
248 
249 /*
250  RTC::ReturnCode_t UndistortImage::onRateChanged(RTC::UniqueId ec_id)
251  {
252  return RTC::RTC_OK;
253  }
254 */
255 
256 
257 
258 extern "C"
259 {
260 
262  {
264  manager->registerFactory(profile,
265  RTC::Create<UndistortImage>,
266  RTC::Delete<UndistortImage>);
267  }
268 
269 };
270 
271 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
InPort< Img::TimedCameraImage > m_imageIn
static const char * cameraimageviewercomponent_spec[]
png_uint_32 i
coil::Properties & getProperties()
Img::TimedCameraImage m_image
bool addOutPort(const char *name, OutPortBase &outport)
std::string m_calibFile
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
cv::Mat m_distortion
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
ExecutionContextHandle_t UniqueId
virtual ~UndistortImage()
Destructor.
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
void UndistortImageInit(RTC::Manager *manager)
prop
IplImage * m_cvImage
virtual bool isNew()
virtual bool write(DataType &value)
UndistortImage(RTC::Manager *manager)
Constructor.
null component
virtual RTC::ReturnCode_t onInitialize()
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
OutPort< Img::TimedCameraImage > m_imageOut


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