[openrtm-commit:01997] r179 - in trunk/ImageProcessing/opencv/components/OpenCVCamera: . include/OpenCVCamera src
openrtm @ openrtm.org
openrtm @ openrtm.org
2016年 8月 9日 (火) 10:54:40 JST
Author: takahashi
Date: 2016-08-09 10:54:39 +0900 (Tue, 09 Aug 2016)
New Revision: 179
Modified:
trunk/ImageProcessing/opencv/components/OpenCVCamera/RTC.xml
trunk/ImageProcessing/opencv/components/OpenCVCamera/include/OpenCVCamera/OpenCVCamera.h
trunk/ImageProcessing/opencv/components/OpenCVCamera/src/OpenCVCamera.cpp
Log:
[modify] Added configuration parameters (frame-width, height, rate) for OpenCVCamera component
Modified: trunk/ImageProcessing/opencv/components/OpenCVCamera/RTC.xml
===================================================================
--- trunk/ImageProcessing/opencv/components/OpenCVCamera/RTC.xml 2016-06-23 09:35:59 UTC (rev 178)
+++ trunk/ImageProcessing/opencv/components/OpenCVCamera/RTC.xml 2016-08-09 01:54:39 UTC (rev 179)
@@ -21,6 +21,15 @@
<rtc:Configuration xsi:type="rtcExt:configuration_ext" rtcExt:variableName="device_num" rtc:unit="" rtc:defaultValue="0" rtc:type="int" rtc:name="device_num">
<rtcExt:Properties rtcExt:value="text" rtcExt:name="__widget__"/>
</rtc:Configuration>
+ <rtc:Configuration xsi:type="rtcExt:configuration_ext" rtcExt:variableName="frame_width" rtc:unit="" rtc:defaultValue="0" rtc:type="int" rtc:name="frame_width">
+ <rtcExt:Properties rtcExt:value="text" rtcExt:name="__widget__"/>
+ </rtc:Configuration>
+ <rtc:Configuration xsi:type="rtcExt:configuration_ext" rtcExt:variableName="frame_height" rtc:unit="" rtc:defaultValue="0" rtc:type="int" rtc:name="frame_height">
+ <rtcExt:Properties rtcExt:value="text" rtcExt:name="__widget__"/>
+ </rtc:Configuration>
+ <rtc:Configuration xsi:type="rtcExt:configuration_ext" rtcExt:variableName="frame_rate" rtc:unit="" rtc:defaultValue="0" rtc:type="int" rtc:name="frame_rate">
+ <rtcExt:Properties rtcExt:value="text" rtcExt:name="__widget__"/>
+ </rtc:Configuration>
</rtc:ConfigurationSet>
<rtc:DataPorts xsi:type="rtcExt:dataport_ext" rtcExt:position="RIGHT" rtcExt:variableName="out" rtc:unit="" rtc:subscriptionType="" rtc:dataflowType="" rtc:interfaceType="" rtc:idlFile="" rtc:type="RTC::CameraImage" rtc:name="out" rtc:portType="DataOutPort"/>
<rtc:Language xsi:type="rtcExt:language_ext" rtc:kind="C++"/>
Modified: trunk/ImageProcessing/opencv/components/OpenCVCamera/include/OpenCVCamera/OpenCVCamera.h
===================================================================
--- trunk/ImageProcessing/opencv/components/OpenCVCamera/include/OpenCVCamera/OpenCVCamera.h 2016-06-23 09:35:59 UTC (rev 178)
+++ trunk/ImageProcessing/opencv/components/OpenCVCamera/include/OpenCVCamera/OpenCVCamera.h 2016-08-09 01:54:39 UTC (rev 179)
@@ -234,6 +234,28 @@
*/
int m_device_num;
+ /*!
+ *
+ * - Name: frame_width
+ * - DefaultValue: 640
+ */
+ int m_frame_width;
+
+ /*!
+ *
+ * - Name: frame_height
+ * - DefaultValue: 480
+ */
+ int m_frame_height;
+
+ /*!
+ *
+ * - Name: frame_rate
+ * - DefaultValue: 10
+ */
+ int m_frame_rate;
+
+
// </rtc-template>
// DataInPort declaration
@@ -275,7 +297,7 @@
// </rtc-template>
int dummy;
-
+ int m_device_id; /* 使用中のカメラデバイスID */
CvCapture* m_capture; /* カメラ用メモリ */
};
Modified: trunk/ImageProcessing/opencv/components/OpenCVCamera/src/OpenCVCamera.cpp
===================================================================
--- trunk/ImageProcessing/opencv/components/OpenCVCamera/src/OpenCVCamera.cpp 2016-06-23 09:35:59 UTC (rev 178)
+++ trunk/ImageProcessing/opencv/components/OpenCVCamera/src/OpenCVCamera.cpp 2016-08-09 01:54:39 UTC (rev 179)
@@ -9,6 +9,8 @@
#include "OpenCVCamera.h"
#include <iostream>
+#include <sapi.h>
+
using namespace std;
// Module specification
@@ -28,14 +30,19 @@
"lang_type", "compile",
// Configuration variables
"conf.default.device_num", "0",
- // Widget
+ "conf.default.frame_width", "640",
+ "conf.default.frame_height", "480",
+ "conf.default.frame_rate", "10",
+ // Widget
"conf.__widget__.device_num", "text",
+ "conf.__widget__.frame_width", "text",
+ "conf.__widget__.frame_height", "text",
+ "conf.__widget__.frame_rate", "text",
// Constraints
""
};
// </rtc-template>
- int device_num_old = 1000;
- bool imgflg = false;
+
/*!
* @brief constructor
* @param manager Maneger Object
@@ -79,8 +86,14 @@
// <rtc-template block="bind_config">
// Bind variables and configuration variable
bindParameter("device_num", m_device_num, "0");
+ bindParameter("frame_width", m_frame_width, "640");
+ bindParameter("frame_height", m_frame_height, "480");
+ bindParameter("frame_rate", m_frame_rate, "10");
// </rtc-template>
-
+
+ m_device_id = -1;
+ m_capture = NULL;
+
return RTC::RTC_OK;
}
@@ -108,102 +121,98 @@
RTC::ReturnCode_t OpenCVCamera::onActivated(RTC::UniqueId ec_id)
{
- /* カメラデバイスの探索 */
-/*
- if(NULL==(m_capture = cvCaptureFromCAM(CV_CAP_ANY))){
- cout<<"No Camera Device"<<endl;
- return RTC::RTC_ERROR;
- }
+ m_device_id = m_device_num;
- return RTC::RTC_OK;
-*/
- return RTC::RTC_OK;
+ /* カメラデバイスの探索 */
+ if (NULL == (m_capture = cvCaptureFromCAM(m_device_id)))
+ {
+ cout << "No Camera Device" << endl;
+ return RTC::RTC_ERROR;
+ }
+
+ return RTC::RTC_OK;
}
RTC::ReturnCode_t OpenCVCamera::onDeactivated(RTC::UniqueId ec_id)
{
/* カメラ用メモリの解放 */
- if(m_capture != NULL)
+ if (m_capture != NULL)
{
cvReleaseCapture(&m_capture);
}
- device_num_old = 1000;
-
return RTC::RTC_OK;
}
-
RTC::ReturnCode_t OpenCVCamera::onExecute(RTC::UniqueId ec_id)
{
- static coil::TimeValue tm_pre;
+ static coil::TimeValue tmOld;
static int count = 0;
+ const int DISPLAY_PERIOD_FRAME_NUM = 100;
IplImage *cam_frame = NULL;
- if(m_device_num != device_num_old){
+ /* 実行中にコンフィグレーションによりデバイスIDが更新された場合 */
+ if (m_device_num != m_device_id)
+ {
+ cvReleaseCapture(&m_capture);
+ m_device_id = m_device_num;
- if (device_num_old != 1000)
+ /* カメラデバイスの再探索 */
+ if (NULL == (m_capture = cvCaptureFromCAM(m_device_id)))
{
- cvReleaseCapture(&m_capture);
+ cout << "No Camera Device" << endl;
+ return RTC::RTC_ERROR;
}
+ }
- device_num_old = m_device_num;
+ cvSetCaptureProperty(m_capture, CV_CAP_PROP_FRAME_WIDTH, m_frame_width);
+ cvSetCaptureProperty(m_capture, CV_CAP_PROP_FRAME_HEIGHT, m_frame_height);
+ cvSetCaptureProperty(m_capture, CV_CAP_PROP_FPS, m_frame_rate);
- if(NULL==(m_capture = cvCaptureFromCAM(device_num_old))){
- cout<<"No Camera Device"<<endl;
- imgflg = false;
- //return RTC::RTC_ERROR;
- }else{
- imgflg = true;
- }
+ cam_frame = cvQueryFrame(m_capture);
+ if (NULL == cam_frame)
+ {
+ cout << "Bad frame or no frame!!" << endl;
+ return RTC::RTC_ERROR;
}
- if(imgflg == true){
- cam_frame = cvQueryFrame(m_capture);
- if(NULL == cam_frame)
- {
- std::cout << "Bad frame or no frame!!" << std::endl;
- return RTC::RTC_ERROR;
- }
+ IplImage* frame = cvCreateImage(cvGetSize(cam_frame), 8, 3);
- IplImage* frame = cvCreateImage(cvGetSize(cam_frame), 8, 3);
+ if (cam_frame->origin == IPL_ORIGIN_TL)
+ {
+ cvCopy(cam_frame, frame);
+ } else {
+ cvFlip(cam_frame, frame);
+ }
- if(cam_frame ->origin == IPL_ORIGIN_TL)
- cvCopy(cam_frame, frame);
- else
- cvFlip(cam_frame, frame);
+ int len = frame->nChannels * frame->width * frame->height;
- int len = frame->nChannels * frame->width * frame->height;
+ /* 画面のサイズ情報を入れる */
+ m_out.pixels.length(len);
+ m_out.width = frame->width;
+ m_out.height = frame->height;
- /* 画面のサイズ情報を入れる */
- m_out.pixels.length(len);
- m_out.width = frame->width;
- m_out.height = frame->height;
+ memcpy((void *)&(m_out.pixels[0]), frame->imageData, len);
+ cvReleaseImage(&frame);
- memcpy((void *)&(m_out.pixels[0]), frame->imageData,len);
- cvReleaseImage(&frame);
+ /* 繋がってるコンポーネントがしんでしまうと問題発生 */
+ m_outOut.write();
- /* 繋がってるコンポーネントがしんでしまうと問題発生 */
- m_outOut.write();
+ /* フレームレートの表示 */
+ if (count++ > DISPLAY_PERIOD_FRAME_NUM)
+ {
+ coil::TimeValue tmNow = coil::gettimeofday();
+ double sec(tmNow - tmOld);
- if (count > 100)
- {
- count = 0;
- coil::TimeValue tm;
- tm = coil::gettimeofday();
+ if (sec > 1.0)
+ {
+ cout << (DISPLAY_PERIOD_FRAME_NUM / sec) << " [FPS]" << endl;
+ tmOld = tmNow;
+ }
+ count = 0;
+ }
- double sec(tm - tm_pre);
-
- if (sec > 1.0 && sec < 1000.0)
- {
- std::cout << 100/sec << " [FPS]" << std::endl;
- }
-
- tm_pre = tm;
- }
- ++count;
- }
return RTC::RTC_OK;
}
More information about the openrtm-commit
mailing list