{"action":"created","issue":{"url":"https://api.github.com/repos/jhako/osakana_prj/issues/15","repository_url":"https://api.github.com/repos/jhako/osakana_prj","labels_url":"https://api.github.com/repos/jhako/osakana_prj/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/jhako/osakana_prj/issues/15/comments","events_url":"https://api.github.com/repos/jhako/osakana_prj/issues/15/events","html_url":"https://github.com/jhako/osakana_prj/issues/15","id":138679308,"number":15,"title":"OpenCVのマウスコールバックに関するエラー","user":{"login":"Teppei-Kanayama","id":12081407,"avatar_url":"https://avatars.githubusercontent.com/u/12081407?v=3","gravatar_id":"","url":"https://api.github.com/users/Teppei-Kanayama","html_url":"https://github.com/Teppei-Kanayama","followers_url":"https://api.github.com/users/Teppei-Kanayama/followers","following_url":"https://api.github.com/users/Teppei-Kanayama/following{/other_user}","gists_url":"https://api.github.com/users/Teppei-Kanayama/gists{/gist_id}","starred_url":"https://api.github.com/users/Teppei-Kanayama/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Teppei-Kanayama/subscriptions","organizations_url":"https://api.github.com/users/Teppei-Kanayama/orgs","repos_url":"https://api.github.com/users/Teppei-Kanayama/repos","events_url":"https://api.github.com/users/Teppei-Kanayama/events{/privacy}","received_events_url":"https://api.github.com/users/Teppei-Kanayama/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-05T13:33:31Z","updated_at":"2016-03-06T00:24:44Z","closed_at":null,"body":"エラーメッセージ\r\n```\r\n/home/teppei/mayfes/osakana_prj/src/main.cpp:198:52: error: cannot convert ‘Pers::onMouse’ from type ‘void (Pers::)(int, int, int, int, void*)’ to type ‘cv::MouseCallback {aka void (*)(int, int, int, int, void*)}’\r\n  cv::setMouseCallback(\"Capture\", p_pers->onMouse, 0);\r\n                                                    ^\r\nmake[2]: *** [CMakeFiles/osakana_prj.dir/src/main.cpp.o] エラー 1\r\nmake[1]: *** [CMakeFiles/osakana_prj.dir/all] エラー 2\r\nmake: *** [all] エラー 2\r\n```\r\n\r\n関係がありそうなコード\r\nmain.cpp\r\n```\r\nint main(int argc, char *argv[])\r\n{\r\n\t//乱数のseedを初期化\r\n\tsrand((unsigned)time(NULL));\r\n\r\n\t//ワールドサイズ\r\n\tint w = 640; int h = 640;\r\n\r\n\t//glutの初期化およびwindowの定義\r\n\tglutInit(&argc, argv);\r\n\t//ALPHA値有効，ダブルバッファリング，Zバッファ有効\r\n\tglutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);\r\n\t//ウィンドウサイズ，タイトルの設定\r\n\tglutInitWindowSize(w, h);\r\n\tglutCreateWindow(\"osakana_prj\");\r\n\t//\r\n\tglEnable(GL_DEPTH_TEST);\r\n/*\r\n// /* -- 3D --\r\n\t//射影行列を設定\r\n\tglMatrixMode(GL_PROJECTION);\r\n\t//単位行列に初期化\r\n\tglLoadIdentity();\r\n\t//視界の設定\r\n\tgluPerspective(60.0, (double)w / h, 0.1, 1000.0);\r\n\t//視点\r\n\tgluLookAt(\r\n\t\t320.0, 0.0, 500.0, // 視点の位置x,y,z;\r\n\t\t320.0, 320.0, 0.0,   // 視界の中心位置の参照点座標x,y,z\r\n\t\t0.0, 0.0, 1.0);  //視界の上方向のベクトルx,y,z\r\n// */\r\n\r\n/* -- 2D --\r\n\t//射影行列を設定\r\n\tglMatrixMode(GL_PROJECTION);\r\n\t//単位行列に初期化\r\n\tglLoadIdentity();\r\n\t//画面の座標をワールド座標に対応\r\n\tglOrtho(0.0, w, h, 0.0, -1.0, 1.0);\r\n\t//ビューポート変換\r\n\tglViewport(0, 0, w, h);\r\n*/\r\n\t//クリアカラーの設定（水色）\r\n\tglClearColor(240.0 / 255, 248.0 / 255, 1.0, 0);\r\n\t//各コールバック関数の設定\r\n\tglutDisplayFunc(display);\r\n\tglutKeyboardFunc(keyboard);\r\n\tglutMouseFunc(mouse);\r\n\tglutMotionFunc(motion); //マウス移動\r\n\tglutIdleFunc(update);\r\n\t//αテストの判定値\r\n\tglAlphaFunc(GL_GEQUAL, 0.5);\r\n\t//光源の作成\r\n\tGLfloat light_position0[] = {320.0, 320.0, 30.0, 1.0}; //光源0の座標\r\n\tconst GLfloat lightcol[] = {1.0f, 1.0f, 1.0f, 1.0f }; // 直接光強度\r\n\tconst GLfloat lightamb[] = {0.3f, 0.3f, 0.3f, 1.0f}; // 環境光強度\r\n\tglLightfv(GL_LIGHT0, GL_POSITION, light_position0);\r\n\tglLightfv(GL_LIGHT0, GL_DIFFUSE, lightcol);\r\n\tglLightfv(GL_LIGHT0, GL_SPECULAR, lightcol);\r\n\tglLightfv(GL_LIGHT0, GL_AMBIENT, lightamb);\r\n\tglLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);\r\n\t//スムースシェーディング（テスト）\r\n\tglShadeModel(GL_SMOOTH);\r\n\t//両面表示\r\n\tglEnable(GL_CULL_FACE);\r\n\r\n\tlast_time = std::chrono::system_clock::now();\r\n\r\n\tprintf(\"version : %s\\n\", glGetString(GL_VERSION));\r\n\r\n\t// GLEW初期化\r\n\tGLenum glewStatus = glewInit();\r\n\tif(glewStatus != GLEW_OK)\r\n\t{\r\n\t\tprintf(\"Error: %s\", glewGetErrorString(glewStatus));\r\n\t\tstd::exit(1);\r\n\t}\r\n\r\n\tp_world = new World(w, h);\r\n\tp_pers = new Pers();\r\n\r\n\t//--OpenCVの設定--\r\n\t//カメラの設定\r\n\tcap.set(CV_CAP_PROP_FRAME_WIDTH, 640);\r\n\tcap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);\r\n\tif(!cap.isOpened()) return -1;\r\n\t//ウィンドウの作成\r\n\tcv::namedWindow(\"Capture\", CV_WINDOW_AUTOSIZE | CV_WINDOW_FREERATIO);\r\n\t//トラックバーの作成\r\n\tint slider_value = 50;\r\n\tcv::createTrackbar(\"Hue min\", \"Capture\", &slider_value, 180);\r\n\tcv::createTrackbar(\"Hue max\", \"Capture\", &slider_value, 180);\r\n\tcv::createTrackbar(\"Satulation min\", \"Capture\", &slider_value, 180);\r\n\tcv::createTrackbar(\"Satulation max\", \"Capture\", &slider_value, 180);\r\n\tcv::createTrackbar(\"Value min\", \"Capture\", &slider_value, 180);\r\n\tcv::createTrackbar(\"Value max\", \"Capture\", &slider_value, 180);\r\n\r\n\t//マウスコールバックの設定\r\n\tcv::setMouseCallback(\"Capture\", p_pers->onMouse, 0);\r\n\r\n\t//メインループの実行\r\n\tglutMainLoop();\r\n\r\n\treturn 0;\r\n}\r\n```\r\n\r\nvision.h\r\n```\r\n#include <opencv/cv.h>\r\n\r\nvoid colorExtraction(cv::Mat*, cv::Mat*, int, int, int, int, int, int, int);\r\n\r\n\r\nclass Pers{\r\n private:\r\n  std::vector<int> pos_x, pos_y;\r\n public:\r\n  Pers();\r\n  void perspective(cv::Mat*, cv::Mat*);\r\n  void onMouse(int, int, int, int, void*);\r\n};\r\n```\r\n\r\nvision.cpp\r\n```\r\nvoid Pers::onMouse(int event, int x, int y, int flag, void*)\r\n{\r\n// マウスイベントを取得                                                                                                                        \r\nif(event ==  cv::EVENT_LBUTTONDOWN){\r\npos_x.push_back(x);\r\npos_y.push_back(y);\r\n}\r\n\r\nreturn;\r\n\r\n//std::cout << desc << \" (\" << pos_x << \", \" << pos_y << \")\" << std::endl;                                                                     \r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/jhako/osakana_prj/issues/comments/192766728","html_url":"https://github.com/jhako/osakana_prj/issues/15#issuecomment-192766728","issue_url":"https://api.github.com/repos/jhako/osakana_prj/issues/15","id":192766728,"user":{"login":"jhako","id":14149906,"avatar_url":"https://avatars.githubusercontent.com/u/14149906?v=3","gravatar_id":"","url":"https://api.github.com/users/jhako","html_url":"https://github.com/jhako","followers_url":"https://api.github.com/users/jhako/followers","following_url":"https://api.github.com/users/jhako/following{/other_user}","gists_url":"https://api.github.com/users/jhako/gists{/gist_id}","starred_url":"https://api.github.com/users/jhako/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jhako/subscriptions","organizations_url":"https://api.github.com/users/jhako/orgs","repos_url":"https://api.github.com/users/jhako/repos","events_url":"https://api.github.com/users/jhako/events{/privacy}","received_events_url":"https://api.github.com/users/jhako/received_events","type":"User","site_admin":false},"created_at":"2016-03-06T00:24:44Z","updated_at":"2016-03-06T00:24:44Z","body":"**問題点**：\r\nメンバ関数のポインタは，通常の関数ポインタに代入することが出来ない．\r\n\r\n**解決案**：\r\nメンバ関数の代わりに，staticな関数を用意してそれを媒介させる．\r\nまた，メンバ関数の呼び出しには，クラスのインスタンスが必要なので，それをparamとして渡す．\r\n\r\n```C++\r\nstatic void cv_onMouse(int event, int x, int y, int flag, void* param)\r\n{\r\n\t//Persのポインタとしてキャスト\r\n\tPers* pers = static_cast<Pers*>(param);\r\n\t//メンバ関数の呼び出し\r\n\tpers->onMouse(event, x, y, flag, nullptr);                          \r\n}\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    ...\r\n    //マウスコールバックの設定\r\n    cv::setMouseCallback(\"Capture\", cv_onMouse, p_pers);\r\n    ...\r\n}\r\n```"}}