Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <iostream>
00003 #include <ctime>
00004 #include <cstdio>
00005
00006 #include "opencv2/core/core.hpp"
00007 #include "opencv2/features2d/features2d.hpp"
00008 #include "opencv2/highgui/highgui.hpp"
00009 #include "opencv2/calib3d/calib3d.hpp"
00010
00011
00012 #include "opencv2/objdetect/objdetect.hpp"
00013 #include "opencv2/imgproc/imgproc.hpp"
00014
00015 #include "ros/ros.h"
00016
00017 using namespace cv;
00018 using namespace std;
00019
00020
00021 Mat frame,img,ROI;
00022 Rect cropRect(0,0,0,0);
00023 Point P1(0,0);
00024 Point P2(0,0);
00025
00026 const char* winName="Crop Image";
00027 bool clicked=false;
00028 int i=0;
00029 char imgName[15];
00030 std::string path_to_template = "/home/bwi/shiqi/template.jpg";
00031
00032
00033 void checkBoundary(){
00034
00035 if(cropRect.width>img.cols-cropRect.x)
00036 cropRect.width=img.cols-cropRect.x;
00037
00038 if(cropRect.height>img.rows-cropRect.y)
00039 cropRect.height=img.rows-cropRect.y;
00040
00041 if(cropRect.x<0)
00042 cropRect.x=0;
00043
00044 if(cropRect.y<0)
00045 cropRect.height=0;
00046 }
00047
00048 void showImage(){
00049 img=frame.clone();
00050 checkBoundary();
00051 if(cropRect.width>0&&cropRect.height>0){
00052 ROI=frame(cropRect);
00053 imshow("cropped",ROI);
00054 }
00055
00056 rectangle(img, cropRect, Scalar(0,255,0), 1, 8, 0 );
00057 imshow(winName,img);
00058 }
00059
00060
00061 void onMouse( int event, int x, int y, int f, void* ){
00062
00063 switch(event){
00064
00065 case CV_EVENT_LBUTTONDOWN :
00066 clicked=true;
00067
00068 P1.x=x;
00069 P1.y=y;
00070 P2.x=x;
00071 P2.y=y;
00072 break;
00073
00074 case CV_EVENT_LBUTTONUP :
00075 P2.x=x;
00076 P2.y=y;
00077 clicked=false;
00078 break;
00079
00080 case CV_EVENT_MOUSEMOVE :
00081 if(clicked){
00082 P2.x=x;
00083 P2.y=y;
00084 }
00085 break;
00086
00087 default : break;
00088
00089
00090 }
00091
00092
00093 if(clicked){
00094
00095 if(P1.x>P2.x){
00096 cropRect.x=P2.x;
00097 cropRect.width=P1.x-P2.x;
00098 }
00099 else {
00100 cropRect.x=P1.x;
00101 cropRect.width=P2.x-P1.x;
00102 }
00103
00104 if(P1.y>P2.y){
00105 cropRect.y=P2.y;
00106 cropRect.height=P1.y-P2.y;
00107 }
00108 else {
00109 cropRect.y=P1.y;
00110 cropRect.height=P2.y-P1.y;
00111 }
00112 }
00113
00114
00115 showImage();
00116
00117 }
00118
00119
00120 int main( int argc, char** argv )
00121 {
00122 if (argc != 1)
00123 return -1;
00124
00125 CvCapture* capture;
00126
00127 capture = cvCaptureFromCAM(0);
00128
00129 if (!capture) {
00130 ROS_ERROR("No camera detected! \n");
00131 return -1;
00132 }
00133
00134 namedWindow(winName, CV_WINDOW_AUTOSIZE);
00135
00136
00137 while (true) {
00138
00139 frame = cvQueryFrame(capture);
00140
00141 if (frame.empty()) {
00142 ROS_WARN("No captured frame -- Break! \n");
00143 break;
00144 }
00145
00146 imshow( winName, frame);
00147 char input = waitKey(10);
00148
00149 if (input == 's') {
00150 std::time_t rawtime;
00151 std::tm* timeinfo;
00152 char buffer [80];
00153
00154 cout<<"Click and drag for Selection"<<endl<<endl;
00155 cout<<"------> Press 's' to save"<<endl<<endl;
00156
00157 cout<<"------> Press '8' to move up"<<endl;
00158 cout<<"------> Press '2' to move down"<<endl;
00159 cout<<"------> Press '6' to move right"<<endl;
00160 cout<<"------> Press '4' to move left"<<endl<<endl;
00161
00162 cout<<"------> Press 'w' increas top"<<endl;
00163 cout<<"------> Press 'x' increas bottom"<<endl;
00164 cout<<"------> Press 'd' increas right"<<endl;
00165 cout<<"------> Press 'a' increas left"<<endl<<endl;
00166
00167 cout<<"------> Press 't' decrease top"<<endl;
00168 cout<<"------> Press 'b' decrease bottom"<<endl;
00169 cout<<"------> Press 'h' decrease right"<<endl;
00170 cout<<"------> Press 'f' decrease left"<<endl<<endl;
00171
00172 cout<<"------> Press 'r' to reset"<<endl;
00173 cout<<"------> Press 'Esc' to quit"<<endl<<endl;
00174
00175
00176
00177
00178 setMouseCallback(winName,onMouse,NULL );
00179 imshow(winName,frame);
00180
00181 while(1){
00182
00183 char c=waitKey();
00184 if(c=='s'&&ROI.data)
00185 {
00186
00187
00188 imwrite(path_to_template, ROI);
00189 ROS_INFO(" template saved: %s\n", path_to_template.c_str());
00190 break;
00191 }
00192 if(c=='6') cropRect.x++;
00193 if(c=='4') cropRect.x--;
00194 if(c=='8') cropRect.y--;
00195 if(c=='2') cropRect.y++;
00196
00197 if(c=='w') { cropRect.y--; cropRect.height++;}
00198 if(c=='d') cropRect.width++;
00199 if(c=='x') cropRect.height++;
00200 if(c=='a') { cropRect.x--; cropRect.width++;}
00201
00202 if(c=='t') { cropRect.y++; cropRect.height--;}
00203 if(c=='h') cropRect.width--;
00204 if(c=='b') cropRect.height--;
00205 if(c=='f') { cropRect.x++; cropRect.width--;}
00206
00207 if(c==27) break;
00208 if(c=='r') {
00209 cropRect.x=0;cropRect.y=0;cropRect.width=0;
00210 cropRect.height=0;
00211 }
00212
00213 showImage();
00214 }
00215 }
00216 }
00217
00218 return 0;
00219 }
00220