PageRenderTime 75ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/JavaCV/src/com/johnflan/javacv/detectcontours/DetectContours.java

https://github.com/johnflan/Maple
Java | 205 lines | 106 code | 72 blank | 27 comment | 11 complexity | b488821a8b3d28494774231018039734 MD5 | raw file
  1. package com.johnflan.javacv.detectcontours;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import com.googlecode.javacpp.Loader;
  5. import com.googlecode.javacv.CanvasFrame;
  6. import com.googlecode.javacv.OpenCVFrameGrabber;
  7. import com.googlecode.javacv.cpp.opencv_core.CvBox2D;
  8. import com.googlecode.javacv.cpp.opencv_core.CvContour;
  9. import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
  10. import com.googlecode.javacv.cpp.opencv_core.CvSeq;
  11. import com.googlecode.javacv.cpp.opencv_core.IplImage;
  12. import static com.googlecode.javacv.cpp.opencv_imgproc.*;
  13. import static com.googlecode.javacv.cpp.opencv_core.*;
  14. import static com.googlecode.javacv.cpp.opencv_video.*;
  15. import static com.googlecode.javacv.cpp.opencv_highgui.*;
  16. public class DetectContours {
  17. public static void main(String[] args) throws Exception {
  18. OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
  19. grabber.start();
  20. IplImage frameRaw = grabber.grab();
  21. //!!IplImage frameRaw = cvLoadImage(
  22. //!! "/home/johnflan/workspace/Maple/docs/misc_images/testimg/leaf.png",
  23. //!! CV_LOAD_IMAGE_UNCHANGED);
  24. IplImage frameProcessed = cvCreateImage( cvGetSize(frameRaw), IPL_DEPTH_8U, 1 );
  25. CanvasFrame canvasFramePre = new CanvasFrame("Preprocessing");
  26. canvasFramePre.setCanvasSize(frameRaw.width(), frameRaw.height());
  27. CanvasFrame canvasFramePost = new CanvasFrame("Postprocessing");
  28. canvasFramePost.setCanvasSize(frameRaw.width(), frameRaw.height());
  29. CanvasFrame canvasFramePre1 = new CanvasFrame("Preprocessing");
  30. canvasFramePre1.setCanvasSize(frameRaw.width(), frameRaw.height());
  31. CanvasFrame canvasFramePost1 = new CanvasFrame("Postprocessing");
  32. canvasFramePost1.setCanvasSize(frameRaw.width(), frameRaw.height());
  33. FramePreProcessor framePreProcessor = new FramePreProcessor();
  34. ContourProcessor contourProcessor = new ContourProcessor();
  35. CvMemStorage storage = CvMemStorage.create();
  36. CvSeq contourPtr = new CvSeq(null);
  37. CvSeq contourLow;
  38. //Load Sample contours
  39. //CvSeq sampleLeaf = contourProcessor.loadTestLeafContour();
  40. Map<String, CvSeq> leafSet = contourProcessor.loadTestLeaves();
  41. CvSeq tempContour = leafSet.get("Maple");
  42. contourProcessor.printContourPoints(tempContour);
  43. IplImage mapleContour = cvLoadImage(
  44. "/home/johnflan/workspace/Maple/docs/misc_images/testimg/leaf.png",
  45. CV_LOAD_IMAGE_UNCHANGED);
  46. cvDrawContours(mapleContour, tempContour, CV_RGB( 0, 255, 0 ), CV_RGB( 0, 255, 0 ), -1, 2,8, cvPoint(0,0));
  47. canvasFramePre1.setTitle("Maple contour raw");
  48. canvasFramePre1.showImage(mapleContour);
  49. System.out.println("Low");
  50. CvSeq tempContourLow = cvApproxPoly(tempContour, Loader.sizeof(CvContour.class), storage,CV_POLY_APPROX_DP,1,1);
  51. contourProcessor.printContourPoints(tempContourLow);
  52. IplImage mapleContourPolyApprox = cvLoadImage(
  53. "/home/johnflan/workspace/Maple/docs/misc_images/testimg/leaf.png",
  54. CV_LOAD_IMAGE_UNCHANGED);
  55. cvDrawContours(mapleContourPolyApprox, tempContourLow, CV_RGB( 0, 255, 0 ), CV_RGB( 0, 255, 0 ), -1, 2,8, cvPoint(0,0));
  56. canvasFramePost1.setTitle("Maple contour poly approx");
  57. canvasFramePost1.showImage(mapleContourPolyApprox);
  58. Iterator leafItr = leafSet.entrySet().iterator();
  59. while (leafItr.hasNext()){
  60. Map.Entry<String, CvSeq> leaf = (Map.Entry)leafItr.next();
  61. leaf.setValue( cvApproxPoly( (CvSeq) leaf.getValue(), Loader.sizeof(CvContour.class), storage,CV_POLY_APPROX_DP,1,1) );
  62. //contourProcessor.printContourPoints(sampleLeafLow);
  63. }
  64. while (canvasFramePre.isVisible() && canvasFramePost.isVisible() && (frameRaw = grabber.grab()) != null) {
  65. //convert to grayscale and equalise levels
  66. framePreProcessor.prepareImage(frameRaw, frameProcessed);
  67. canvasFramePre1.setTitle("After Equalisation");
  68. canvasFramePre1.showImage(frameProcessed);
  69. framePreProcessor.applyThreshold(frameProcessed, frameProcessed);
  70. cvNot(frameProcessed, frameProcessed); //invert the image
  71. canvasFramePost1.setTitle("After threshold");
  72. canvasFramePost1.showImage(frameProcessed);
  73. framePreProcessor.dilate(frameProcessed);
  74. canvasFramePost.setTitle("Final image");
  75. canvasFramePost.showImage(frameProcessed);
  76. //Detect and Draw contours
  77. cvFindContours(cvCloneImage(frameProcessed), storage, contourPtr, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  78. //Approx contours
  79. contourLow = cvApproxPoly(contourPtr, Loader.sizeof(CvContour.class), storage,CV_POLY_APPROX_DP,1,1);
  80. for (; contourLow != null; contourLow = contourLow.h_next()){
  81. //CvScalar colour = CV_RGB( Math.random() * 255, Math.random() * 255, Math.random() * 255 );
  82. CvScalar colour = CV_RGB( 0, 255, 0 );
  83. //Find bounding box
  84. CvRect rect;
  85. CvPoint p1 = new CvPoint();
  86. CvPoint p2 = new CvPoint();
  87. rect = cvBoundingRect(contourLow, 1);
  88. //If the width is less than 1/10 of the image width skip it
  89. if ((frameRaw.width() * .2) > rect.width() )
  90. continue;
  91. //if (contourLow == null || sampleLeaf == null){
  92. // System.out.println("Error contourLow or sampleLeaf contain null value");
  93. // continue;
  94. //}
  95. leafItr = leafSet.entrySet().iterator();
  96. double matchValue = 100;
  97. String matchName = "";
  98. while (leafItr.hasNext()){
  99. Map.Entry<String, CvSeq> leaf = (Map.Entry)leafItr.next();
  100. double tmpMatchValue = cvMatchShapes(contourLow, leaf.getValue(), CV_CONTOURS_MATCH_I1, 0);
  101. if (tmpMatchValue <= matchValue){
  102. matchValue = tmpMatchValue;
  103. matchName = leaf.getKey();
  104. }
  105. //contourProcessor.printContourPoints(sampleLeafLow);
  106. }
  107. //double matchValue = cvMatchShapes(contourLow, sampleLeafLow, CV_CONTOURS_MATCH_I1, 0);
  108. //double matchValue = .1;
  109. if (matchValue > .04)
  110. continue;
  111. //Draw bounding box
  112. p1.x(rect.x());
  113. p2.x(rect.x() + rect.width());
  114. p1.y(rect.y());
  115. p2.y(rect.y() + rect.height());
  116. cvRectangle(frameRaw, p1, p2, colour, 2, 8, 0);
  117. //Draw contour
  118. //cvDrawContours(frameRaw, contourLow, colour, colour, -1, 0,8, cvPoint(0,0));
  119. //----Calculate the objects features using hu moments
  120. //cvMoments(contourLow, moments, 0);
  121. //cvGetHuMoments(moments, humoments);
  122. System.out.println("Found a " + matchName + " leaf, with a value of: " + matchValue);
  123. }
  124. canvasFramePre.showImage(frameRaw);
  125. canvasFramePost.showImage(frameProcessed);
  126. // cvReleaseMemStorage(storage);
  127. // storage = new CvMemStorage();
  128. }
  129. grabber.stop();
  130. canvasFramePre.dispose();
  131. canvasFramePost.dispose();
  132. }
  133. }