contour.cpp
Go to the documentation of this file.
00001 // Software License Agreement (BSD License)
00002 // 
00003 //   Copyright (c) 2011, Shulei Zhu <schuleichu@gmail.com>
00004 //   All rights reserved.
00005 // 
00006 //   Redistribution and use in source and binary forms, with or without
00007 //   modification, are permitted provided that the following conditions
00008 //   are met:
00009 // 
00010 //    * Redistributions of source code must retain the above copyright
00011 //      notice, this list of conditions and the following disclaimer.
00012 //    * Redistributions in binary form must reproduce the above
00013 //      copyright notice, this list of conditions and the following
00014 //      disclaimer in the documentation and/or other materials provided
00015 //      with the distribution.
00016 //    * Neither the name of Shulei Zhu nor the names of its
00017 //      contributors may be used to endorse or promote products derived
00018 //      from this software without specific prior written permission.
00019 // 
00020 //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 //   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 //   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 //   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 //   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 //   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 //   POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // 
00034 // contour.cpp --- 
00035 // File            : contour.cpp
00036 // Created: Sa Jun 18 14:05:01 2011 (+0200)
00037 // Author: Shulei Zhu
00038 
00039 // Code:
00040 
00041 
00042 
00043 #include "opencv/cv.h"
00044 #include "opencv/highgui.h"
00045 #include <iostream>
00046 #include <vector>
00047 #include <string>
00048 #include "ccd/sift_init.h"
00049 #include "ccd/bspline.h"
00050 #include <fstream>
00051 using namespace std;
00052 using namespace cv;
00053 vector<cv::Point3d> pts;
00054 void on_mouse(int event, int x, int y, int flags, void* param )
00055 {
00056   //  MaskParams* params = (MaskParams*)_params;
00057   
00058   cv::Mat *image = (cv::Mat *)param;
00059   if( image->empty())
00060     return;
00061 
00062   //caution: check
00063   // if( image1.at<double>() )
00064   //   y = image1->height - y;
00065 
00066   switch( event )
00067   {
00068     case CV_EVENT_LBUTTONDOWN:
00069       break;
00070     case CV_EVENT_LBUTTONUP:
00071       cv::circle(*image,cv::Point(x,y),2,cv::Scalar(0,0,255),2);
00072       pts.push_back(cv::Point3d(x,y,1.0));
00073       cv::imshow("Contour", *image);
00074       break;
00075   }
00076 }
00077 
00078 int main (int argc, char * argv[]) 
00079 {
00080   char key;
00081   cv::Mat tpl = cv::imread(argv[1]);
00082   cv::namedWindow("Contour", 1);
00083   cv::setMouseCallback( "Contour", on_mouse,  (void*)&tpl);
00084   cv::imshow("Contour", tpl);
00085   while (1)
00086   {
00087     key = cv::waitKey(10);
00088     if (key == 27) break;
00089   }
00090   cv::Mat contour_mat(pts.size(),3,CV_64FC1);
00091   for (size_t i = 0; i < pts.size(); ++i){
00092     double *ptr = contour_mat.ptr<double>(i);
00093     ptr[0] = pts[i].x;
00094     ptr[1] = pts[i].y;
00095     ptr[2] = pts[i].z;
00096   }
00097 
00098   FileStorage fs("contour.xml", cv::FileStorage::WRITE);
00099   fs.open("contour.xml", FileStorage::WRITE);
00100   fs<< "contour_points" <<  contour_mat ;
00101   fs.release();
00102 
00103   
00104   CvFileStorage* new_fs= cvOpenFileStorage("contour.xml", 0, CV_STORAGE_READ);
00105   CvMat *coordinates= (CvMat*)cvReadByName(new_fs,  NULL, "contour_points", NULL);
00106   // CvMat *coordinates_t = cvCreateMat(3 ,coordinates->rows,  CV_64FC1);
00107   int step = coordinates->step/sizeof(double);
00108   double *ptr = coordinates->data.db;
00109   vector<cv::Point3d> test(pts.size());     
00110   for (int i = 0; i < coordinates->rows; ++i)
00111   {
00112       test[i].x = (ptr+i*step)[0];
00113       test[i].y = (ptr+i*step)[1];
00114       test[i].z = (ptr+i*step)[2];
00115       std::cout << test[i].x  << " " << test[i].y  << " " <<test[i].z<< std::endl;
00116     }
00117   
00118   // cv::Mat test_mat(pts.size(), 2,CV_64FC1);;
00119   // vector<cv::Point2d> test(pts.size());
00120   // if (!fs.isOpened()){
00121   //   fs.open("contour.xml", FileStorage::READ);
00122   //   fs["contour_points"] >> test_mat ;
00123   //   for (size_t i = 0; i < pts.size(); ++i)
00124   //   {
00125   //     double *ptr = test_mat.ptr<double>(i);
00126   //     test[i].x = ptr[0];
00127   //     test[i].y = ptr[1];
00128   //     std::cout << test[i].x  << " " << test[i].y << std::endl;
00129   //   }
00130   //   fs.release();
00131   // }
00132   return 0;
00133 }


contracting_curve_density_algorithm
Author(s): Shulei Zhu, Dejan Pangercic
autogenerated on Mon Oct 6 2014 10:42:03