[openrtm-commit:01134] r104 - in branches/newCMakeForVC2010/ImageProcessing/opencv/components: . ImageSubstraction/include/ImageSubstraction ImageSubstraction/src RockPaperScissors RockPaperScissors/include RockPaperScissors/include/RockPaperScissors RockPaperScissors/src
openrtm @ openrtm.org
openrtm @ openrtm.org
2013年 11月 29日 (金) 15:24:42 JST
Author: kawauchi
Date: 2013-11-29 15:24:42 +0900 (Fri, 29 Nov 2013)
New Revision: 104
Removed:
branches/newCMakeForVC2010/ImageProcessing/opencv/components/Houghline/
Modified:
branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/include/ImageSubstraction/ImageSubstraction.h
branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/src/ImageSubstraction.cpp
branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/RockPaperScissors.conf
branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/LabellingW.h
branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/RockPaperScissors/RockPaperScissors.h
branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/src/RockPaperScissors.cpp
Log:
For replacement, remove Hough component . refs #2704
Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/include/ImageSubstraction/ImageSubstraction.h
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/include/ImageSubstraction/ImageSubstraction.h 2013-11-14 07:48:22 UTC (rev 103)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/include/ImageSubstraction/ImageSubstraction.h 2013-11-29 06:24:42 UTC (rev 104)
@@ -24,9 +24,7 @@
#include <highgui.h>
#define NUM_OF_BACKGROUND_FRAMES 50 // wif𶬷éÌÉgp·éæÌ
-#define THRESHOLD_COEFFICIENT 5.0 // èlðßéÛÉgp·éWηɩ¯é
-#define CONSTANT_THRESHOLD 20 // æÅ1ÂÌèlðg¤ÛÌèl
#define CONSTANT_MODE 0 // æSÌÅ1ÂÌèlðp¢étOl
#define DYNAMIC_MODE 1 // æf²ÆÉèlðݯétOl
@@ -236,6 +234,12 @@
// <rtc-template block="config_declare">
/*!
*
+ * - Name: cont_mode
+ * - DefaultValue: b
+ */
+ char m_cont_mode;
+ /*!
+ *
* - Name: img_height
* - DefaultValue: 240
*/
@@ -246,7 +250,19 @@
* - DefaultValue: 320
*/
int m_img_width;
-
+ /*!
+ *
+ * - Name: thre_coefficient
+ * - DefaultValue: 5.0
+ */
+ double m_thre_coefficient;
+ /*!
+ *
+ * - Name: constant_thre
+ * - DefaultValue: 20
+ */
+ int m_constant_thre;
+
// </rtc-template>
// DataInPort declaration
Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/src/ImageSubstraction.cpp
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/src/ImageSubstraction.cpp 2013-11-14 07:48:22 UTC (rev 103)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/ImageSubstraction/src/ImageSubstraction.cpp 2013-11-29 06:24:42 UTC (rev 104)
@@ -25,19 +25,26 @@
"language", "C++",
"lang_type", "compile",
// Configuration variables
+ "conf.default.control_mode", "b",
"conf.default.image_height", "240",
"conf.default.image_width", "320",
+ "conf.default.threshold_coefficient", "5.0",
+ "conf.default.constant_threshold", "20",
// Widget
+ "conf.__widget__.control_mode", "radio",
"conf.__widget__.image_height", "text",
"conf.__widget__.image_width", "text",
+ "conf.__widget__.threshold_coefficient", "text",
+ "conf.__widget__.constant_threshold", "slider.1",
// Constraints
+ "conf.__constraints__.control_mode", "(b,m)",
+ "conf.__constraints__.constant_threshold", "0<=x<=255",
""
};
// </rtc-template>
int ImageSubstraction_count = 0;
-int key; // L[üÍpÌÏ
-int mode = 0; // 0: æf²ÆÉÙÈéèl / 1: æSÌÅêÂÌèl
+int mode = DYNAMIC_MODE; // DYNAMIC_MODE: æf²ÆÉÙÈéèl / CONSTANT_MODE: æSÌÅêÂÌèl
int g_temp_w = 0;
int g_temp_h = 0;
@@ -46,9 +53,9 @@
//char *windowNameBackground = "Background"; // wiæð\¦·éEBhE̼O
//char *windowNameThreshold = "Threshold"; // èlð\¦·éEBhE̼O
-char *mode_str[2] = {
- "æfPÊ",
- "æÅêÂ"
+std::string mode_str[2] = {
+ "DYNAMIC_MODE", //æfPÊ
+ "CONSTANT_MODE" //æÅêÂ
};
IplImage *backgroundAverageImage = NULL; // wi̽ÏlÛ¶pIplImage
@@ -83,7 +90,7 @@
// num : wif𶬷éÌÉgp·éæÌ
// size : æTCY
//
-void initializeBackgroundModel( int num, CvSize size ){
+void initializeBackgroundModel( int num, CvSize size, double thre_coefficient ){
int i;
// ÈOÌwiîñª êÎjü
@@ -103,15 +110,15 @@
cvSetZero( acc2 );
// æîñÌ~Ï
- printf( "wiæ¾...\n" );
+ printf( "Getting background...\n" ); //wiæ¾
//IplImage *frameImage;
for( i = 0; i < num; i++ ){
//frameImage = cvQueryFrame( capture );
cvAcc( originalImage, acc );
cvSquareAcc( originalImage, acc2 );
- printf( "%d %d Ú\n", num, i + 1 );
+ printf( "%d / %d image\n", i + 1, num );
}
- printf( "wiæ¾®¹\n" );
+ printf( "Completion!\n" ); //wiæ¾®¹
// cvAddS, cvSubS Í éª cvMulS ÍÈ¢ÌÅAcvConvertScale ðg¤
cvConvertScale( acc, acc, 1.0 / num ); // ½Ï
@@ -132,7 +139,7 @@
// èlðvZ·é
backgroundThresholdImage = cvCreateImage( size, IPL_DEPTH_8U, 3 );
- cvConvertScale( sd, backgroundThresholdImage, THRESHOLD_COEFFICIENT );
+ cvConvertScale( sd, backgroundThresholdImage, thre_coefficient );
// ððú·é
cvReleaseImage( &acc );
@@ -149,7 +156,7 @@
// <rtc-template block="initializer">
: RTC::DataFlowComponentBase(manager),
m_img_origIn("original_image", m_img_orig),
- m_keyIn("Key", m_key),
+ m_keyIn("Key", m_key),
m_img_captureOut("capture_image", m_img_capture),
m_img_resultOut("result_image", m_img_result),
m_img_backOut("back_image", m_img_back),
@@ -192,8 +199,11 @@
// <rtc-template block="bind_config">
// Bind variables and configuration variable
+ bindParameter("control_mode", m_cont_mode, "b");
bindParameter("image_height", m_img_height, "240");
bindParameter("image_width", m_img_width, "320");
+ bindParameter("threshold_coefficient", m_thre_coefficient, "5.0");
+ bindParameter("constant_threshold", m_constant_thre, "20");
// </rtc-template>
return RTC::RTC_OK;
@@ -231,6 +241,9 @@
outputImage = NULL;
resultImage = NULL;
differenceImage = NULL;
+
+ //èlÌúÝèð\¦
+ printf( "threshold: %s\n", mode_str[1-mode].c_str() );
return RTC::RTC_OK;
}
@@ -258,7 +271,23 @@
RTC::ReturnCode_t ImageSubstraction::onExecute(RTC::UniqueId ec_id)
{
-
+ // L[üÍ»è
+ if(m_keyIn.isNew()){
+ m_keyIn.read();
+
+ if( m_cont_mode == 'b' ){
+ // 'b'L[ª³ê½ç»Ì_ÅÌæðwiæÆ·é
+ initializeBackgroundModel( NUM_OF_BACKGROUND_FRAMES, cvSize(m_img_width, m_img_height), m_thre_coefficient);
+
+ printf( "Background image update\n" ); //wiîñXV
+
+ } else if( m_cont_mode == 'm' ){
+ // 'm'L[ª³ê½çèlÌÝèû@ðÏX·é
+ mode = 1 - mode;
+ printf( "threshold: %s\n", mode_str[mode].c_str() );
+ }
+ }
+
//úlðæ¾·éB
if(ImageSubstraction_count == 0 && m_img_origIn.isNew()) {
@@ -295,7 +324,7 @@
resultImage = cvCreateImage(cvSize(m_img_orig.width, m_img_orig.height),IPL_DEPTH_8U, 1);
}
- initializeBackgroundModel( NUM_OF_BACKGROUND_FRAMES, cvSize(m_img_orig.width, m_img_orig.height) );
+ initializeBackgroundModel( NUM_OF_BACKGROUND_FRAMES, cvSize(m_img_orig.width, m_img_orig.height) , m_thre_coefficient);
ImageSubstraction_count = 1;
g_temp_w = m_img_orig.width;
@@ -325,21 +354,16 @@
outputImage = cvCreateImage(cvSize(m_img_orig.width, m_img_orig.height), IPL_DEPTH_8U, 3);
}
- if(m_keyIn.isNew()) {
- m_keyIn.read();
- key = (int)m_key.data;
- }
-
memcpy(originalImage->imageData,(void *)&(m_img_orig.pixels[0]), m_img_orig.pixels.length());
// »ÝÌwiÆÌ·ÌâÎl𬪲ÆÉæé
cvAbsDiff( originalImage, backgroundAverageImage, differenceImage );
// Sub Í}CiXÉÈÁ½ç0ÉØèlßÄêé
- if( mode == 0 ){
+ if( mode == DYNAMIC_MODE ){
cvSub( differenceImage, backgroundThresholdImage, differenceImage );
} else{
- cvSubS( differenceImage, cvScalarAll( CONSTANT_THRESHOLD ), differenceImage );
+ cvSubS( differenceImage, cvScalarAll( m_constant_thre ), differenceImage );
}
// differenceImage Ìvfª1ÂÅà0Èã¾Á½çOi
@@ -395,26 +419,10 @@
m_img_thresholdOut.write();
cvReleaseImage( &tmp );
- // L[üÍ»è
- cvWaitKey( 1 );
- //key = (int)m_key.data;
- if( key == 'b' ){
- // 'b'L[ª³ê½ç»Ì_ÅÌæðwiæÆ·é
- initializeBackgroundModel( NUM_OF_BACKGROUND_FRAMES, cvSize(m_img_width, m_img_height));
-
- printf( "wiîñXV\n" );
-
- } else if( key == 'm' ){
- // 'm'L[ª³ê½çèlÌÝèû@ðÏX·é
- mode = 1 - mode;
- printf( "èl: %s\n", mode_str[mode] );
- }
-
cvReleaseImage(&originalImage);
cvReleaseImage(&outputImage);
- key = '0';
g_temp_w = m_img_orig.width;
g_temp_h = m_img_orig.height;
Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/RockPaperScissors.conf
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/RockPaperScissors.conf 2013-11-14 07:48:22 UTC (rev 103)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/RockPaperScissors.conf 2013-11-29 06:24:42 UTC (rev 104)
@@ -130,5 +130,5 @@
##
## The execution cycle of ExecutionContext
##
-exec_cxt.periodic.rate:1000.0
+exec_cxt.periodic.rate:2000.0
Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/LabellingW.h
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/LabellingW.h 2013-11-14 07:48:22 UTC (rev 103)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/LabellingW.h 2013-11-29 06:24:42 UTC (rev 104)
@@ -8,7 +8,7 @@
#include "Labeling.h"
#define Label LabelingBS
-/*
+
Label *createLabeling(){
return new LabelingBS();
}
@@ -26,5 +26,6 @@
void releaseLabeling(Label *label){
delete label;
}
-*/
-#endif LABELINGW_H
\ No newline at end of file
+
+#endif //LABELINGW_H
+
Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/RockPaperScissors/RockPaperScissors.h
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/RockPaperScissors/RockPaperScissors.h 2013-11-14 07:48:22 UTC (rev 103)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/include/RockPaperScissors/RockPaperScissors.h 2013-11-29 06:24:42 UTC (rev 104)
@@ -19,33 +19,24 @@
#include <rtm/idl/ExtendedDataTypesSkel.h>
#include <rtm/idl/InterfaceDataTypesSkel.h>
-// OpenCVHeadt@CÌIncluding
+// OpenCVHeadãã¡ã¤ã«ã®Including
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
-#include "LabellingW.h"
+#include <string>
+#include "Labeling.h"
-#define ITERATIONS 4 // c£AûkÌñ
+//èè²æ½åºç¨é¾å¤
+#define HMAX 20 // Hå¤ã®ä¸éã®é¾å¤
+#define HMIN 0 // Hå¤ã®ä¸éã®é¾å¤
+#define SMAX 255*1 // Så¤ã®ä¸éã®é¾å¤
+#define SMIN 255*0.2 // Så¤ã®ä¸éã®é¾å¤
+#define VMAX 255*1.0 // Vå¤ã®ä¸éã®é¾å¤
+#define VMIN 255*0 // Vå¤ã®ä¸éã®é¾å¤
-//§Fopèl
-#define HMAX 20 // HlÌãÀÌèl
-#define HMIN 0 // Hl̺ÀÌèl
-#define SMAX 255*1 // SlÌãÀÌèl
-#define SMIN 255*0.2 // Sl̺ÀÌèl
-#define VMAX 255*1.0 // VlÌãÀÌèl
-#define VMIN 255*0 // Vl̺ÀÌèl
+#define IGNORE_SIZE 1000 //ç¡è¦ããé åãµã¤ãº(ã©ããªã³ã°ç¨)
-//WP»èpèl
-#define ROCKMAX 1.0 // O[Æ»è·éãÀÌèl
-#define ROCKMIN 0.85 // O[Æ»è·éºÀÌèl
-#define SCISSORMAX 0.85 // `LÆ»è·éãÀÌèl
-#define SCISSORMIN 0.7 // `LÆ»è·éºÀÌèl
-#define PAPERMAX 0.7 // p[Æ»è·éãÀÌèl
-#define PAPERMIN 0.5 // p[Æ»è·éºÀÌèl
-
-#define IGNORE_SIZE 1000 //³·éÌæTCY(xOp)
-
// Service implementation headers
// <rtc-template block="service_impl_h">
@@ -252,18 +243,48 @@
// <rtc-template block="config_declare">
/*!
*
- * - Name: img_height
- * - DefaultValue: 240
+ * - Name: rock_max
+ * - DefaultValue: 1.0
*/
- int m_img_height;
+ double m_rock_max;
/*!
*
- * - Name: img_width
- * - DefaultValue: 320
+ * - Name: rock_min
+ * - DefaultValue: 0.85
*/
- int m_img_width;
+ double m_rock_min;
/*!
*
+ * - Name: scissor_max
+ * - DefaultValue: 0.85
+ */
+ double m_scissor_max;
+ /*!
+ *
+ * - Name: scissor_min
+ * - DefaultValue: 0.7
+ */
+ double m_scissor_min;
+ /*!
+ *
+ * - Name: paper_max
+ * - DefaultValue: 0.7
+ */
+ double m_paper_max;
+ /*!
+ *
+ * - Name: paper_min
+ * - DefaultValue: 0.5
+ */
+ double m_paper_min;
+ /*!
+ *
+ * - Name: iterations
+ * - DefaultValue: 4
+ */
+ int m_iterations;
+ /*!
+ *
* - Name: out_mode
* - DefaultValue: 1
*/
@@ -288,6 +309,11 @@
*/
OutPort<RTC::CameraImage> m_img_outputOut;
+ RTC::TimedString m_result;
+ /*!
+ */
+ OutPort<RTC::TimedString> m_resultOut;
+
// </rtc-template>
// CORBA Port declaration
@@ -313,7 +339,18 @@
// <rtc-template block="private_operation">
// </rtc-template>
+
+ void extractSkinColor( void );
+ void interpolate( void );
+ int pickupMaxArea( void );
+ void createConvexHull( int handarea, CvPoint **handpoint, int **hull,
+ CvMat *pointMatrix, CvMat *hullMatrix );
+ void drawConvexHull( CvPoint *handpoint, int *hull, int hullcount );
+ int calcConvexHullArea( CvPoint *handpoint, int *hull, int hullcount );
+ void decide( int handarea, int hullarea );
+
int dummy;
+ std::string m_prev_judge; //ååã®å¤å®ãã°ã¼ / ãã§ã / ãã¼
IplImage* m_frame_image;
IplImage* m_image_buff;
Modified: branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/src/RockPaperScissors.cpp
===================================================================
--- branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/src/RockPaperScissors.cpp 2013-11-14 07:48:22 UTC (rev 103)
+++ branches/newCMakeForVC2010/ImageProcessing/opencv/components/RockPaperScissors/src/RockPaperScissors.cpp 2013-11-29 06:24:42 UTC (rev 104)
@@ -8,10 +8,10 @@
*/
#include "RockPaperScissors.h"
+#define Label LabelingBS
using namespace std;
-
CvCapture *capture = NULL;
// Module specification
@@ -30,14 +30,31 @@
"language", "C++",
"lang_type", "compile",
// Configuration variables
- "conf.default.image_height", "240",
- "conf.default.image_width", "320",
+ "conf.default.rock_max", "1.0",
+ "conf.default.rock_min", "0.85",
+ "conf.default.scissor_max", "0.85",
+ "conf.default.scissor_min", "0.7",
+ "conf.default.paper_max", "0.7",
+ "conf.default.paper_min", "0.5",
+ "conf.default.iterations", "4",
"conf.default.out_mode", "1",
// Widget
- "conf.__widget__.image_height", "text",
- "conf.__widget__.image_width", "text",
- "conf.__widget__.out_mode", "text",
+ "conf.__widget__.rock_max", "text",
+ "conf.__widget__.rock_min", "text",
+ "conf.__widget__.scissor_max", "text",
+ "conf.__widget__.scissor_min", "text",
+ "conf.__widget__.paper_max", "text",
+ "conf.__widget__.paper_min", "text",
+ "conf.__widget__.iterations", "text",
+ "conf.__widget__.out_mode", "radio",
// Constraints
+ "conf.__constraints__.rock_max", "0<=x<=1.0",
+ "conf.__constraints__.rock_min", "0<=x<=1.0",
+ "conf.__constraints__.scissor_max", "0<=x<=1.0",
+ "conf.__constraints__.scissor_min", "0<=x<=1.0",
+ "conf.__constraints__.paper_max", "0<=x<=1.0",
+ "conf.__constraints__.paper_min", "0<=x<=1.0",
+ "conf.__constraints__.out_mode", "(0,1)",
""
};
// </rtc-template>
@@ -60,200 +77,6 @@
delete label;
}
- //
- // §Fðo·é
- //
- // ø:
- // frameImage : Lv`櫑pIplImage
- // hsvImage : HSVæpIplImage
- // skinImage : §FoæpIplImage
- //
-void extractSkinColor( IplImage *frameImage, IplImage *hsvImage, IplImage *skinImage ) {
- CvScalar color; // HSV\FnÅ\µ½F
- unsigned char h; // H»
- unsigned char s; // S»
- unsigned char v; // V»
-
- // BGR©çHSVÉÏ··é
-
- cvCvtColor( frameImage, hsvImage, CV_BGR2HSV );
-
- //§Fo
- for( int x = 0; x < skinImage->width; x++ ) {
- for( int y = 0 ; y < skinImage->height; y++ ) {
-
- color = cvGet2D( hsvImage, y, x );
- h = color.val[0];
- s = color.val[1];
- v = color.val[2];
-
- if( h <= HMAX && h >= HMIN &&
- s <= SMAX && s >= SMIN &&
- v <= VMAX && v >= VMIN ) {
- // §FÌê
- cvSetReal2D( skinImage, y, x, 255 );
- } else {
- cvSetReal2D( skinImage, y, x, 0 );
- }
- }
- }
-}
-
- //
- // ¹ÌæðâÔ·é
- //
- // ø:
- // skinImage : §FoæpIplImage
- // temp : êÛ¶pIplImage
- //
-void interpolate( IplImage *skinImage, IplImage *temp ) {
- //c£ðITERATIONSñs¤
- cvDilate( skinImage, temp, NULL, ITERATIONS );
-
- //ûkðITERATIONSñs¤
- cvErode( temp, skinImage, NULL, ITERATIONS );
-}
-
- //
- // ÅåÌæ(èÌæ)Ìoðs¤
- //
- // ø:
- // skinImage : §FoæpIplImage
- // label : xOµ½Ê
- // convexHullImage : ConvexHullæpIplImage
- //
- // ßèl:
- // èÌæÌÊÏ
- //
-int pickupMaxArea(IplImage *skinImage, IplImage *label, IplImage *convexHullImage ) {
-
- int handarea = 0; // èÌæÌÊÏ
-
- for(int x = 0; x < skinImage->width; x++ ) {
- for( int y=0; y < skinImage->height; y++ ) {
- if( cvGetReal2D( label, y, x ) == 1 ) {
- // ÅåÌæ¾Á½ê
- handarea++;
- cvSet2D( convexHullImage, y, x, CV_RGB( 255, 255, 255 ) );
- } else {
- cvSetReal2D( skinImage, y, x, 0 );
- cvSet2D( convexHullImage, y, x, CV_RGB( 0, 0, 0 ) );
- }
- }
- }
- return handarea;
-}
-
- //
- // ConvexHull𶬷é
- //
- // ø:
- // skinImage : §FoæpIplImage
- // handarea : èÌæÌÊÏ(_Ì)
- // handpoint : èÌæàÌ_ÌÀWzñÖÌ|C^
- // hull : ConvexHull̸_Ìhandpointɨ¯éindexÔÖÌ|C^
- // pointMatrix : èÌæpsñÖÌ|C^
- // hullMatrix : ConvexHullpsñÖÌ|C^
- //
-void createConvexHull(IplImage *skinImage, int handarea, CvPoint **handpoint, int **hull,
- CvMat *pointMatrix, CvMat *hullMatrix ) {
- int i=0;
-
- // ConvexHullðvZ·é½ßÉKvÈsñ𶬷é
- *handpoint=( CvPoint * )malloc( sizeof( CvPoint ) * handarea );
- *hull = ( int * )malloc( sizeof( int ) * handarea );
- *pointMatrix = cvMat( 1, handarea, CV_32SC2, *handpoint );
- *hullMatrix = cvMat( 1, handarea, CV_32SC1, *hull );
-
- for( int x = 0; x < skinImage->width; x++ ) {
- for( int y = 0; y < skinImage->height; y++ ) {
- if( cvGetReal2D( skinImage, y, x ) == 255 ) {
- ( *handpoint )[i].x = x;
- ( *handpoint )[i].y = y;
- i++;
- }
- }
- }
-
- // ConvexHull𶬷é
- cvConvexHull2( pointMatrix, hullMatrix, CV_CLOCKWISE, 0 );
-}
-
- //
- // ConvexHullð`æ·é
- //
- // ø:
- // convexHullImage : ConvexHullæpIplImage
- // handpoint : èÌæàÌ_ÌÀWzñ
- // hull : ConvexHull̸_Ìhandpointɨ¯éindexÔ
- // hullcount : ConvexHull̸_Ì
- //
-void drawConvexHull(IplImage *convexHullImage, CvPoint *handpoint, int *hull, int hullcount ) {
- CvPoint pt0 = handpoint[hull[hullcount-1]];
- for( int i = 0; i < hullcount; i++ ) {
- CvPoint pt = handpoint[hull[i]];
- cvLine( convexHullImage, pt0, pt, CV_RGB( 0, 255, 0 ) );
- pt0 = pt;
- }
-}
-
- //
- // ConvexHullàÌÊÏðßé
- //
- // ø:
- // convexHullImage : ConvexHullæpIplImage
- // handpoint : èÌæàÌ_ÌÀWzñ
- // hull : ConvexHull̸_Ìhandpointɨ¯éindexÔ
- // hullcount : ConvexHull̸_Ì@@
- //
- // ßèl:
- // ConvexHullàÌÊÏ
- //
-int calcConvexHullArea( IplImage *convexHullImage, CvPoint *handpoint, int *hull, int hullcount ) {
-
- // ConvexHull̸_©çÈésñð¶¬
- CvPoint *hullpoint = ( CvPoint * )malloc( sizeof( CvPoint ) * hullcount );
- CvMat hMatrix = cvMat( 1, hullcount, CV_32SC2, hullpoint );
- for( int i = 0; i < hullcount; i++ ) {
- hullpoint[i]=handpoint[hull[i]];
- }
-
- // ConvexHullàÌ_Ìð¦é
- int hullarea = 0;
- for( int x = 0; x < convexHullImage->width; x++ ) {
- for( int y = 0;y < convexHullImage->height; y++ ) {
- if( cvPointPolygonTest( &hMatrix, cvPoint2D32f( x, y ), 0 ) > 0) {
- hullarea++;
- }
- }
- }
-
- free( hullpoint );
- return hullarea;
-}
-
- //
- // WPÌ»èðs¤
- //
- // ø:
- // handarea : èÌæÌÊÏ
- // hullarea : ConvexHullàÌÊÏ
- //
-void decide( int handarea, int hullarea ) {
- double ratio; // ConvexHullàÌÊÏÉηéèÌæÌÊÏÌ
-
- ratio=handarea / ( double )hullarea;
- printf( "Ratio = %lf\n", ratio );
-
- if( ratio >= ROCKMIN && ratio <= ROCKMAX ) {
- printf( "O[\n" );
- } else if( ratio >= SCISSORMIN && ratio <= SCISSORMAX ) {
- printf( "`L\n" );
- } else if( ratio >= PAPERMIN && ratio <= PAPERMAX ) {
- printf( "p[\n" );
- }
-}
-
/*!
* @brief constructor
* @param manager Maneger Object
@@ -262,7 +85,8 @@
// <rtc-template block="initializer">
: RTC::DataFlowComponentBase(manager),
m_img_inputIn("image_input", m_img_input),
- m_img_outputOut("image_output", m_img_output)
+ m_img_outputOut("image_output", m_img_output),
+ m_resultOut("result", m_result)
// </rtc-template>
{
@@ -276,7 +100,6 @@
}
-
RTC::ReturnCode_t RockPaperScissors::onInitialize()
{
// Registration: InPort/OutPort/Service
@@ -286,6 +109,7 @@
// Set OutPort buffer
addOutPort("image_output", m_img_outputOut);
+ addOutPort("result", m_resultOut);
// Set service provider to Ports
@@ -297,8 +121,13 @@
// <rtc-template block="bind_config">
// Bind variables and configuration variable
- bindParameter("image_height", m_img_height, "240");
- bindParameter("image_width", m_img_width, "320");
+ bindParameter("rock_max", m_rock_max, "1.0");
+ bindParameter("rock_min", m_rock_min, "0.85");
+ bindParameter("scissor_max", m_scissor_max, "0.85");
+ bindParameter("scissor_min", m_scissor_min, "0.7");
+ bindParameter("paper_max", m_paper_max, "0.7");
+ bindParameter("paper_min", m_paper_min, "0.5");
+ bindParameter("iterations", m_iterations, "4");
bindParameter("out_mode", m_out_mode, "1");
// </rtc-template>
@@ -330,13 +159,14 @@
RTC::ReturnCode_t RockPaperScissors::onActivated(RTC::UniqueId ec_id)
{
- m_image_buff = NULL; // üÍImage
- m_hsv_buff = NULL; // HSVp
- m_convexHull_buff = NULL; // ConvexHullp
- m_skin_buff = NULL; // §Fop
- m_temp_buff = NULL; // êÛ¶p
- m_label_buff = NULL; // xÊÛ¶p
- m_output_buff = NULL; // oÍp
+ m_image_buff = NULL; // å
¥åImage
+ m_hsv_buff = NULL; // HSVç¨
+ m_convexHull_buff = NULL; // ConvexHullç¨
+ m_skin_buff = NULL; // èè²æ½åºç¨
+ m_temp_buff = NULL; // ä¸æä¿åç¨
+ m_label_buff = NULL; // ã©ãã«çµæä¿åç¨
+ m_output_buff = NULL; // åºåç¨
+ m_prev_judge = ""; // æªå¤å®
return RTC::RTC_OK;
}
@@ -372,68 +202,66 @@
RTC::ReturnCode_t RockPaperScissors::onExecute(RTC::UniqueId ec_id)
{
- int key;
-
- //Vf[^Ì`FbN
+ //æ°ãã¼ã¿ã®ãã§ãã¯
if(m_img_inputIn.isNew()){
- //f[^ÌÇÝÝ
+ //ãã¼ã¿ã®èªã¿è¾¼ã¿
m_img_inputIn.read();
- m_image_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // üÍImage
- m_hsv_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // HSVp
- m_convexHull_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // ConvexHullp
- m_skin_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 1); // §Fop
- m_temp_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 1); // êÛ¶p
- m_label_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_16S, 1); // xÊÛ¶p
- m_output_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // oÍp
+ m_image_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // å
¥åImage
+ m_hsv_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // HSVç¨
+ m_convexHull_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // ConvexHullç¨
+ m_skin_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 1); // èè²æ½åºç¨
+ m_temp_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 1); // ä¸æä¿åç¨
+ m_label_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_16S, 1); // ã©ãã«çµæä¿åç¨
+ m_output_buff = cvCreateImage(cvSize(m_img_input.width, m_img_input.height), IPL_DEPTH_8U, 3); // åºåç¨
- //InPortÌfÌæ¾
+ //InPortã®æ åã®åå¾
memcpy(m_image_buff->imageData,(void *)&(m_img_input.pixels[0]),m_img_input.pixels.length());
- // §Fðo·éB
- extractSkinColor( m_image_buff, m_hsv_buff, m_skin_buff);
+ // èè²ãæ½åºããã
+ extractSkinColor();
- // ¹ÌæðâÔ·é
- interpolate( m_skin_buff, m_temp_buff );
+ // æ¬ æé åãè£éãã
+ interpolate();
- // xOðs¤
+ // ã©ããªã³ã°ãè¡ã
Label *labeling = createLabeling();
exec( labeling, m_skin_buff, m_label_buff, true, IGNORE_SIZE );
if(getNumOfResultRegions( labeling ) > 0 ) {
- // IGNORE_SIZEæèàå«ÈÌæª Á½ê
- int handarea; // èÌæÌÊÏ
- int hullarea; // ConvexHullàÌÊÏ
- int hullcount; // ConvexHull̸_Ì
- CvPoint *handpoint; // èÌæàÌ_ÌÀWzñ
- int *hull; // ConvexHull̸_Ìhandpointɨ¯éindexÔ
- CvMat pointMatrix; // èÌæpsñ
- CvMat hullMatrix; // ConvexHullpsñ
+ // IGNORE_SIZEããã大ããªé åããã£ãå ´å
+ int handarea; // æé åã®é¢ç©
+ int hullarea; // ConvexHullå
ã®é¢ç©
+ int hullcount; // ConvexHullã®é ç¹ã®æ°
+ CvPoint *handpoint; // æé åå
ã®ç¹ã®åº§æ¨é
å
+ int *hull; // ConvexHullã®é ç¹ã®handpointã«ãããindexçªå·
+ CvMat pointMatrix; // æé åç¨è¡å
+ CvMat hullMatrix; // ConvexHullç¨è¡å
- // ÅåÌæ(èÌæ)Ìoðs¤
- handarea = pickupMaxArea( m_skin_buff, m_label_buff, m_convexHull_buff );
+ // æ大é å(æé å)ã®æ½åºãè¡ã
+ handarea = pickupMaxArea();
- // ConvexHull𶬷é
- createConvexHull( m_skin_buff, handarea, &handpoint, &hull, &pointMatrix, &hullMatrix );
+ // ConvexHullãçæãã
+ createConvexHull( handarea, &handpoint, &hull, &pointMatrix, &hullMatrix );
hullcount = hullMatrix.cols;
- // ConvexHullð`æ·é
- drawConvexHull( m_convexHull_buff, handpoint, hull, hullcount );
+ // ConvexHullãæç»ãã
+ drawConvexHull( handpoint, hull, hullcount );
- // ConvexHullàÌÊÏðßé
- hullarea = calcConvexHullArea( m_convexHull_buff, handpoint,hull, hullcount );
+ // ConvexHullå
ã®é¢ç©ãæ±ãã
+ hullarea = calcConvexHullArea( handpoint,hull, hullcount );
- // WPÌ»èðs¤
+ // ã¸ã£ã³ã±ã³ã®å¤å®ãè¡ã
decide( handarea, hullarea );
- // ððú·é
+ // ã¡ã¢ãªã解æ¾ãã
free( handpoint );
free( hull );
-
+
} else {
- // æðú»·é
+ // ç»åãåæåãã
cvSetZero( m_convexHull_buff );
}
@@ -441,23 +269,23 @@
releaseLabeling( labeling );
if ( m_skin_buff->origin == 0 ) {
- //@¶ãª´_Ìê
+ //ãå·¦ä¸ãåç¹ã®å ´å
cvFlip( m_skin_buff, m_skin_buff, 0 );
}
if ( m_convexHull_buff->origin == 0 ) {
- //@¶ãª´_Ìê
+ //ãå·¦ä¸ãåç¹ã®å ´å
cvFlip( m_convexHull_buff, m_convexHull_buff, 0 );
}
- // æf[^ÌTCYæ¾
+ // ç»åãã¼ã¿ã®ãµã¤ãºåå¾
double len = (m_output_buff->nChannels * m_output_buff->width * m_output_buff->height);
m_img_output.pixels.length(len);
- // YÌC[WðMemCopy·é
+ // 該å½ã®ã¤ã¡ã¼ã¸ãMemCopyãã
memcpy((void *)&(m_img_output.pixels[0]), m_convexHull_buff->imageData, len);
- // ½]µ½æf[^ðOutPort©çoÍ·éB
+ // å転ããç»åãã¼ã¿ãOutPortããåºåããã
m_img_output.width = m_image_buff->width;
m_img_output.height = m_image_buff->height;
@@ -511,8 +339,220 @@
}
*/
+//
+// èè²ãæ½åºãã
+//
+void RockPaperScissors::extractSkinColor( void )
+{
+ CvScalar color; // HSV表è²ç³»ã§è¡¨ããè²
+ unsigned char h; // Hæå
+ unsigned char s; // Sæå
+ unsigned char v; // Væå
+
+ // BGRããHSVã«å¤æãã
+
+ cvCvtColor( m_image_buff, m_hsv_buff, CV_BGR2HSV );
+
+ //èè²æ½åº
+ for( int x = 0; x < m_skin_buff->width; x++ ) {
+ for( int y = 0 ; y < m_skin_buff->height; y++ ) {
+ color = cvGet2D( m_hsv_buff, y, x );
+ h = color.val[0];
+ s = color.val[1];
+ v = color.val[2];
+ if( h <= HMAX && h >= HMIN &&
+ s <= SMAX && s >= SMIN &&
+ v <= VMAX && v >= VMIN ) {
+ // èè²ã®å ´å
+ cvSetReal2D( m_skin_buff, y, x, 255 );
+ } else {
+ cvSetReal2D( m_skin_buff, y, x, 0 );
+ }
+ }
+
+ }
+}
+
+//
+// æ¬ æé åãè£éãã
+//
+void RockPaperScissors::interpolate( void )
+{
+ //è¨å¼µãITERATIONSåè¡ã
+ cvDilate( m_skin_buff, m_temp_buff, NULL, m_iterations );
+
+ //å縮ãITERATIONSåè¡ã
+ cvErode( m_temp_buff, m_skin_buff, NULL, m_iterations );
+}
+
+//
+// æ大é å(æé å)ã®æ½åºãè¡ã
+//
+// æ»ãå¤:
+// æé åã®é¢ç©
+//
+int RockPaperScissors::pickupMaxArea( void )
+{
+ int handarea = 0; // æé åã®é¢ç©
+
+ for(int x = 0; x < m_skin_buff->width; x++ ) {
+ for( int y=0; y < m_skin_buff->height; y++ ) {
+ if( cvGetReal2D( m_label_buff, y, x ) == 1 ) {
+ // æ大é åã ã£ãå ´å
+ handarea++;
+ cvSet2D( m_convexHull_buff, y, x, CV_RGB( 255, 255, 255 ) );
+ } else {
+ cvSetReal2D( m_skin_buff, y, x, 0 );
+ cvSet2D( m_convexHull_buff, y, x, CV_RGB( 0, 0, 0 ) );
+ }
+ }
+ }
+ return handarea;
+}
+
+//
+// ConvexHullãçæãã
+//
+// å¼æ°:
+// handarea : æé åã®é¢ç©(ç¹ã®æ°)
+// handpoint : æé åå
ã®ç¹ã®åº§æ¨é
åã¸ã®ãã¤ã³ã¿
+// hull : ConvexHullã®é ç¹ã®handpointã«ãããindexçªå·ã¸ã®ãã¤ã³ã¿
+// pointMatrix : æé åç¨è¡åã¸ã®ãã¤ã³ã¿
+// hullMatrix : ConvexHullç¨è¡åã¸ã®ãã¤ã³ã¿
+//
+void RockPaperScissors::createConvexHull( int handarea, CvPoint **handpoint, int **hull,
+ CvMat *pointMatrix, CvMat *hullMatrix )
+{
+ int i=0;
+
+ // ConvexHullãè¨ç®ããããã«å¿
è¦ãªè¡åãçæãã
+ *handpoint=( CvPoint * )malloc( sizeof( CvPoint ) * handarea );
+ *hull = ( int * )malloc( sizeof( int ) * handarea );
+ *pointMatrix = cvMat( 1, handarea, CV_32SC2, *handpoint );
+ *hullMatrix = cvMat( 1, handarea, CV_32SC1, *hull );
+
+ for( int x = 0; x < m_skin_buff->width; x++ ) {
+ for( int y = 0; y < m_skin_buff->height; y++ ) {
+ if( cvGetReal2D( m_skin_buff, y, x ) == 255 ) {
+ ( *handpoint )[i].x = x;
+ ( *handpoint )[i].y = y;
+ i++;
+ }
+ }
+ }
+
+ // ConvexHullãçæãã
+ cvConvexHull2( pointMatrix, hullMatrix, CV_CLOCKWISE, 0 );
+}
+
+//
+// ConvexHullãæç»ãã
+//
+// å¼æ°:
+// handpoint : æé åå
ã®ç¹ã®åº§æ¨é
å
+// hull : ConvexHullã®é ç¹ã®handpointã«ãããindexçªå·
+// hullcount : ConvexHullã®é ç¹ã®æ°
+//
+void RockPaperScissors::drawConvexHull( CvPoint *handpoint, int *hull, int hullcount )
+{
+ CvPoint pt0 = handpoint[hull[hullcount-1]];
+ for( int i = 0; i < hullcount; i++ ) {
+ CvPoint pt = handpoint[hull[i]];
+ cvLine( m_convexHull_buff, pt0, pt, CV_RGB( 0, 255, 0 ) );
+ pt0 = pt;
+ }
+}
+
+//
+// ConvexHullå
ã®é¢ç©ãæ±ãã
+//
+// å¼æ°:
+// handpoint : æé åå
ã®ç¹ã®åº§æ¨é
å
+// hull : ConvexHullã®é ç¹ã®handpointã«ãããindexçªå·
+// hullcount : ConvexHullã®é ç¹ã®æ°ãã
+//
+// æ»ãå¤:
+// ConvexHullå
ã®é¢ç©
+//
+int RockPaperScissors::calcConvexHullArea( CvPoint *handpoint, int *hull, int hullcount )
+{
+ // ConvexHullã®é ç¹ãããªãè¡åãçæ
+ CvPoint *hullpoint = ( CvPoint * )malloc( sizeof( CvPoint ) * hullcount );
+
+ CvMat hMatrix = cvMat( 1, hullcount, CV_32SC2, hullpoint );
+ for( int i = 0; i < hullcount; i++ ) {
+ hullpoint[i]=handpoint[hull[i]];
+ }
+
+ // ConvexHullå
ã®ç¹ã®æ°ãæ°ãã
+ int hullarea = 0;
+ for( int x = 0; x < m_convexHull_buff->width; x++ ) {
+ for( int y = 0;y < m_convexHull_buff->height; y++ ) {
+
+ if( cvPointPolygonTest( &hMatrix, cvPoint2D32f( x, y ), 0 ) > 0) {
+ hullarea++;
+ }
+ }
+ }
+
+ free( hullpoint );
+ return hullarea;
+}
+
+
+//
+// ã¸ã£ã³ã±ã³ã®å¤å®ãè¡ã
+//
+// å¼æ°:
+// handarea : æé åã®é¢ç©
+// hullarea : ConvexHullå
ã®é¢ç©
+//
+void RockPaperScissors::decide( int handarea, int hullarea )
+{
+ double ratio; // ConvexHullå
ã®é¢ç©ã«å¯¾ããæé åã®é¢ç©ã®å²å
+ string judge = "é¾å¤å¤";
+
+ ratio=handarea / ( double )hullarea;
+
+ if( ratio >= m_rock_min && ratio <= m_rock_max ) {
+ judge = "ã°ã¼";
+ }
+ else if( ratio >= m_scissor_min && ratio <= m_scissor_max )
+ {
+ judge = "ãã§ã";
+ }
+ else if( ratio >= m_paper_min && ratio <= m_paper_max )
+ {
+ judge = "ãã¼";
+ }
+
+ if( m_out_mode == 1 )
+ {
+ //ååã¨ã¸ã£ã³ã±ã³ã®ç¨®é¡ãç°ãªãå ´åã®ã¿åºåãã
+ //ConvexHullé åã¯ä¸å®ä»¥ä¸ã®å¤§ããã§ãªãã¨ãã¸ã£ã³ã±ã³ã®å¤å®ã¯æå³ããªã
+ //ããã§ã¯æ«å®çã«10000以ä¸ã®é åã«å¯¾ãã¦ã¸ã£ã³ã±ã³ãå¤å®ãããã®ã¨ãã
+ if( judge != "é¾å¤å¤" && m_prev_judge != judge && hullarea >= 10000 )
+ {
+ //printf( "Ratio = %lf\n", ratio );
+ printf( "Ratio = %lf : å¤å®é åã®å¤§ãã = %d\n", ratio, hullarea );
+ printf( "%s\n", judge.c_str() );
+ m_prev_judge = judge;
+
+ m_result.data = judge.c_str();
+ m_resultOut.write();
+ }
+ }
+ else
+ {
+ //ãã¹ã¦ã®çµæãåºåãã
+ printf( "Ratio = %lf : å¤å®é åã®å¤§ãã = %d\n", ratio, hullarea );
+ printf( "%s\n", judge.c_str() );
+ }
+}
+
+
extern "C"
{
More information about the openrtm-commit
mailing list