PageRenderTime 299ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Contract_meeting/7_ObjectTrackingSIFT/findLocation.cpp

https://github.com/yianni/cappocacciaactivevision
C++ | 216 lines | 137 code | 33 blank | 46 comment | 14 complexity | 7fc370931aa6b39c899f1134c9a6888b MD5 | raw file
  1. // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
  2. /**
  3. * @ingroup icub_tutorials
  4. *
  5. * \defgroup findLocation Find Location
  6. *
  7. * This tutorials shows how to do basic
  8. * image processsing from images coming from
  9. * the robot or the simulator
  10. *
  11. * \author Tutorial Fitzpatrick
  12. */
  13. #include <stdio.h>
  14. // Get all OS and signal processing YARP classes
  15. #include <iostream>
  16. #include <time.h>
  17. #include <cv.h>
  18. #include <highgui.h>
  19. #include <cxcore.h>
  20. #include <yarp/os/all.h>
  21. #include <yarp/sig/all.h>
  22. #include "highgui.h"
  23. using namespace std;
  24. using namespace yarp::os;
  25. using namespace yarp::sig;
  26. int main() {
  27. Network yarp; // set up yarp
  28. BufferedPort<ImageOf<PixelRgb> > imagePort; // crea una istanza (la porta imagePort) dalla classe BudderedPort che manda/riceve un'immagine rgb in background senza stoppare il processo.
  29. BufferedPort<Vector> targetPort; //crea una istanza (la porta targetPort) della classe BufferedPort che manda/riceve un vettore
  30. imagePort.open("/tutorial/image/in"); // give the port a name
  31. targetPort.open("/tutorial/target/out");
  32. //Network::connect("/icubSim/cam/left","/tutorial/image/in");
  33. Network::connect("/icub/cam/left","/tutorial/image/in");//la porta /icub/cam/left รจ aperta dal modulo camera
  34. //training part
  35. while (1) { // repeat forever
  36. ImageOf<PixelRgb> *image = imagePort.read(); // read an image: crea un puntatore image del tipo ImageOf<PixelRgb> e gli assegna quello che viene letto sulla porta imagePort.
  37. if (image!=NULL) { // check we actually got something
  38. printf("We got an image of size %dx%d\n", image->width(), image->height());
  39. //pramod added nov 15
  40. IplImage *frame_temp = cvCreateImage( cvSize(image->width(), image->height()), IPL_DEPTH_8U, 1 );
  41. frame_temp = (IplImage*)image->getIplImage();
  42. IplImage *frame1 = cvCreateImage( cvSize(frame_temp->width, frame_temp->height), IPL_DEPTH_8U, 1 );
  43. cvCvtColor(frame_temp,frame1, CV_RGB2GRAY);
  44. cvSaveImage("object.pgm",frame1);
  45. //frame1->origin=0;
  46. // cvNamedWindow("cNameoriginal",CV_WINDOW_AUTOSIZE);
  47. // cvShowImage("cNameoriginal",frame_temp);
  48. //cvSaveImage("save_first.jpg",image1);
  49. //cvReleaseImage(&image1);
  50. cvNamedWindow("cNameselect",CV_WINDOW_AUTOSIZE);
  51. cvShowImage("cNameselect",frame1);
  52. cvReleaseImage(&frame1);
  53. char c = cvWaitKey(33);
  54. if(c==27) break;
  55. }
  56. }
  57. printf("breaked");
  58. system("/home/gabriele/siftWin32.exe <object.pgm >tmp1.txt");
  59. IplImage *image1 = cvLoadImage("object.pgm");
  60. FILE* fp1=fopen("tmp1.txt","rb");
  61. float *arr1x; float *arr1y; int *arr2;float scale,ori;int temp1, temp2;
  62. fscanf(fp1,"%d %d \n",&temp1, &temp2);
  63. //printf("%d %d \n",temp1,temp2);
  64. //getchar();
  65. arr1x=(float*)malloc(sizeof(float)*temp1);
  66. arr1y=(float*)malloc(sizeof(float)*temp1);
  67. arr2=(int*)malloc(sizeof(int)*temp2*temp1);
  68. for(int i2=1;i2<=temp1;i2++)
  69. {
  70. //printf("temp1 %d \n",i2);
  71. fscanf(fp1,"%f %f %f %f \n", &arr1x[i2-1], &arr1y[i2-1], &scale,&ori);
  72. //printf("%f %f %f %f \n", arr1x[i2-1], arr1y[i2-1], scale,ori);
  73. //getchar();
  74. for(int i3=1;i3<=temp2;i3++)
  75. {
  76. fscanf(fp1,"%d ", &arr2[(i2-1)*temp2+(i3-1)]);
  77. //printf("%d ", arr2[(i2-1)*temp2+(i3-1)]);
  78. }fscanf(fp1,"\n");printf("\n");
  79. cvCircle(image1,cvPoint((int)arr1x[i2-1],(int)arr1y[i2-1]),3,cvScalar(0,255,255,255),1);
  80. //getchar();
  81. }
  82. cvNamedWindow("cNameobject",CV_WINDOW_AUTOSIZE);
  83. cvShowImage("cNameobject",image1);
  84. printf("first one finished \n");
  85. fclose(fp1);
  86. //training part ends
  87. while (1) { // repeat forever
  88. ImageOf<PixelRgb> *image = imagePort.read(); // read an image: crea un puntatore image del tipo ImageOf<PixelRgb> e gli assegna quello che viene letto sulla porta imagePort.
  89. if (image!=NULL) { // check we actually got something
  90. printf("We got an image of size %dx%d\n", image->width(), image->height());
  91. //pramod added nov 15
  92. IplImage *frame_temp = cvCreateImage( cvSize(image->width(), image->height()), IPL_DEPTH_8U, 1 );
  93. frame_temp = (IplImage*)image->getIplImage();
  94. IplImage *frame1 = cvCreateImage( cvSize(frame_temp->width, frame_temp->height), IPL_DEPTH_8U, 1 );
  95. cvCvtColor(frame_temp,frame1, CV_RGB2GRAY);
  96. cvSaveImage("object_test.pgm",frame1);
  97. //frame1->origin=0;
  98. cvNamedWindow("cNameoriginal",CV_WINDOW_AUTOSIZE);
  99. cvShowImage("cNameoriginal",frame_temp);
  100. // cvWaitKey(0);
  101. //pramod added ends
  102. system("/home/gabriele/siftWin32.exe <object_test.pgm >tmp2.txt");
  103. IplImage *image2 = cvLoadImage("object_test.pgm");
  104. FILE* fp1new=fopen("tmp2.txt","rb");
  105. float *arr1xnew; float *arr1ynew; int *arr2new;float scalenew,orinew;int temp1new, temp2new;
  106. //while(fp1new!=NULL)
  107. {
  108. fscanf(fp1new,"%d %d \n",&temp1new, &temp2new);
  109. //printf("%d %d \n",temp1new,temp2new);
  110. //getchar();
  111. arr1xnew=(float*)malloc(sizeof(float)*temp1new);
  112. arr1ynew=(float*)malloc(sizeof(float)*temp1new);
  113. arr2new=(int*)malloc(sizeof(int)*temp2new*temp1new);
  114. for(int i2new=1;i2new<=temp1new;i2new++)
  115. {
  116. fscanf(fp1new,"%f %f %f %f \n", &arr1xnew[i2new-1], &arr1ynew[i2new-1], &scalenew,&orinew);
  117. //printf("%f %f %f %f \n", arr1xnew[i2new-1], arr1ynew[i2new-1], scalenew,orinew);
  118. //getchar();
  119. for(int i3new=1;i3new<=temp2new;i3new++)
  120. {
  121. fscanf(fp1new,"%d ", &arr2new[(i2new-1)*temp2new+(i3new-1)]);
  122. //printf("%d ", arr2new[(i2new-1)*temp2new+(i3new-1)]);
  123. }fscanf(fp1new,"\n");printf("\n");
  124. cvCircle(image2,cvPoint((int)arr1xnew[i2new-1],(int)arr1ynew[i2new-1]),3,cvScalar(0,255,255,255),1);
  125. //getchar();
  126. }
  127. }
  128. cvNamedWindow("cNametest",CV_WINDOW_AUTOSIZE);
  129. cvShowImage("cNametest",image2);
  130. //matching
  131. //IplImage *imagematch = cvLoadImage("blue2.pgm");
  132. float *bestmatchx = (float*)malloc(sizeof(float)*temp1);
  133. float *bestmatchy = (float*)malloc(sizeof(float)*temp1);
  134. for (int i4=1;i4<=temp1;i4++)
  135. {
  136. float bestval = 1000000.0;
  137. for(int i5=1;i5<=temp1new;i5++)
  138. {
  139. double value=0.0;
  140. for(int i6=1;i6<=temp2new;i6++)
  141. {
  142. double temp = arr2[(i4-1)*temp2+(i6-1)]-arr2new[(i5-1)*temp2new+(i6-1)];
  143. value+= temp*temp;
  144. }
  145. value = sqrt(value)/temp2new;
  146. if (value < bestval)
  147. {
  148. bestval=value;
  149. bestmatchx[i4-1]=arr1xnew[i5-1];
  150. bestmatchy[i4-1]=arr1ynew[i5-1];
  151. }
  152. }
  153. cvCircle(frame1,cvPoint((int)bestmatchx[i4-1],(int)bestmatchy[i4-1]),3,cvScalar(0,255,255,255),1);
  154. }
  155. double xmean = 0.0; double ymean = 0.0;
  156. for(int i7=1;i7<=temp1;i7++)
  157. {
  158. xmean += bestmatchx[i7-1];
  159. ymean += bestmatchy[i7-1];
  160. }
  161. xmean = xmean/temp1;
  162. ymean = ymean/temp1;
  163. printf("%lf %lf",xmean,ymean);
  164. //getchar();
  165. //cvSaveImage("savematch.jpg",imagematch);
  166. //cvReleaseImage(&imagematch);
  167. cvCircle(frame1,cvPoint((int)xmean,(int)ymean),10,cvScalar(0,255,255,0));
  168. cvNamedWindow("cNamematch",CV_WINDOW_AUTOSIZE);
  169. cvShowImage("cNamematch",frame1);
  170. char c = cvWaitKey(33);
  171. //int xmean=0;int ymean=0;
  172. Vector& target = targetPort.prepare();
  173. target.resize(3);
  174. target[0] = xmean;
  175. target[1] = ymean;
  176. target[2] = 1;
  177. targetPort.write();
  178. cvReleaseImage(&image2);
  179. }
  180. }
  181. cvReleaseImage(&image1);
  182. return 0;
  183. }