PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/templatematching.java

https://bitbucket.org/yan_chen/pc-image-processing
Java | 253 lines | 148 code | 39 blank | 66 comment | 27 complexity | eb6af9564fc0fe601c6232b18c1f9ed9 MD5 | raw file
  1. import static com.googlecode.javacv.cpp.opencv_core.*;
  2. import static com.googlecode.javacv.cpp.opencv_imgproc.*;
  3. import static com.googlecode.javacv.cpp.opencv_highgui.*;
  4. import java.io.File;
  5. import java.util.Scanner;
  6. public class templatematching{
  7. public static void main (String[] args)throws Exception,NullPointerException
  8. {
  9. int location[][] = new int[16][4];
  10. File[] g = new File(("C:/Users/chens_000/Desktop/new")).listFiles();
  11. for (int p=0;p <g.length;p++)
  12. {
  13. // System.out.println(g[p].getName());
  14. int task = taskorder(g[p].getName());
  15. IplImage src = cvLoadImage(g[p].getPath(),0);
  16. IplImage tmp = cvLoadImage("d.jpg",0);
  17. IplImage result = cvCreateImage(cvSize(src.width()-tmp.width()+1, src.height()-tmp.height()+1), IPL_DEPTH_32F, 1);
  18. cvZero(result);
  19. cvMatchTemplate(src, tmp, result, CV_TM_CCORR_NORMED);
  20. double[] min_val = new double[2];
  21. double[] max_val = new double[2];
  22. CvPoint minLoc = new CvPoint();
  23. CvPoint maxLoc = new CvPoint();
  24. cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc, null);
  25. CvPoint point = new CvPoint();
  26. point.x(maxLoc.x()+tmp.width());
  27. point.y(maxLoc.y()+tmp.height());
  28. location[task][0] = maxLoc.x();
  29. location[task][1] = maxLoc.y();
  30. location[task][2] = point.x();
  31. location[task][3] = point.y();
  32. // System.out.println("("+maxLoc.x()+", "+maxLoc.y()+") ("+point.x()+", "+point.y()+")");
  33. cvRectangle(src, maxLoc, point, CvScalar.RED, 2, 8, 0);
  34. // cvSaveImage("abc.jpg", src);
  35. // cvShowImage("abc.jpg", src);
  36. // cvWaitKey(0);
  37. //Release
  38. cvReleaseImage(src);
  39. cvReleaseImage(tmp);
  40. cvReleaseImage(result);
  41. }
  42. File f = new File(("C:/Users/chens_000/Desktop/1.txt"));
  43. File h = new File(("C:/Users/chens_000/Desktop/2.txt"));
  44. // File g = new File("C:/Users/chens_000/Desktop/New1/Mrigendra_Singh/Frame_and_mouselocation_and_keystroke_and_time.txt");
  45. // File f = new File("C:/Users/chens_000/Desktop/New1/Mrigendra_Singh/Mouse_location_with_time.txt");
  46. // System.out.println(g[0].getName());
  47. long timestample[] = new long[16];
  48. Scanner s = new Scanner(h);
  49. Scanner r = new Scanner(f);
  50. s.useDelimiter("\\s+|,|\\n|:|;|\\(|\\)| ");
  51. r.useDelimiter("\\s+|,|\\n|:|;|\\(|\\)| ");
  52. long a[] = new long[200000];
  53. long b[] = new long[200000];
  54. int ii = 0;
  55. a[0] = 0;
  56. String c = null;
  57. int jj = 0;
  58. b[0] = 0;
  59. String e = null;
  60. while (s.hasNext())
  61. {
  62. c = s.next();
  63. if (isParsableToInt(c))
  64. {
  65. a[ii] = Long.parseLong(c);
  66. // System.out.println(a[ii]+" ");
  67. ii++;
  68. }
  69. }
  70. while (r.hasNext())
  71. {
  72. e = r.next();
  73. if (isParsableToInt(e))
  74. {
  75. b[jj] = Long.parseLong(e);
  76. // System.out.println(b[jj]+" ");
  77. jj++;
  78. }
  79. }
  80. int k = 0;
  81. int l=0;
  82. int m;
  83. for(int n=0;n<16;n++)
  84. {
  85. for(m=l;m<jj;m+=4)
  86. {
  87. if((location[n][0]<=b[m]) & (b[m]<=location[n][2]) & (location[n][1]<=b[m+1]) & (b[m+1]<=location[n][3]) & (b[m+2]==1) & (b[m+6]==0))
  88. {
  89. timestample[n]=b[m+3];
  90. // System.out.println(n+" "+timestample[n]);
  91. l=m+4;
  92. break;
  93. }
  94. }
  95. }
  96. int u=4;
  97. int w;
  98. long frame[] = new long[16];
  99. for(int n=0;n<16;n++)
  100. {
  101. for( w=u;w<ii;w++)
  102. {
  103. if( (100000<a[w-3]) & (100000<a[w+1]) & (a[w-3]<=timestample[n]) & (timestample[n]<=a[w+1]))
  104. {
  105. // System.out.println(n+" "+w+" "+a[w-3]+" "+timestample[n]+" "+a[w+1]);
  106. frame[n] = a[w];
  107. System.out.println(frame[n]);
  108. u=w;
  109. break;
  110. }
  111. }
  112. }
  113. /*
  114. long d[] = new long[ii];
  115. for (int j = 0; j < ii; j++)
  116. {
  117. d[j] = Long.parseLong(a[j]);
  118. }
  119. k=0;
  120. for (int j = 1; j <ii; j++)
  121. {
  122. if(d[j]>90000)
  123. k++;
  124. }
  125. long m[]=new long[k];
  126. k=0;
  127. for( int j=1;j<ii;j++)
  128. {
  129. if(d[j]>90000)
  130. {
  131. m[k]=d[j];
  132. k++;
  133. }
  134. }
  135. int count1=0;
  136. int temp=0;
  137. for(int j=1;j<k;j++)
  138. {
  139. for(int ij=2;ij<=jj/4;ij++)
  140. {
  141. if((m[j-1]<=b[ij*4-1])&(b[ij*4-1]<m[j]))
  142. {
  143. if(((b[ij*4-2]-b[ij*4-6])!=0) |((b[ij*4-3]-b[ij*4-7])!=0)|((b[ij*4-4])!=0)|((b[ij*4-8])!=0))
  144. count1++;
  145. }
  146. }
  147. System.out.println(count1);
  148. count1=0;
  149. }
  150. */
  151. }
  152. public static int taskorder(String i){
  153. int j = 0;
  154. if(i.equals("task1.jpg"))
  155. j=0;
  156. if(i.equals("task2.jpg"))
  157. j=1;
  158. if(i.equals("task3.jpg"))
  159. j=2;
  160. if(i.equals("task3a.jpg"))
  161. j=3;
  162. if(i.equals("task3b.jpg"))
  163. j=4;
  164. if(i.equals("task3c.jpg"))
  165. j=5;
  166. if(i.equals("task3d.jpg"))
  167. j=6;
  168. if(i.equals("task4.jpg"))
  169. j=7;
  170. if(i.equals("task5.jpg"))
  171. j=8;
  172. if(i.equals("task6.jpg"))
  173. j=9;
  174. if(i.equals("task7.jpg"))
  175. j=10;
  176. if(i.equals("task8.jpg"))
  177. j=11;
  178. if(i.equals("task9.jpg"))
  179. j=12;
  180. if(i.equals("task10.jpg"))
  181. j=13;
  182. if(i.equals("task11.jpg"))
  183. j=14;
  184. if(i.equals("task12.jpg"))
  185. j=15;
  186. return j;
  187. }
  188. public static boolean isParsableToInt(String i) {
  189. try
  190. {
  191. Long.parseLong(i);
  192. return true;
  193. }
  194. catch (NumberFormatException nfe)
  195. {
  196. return false;
  197. }
  198. }
  199. }