00001 #include <iostream>
00002 #include <vector>
00003 #include <opencv/cv.h>
00004 #include <opencv/highgui.h>
00005 #include "ccd/bspline.h"
00006 #include "ccd/ccd.h"
00007 using namespace std;
00008 using namespace cv;
00009 std::vector<cv::Point3d> pts;
00010 void on_mouse(int event, int x, int y, int flags, void *param )
00011 {
00012 CCD *my_ccd = (CCD *)param;
00013 cv::Mat image = my_ccd->canvas;
00014 if( image.empty())
00015 return ;
00016
00017
00018
00019
00020 switch( event )
00021 {
00022 case CV_EVENT_LBUTTONDOWN:
00023 break;
00024 case CV_EVENT_LBUTTONUP:
00025 cv::circle(image,cv::Point(x,y),1,cv::Scalar(0,0,255),1);
00026 pts.push_back(cv::Point3d(x,y,1));
00027 cv::imshow("B-spline", image);
00028 break;
00029 }
00030 }
00031 void contourManually(CCD &my_ccd)
00032 {
00033 int key;
00034 cv::namedWindow("B-spline", 1);
00035 cv::setMouseCallback( "B-spline", on_mouse, (void*)&my_ccd);
00036 cv::imshow("B-spline", my_ccd.canvas);
00037 while (1)
00038 {
00039 key = cv::waitKey(10);
00040 if (key == 27) break;
00041 }
00042 }
00043
00044 int main (int argc, char * argv[])
00045 {
00046 cv::namedWindow("B-spline", 1);
00047 cv::Mat image = cv::Mat::ones(600, 800, CV_8UC3);
00048 for (int i = 0; i < image.rows; ++i){
00049 for (int j =0; j < image.cols; ++j){
00050 image.at<Vec3b>(i,j)[0] = 255;
00051 image.at<Vec3b>(i,j)[1] = 255;
00052 image.at<Vec3b>(i,j)[2] = 255;
00053 }
00054 }
00055
00056 CCD my_ccd;
00057 image.copyTo(my_ccd.canvas);
00058 image.copyTo(my_ccd.image);
00059 contourManually(my_ccd);
00060
00061
00062
00063
00064
00065 for (int i = 0; i < pts.size()-1; ++i)
00066 {
00067 int j = (i+1)%pts.size();
00068 std::cout << pts[i].x << " " << pts[i].y << std::endl;
00069 cv::circle(my_ccd.canvas, cv::Point2d(pts[i].x, pts[i].y), 2, CV_RGB(0,255,0), 2);
00070 cv::line(my_ccd.canvas, cv::Point2d(pts[i].x, pts[i].y),cv::Point2d(pts[i+1].x, pts[i+1].y),CV_RGB( 0, 0, 0 ),2,8,0);
00071 }
00072 cv::circle(my_ccd.canvas, cv::Point2d(pts[pts.size()-1].x, pts[pts.size()-1].y), 2, CV_RGB(0,255,0), 2);
00073
00074 my_ccd.read_params("ccd_params1.xml");
00075 BSpline bs1(my_ccd.degree() , my_ccd.resolution(), pts);
00076 for (int i = 0; i < my_ccd.resolution()-1; ++i)
00077 {
00078 int j = (i+1)%(int)my_ccd.resolution();
00079 cv::line(my_ccd.canvas, cv::Point2d(bs1[i].x, bs1[i].y),cv::Point2d(bs1[i+1].x, bs1[i+1].y),CV_RGB( 255, 0, 0 ),2,8,0);
00080 }
00081
00082 my_ccd.read_params("ccd_params.xml");
00083 BSpline bs2(my_ccd.degree() , my_ccd.resolution(), pts);
00084 for (int i = 0; i < my_ccd.resolution()-1; ++i)
00085 {
00086 int j = (i+1)%(int)my_ccd.resolution();
00087 cv::line(my_ccd.canvas, cv::Point2d(bs2[i].x, bs2[i].y),cv::Point2d(bs2[i+1].x, bs2[i+1].y),CV_RGB(0, 0, 255 ),2,8,0);
00088 }
00089 char key;
00090 cv::imshow("B-spline", my_ccd.canvas);
00091 cv::imwrite("bspline.jpg", my_ccd.canvas);
00092 while (1)
00093 {
00094 key = cv::waitKey(10);
00095 if (key == 27) break;
00096 }
00097 return 0;
00098 }