PageRenderTime 130ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/johnflan/Maple
Java | 173 lines | 108 code | 54 blank | 11 comment | 5 complexity | dec1b02e678a8ec4e1597d7636cedc71 MD5 | raw file
  1. package com.johnflan.javacv.detectcontours;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import com.googlecode.javacpp.Loader;
  9. import com.googlecode.javacv.CanvasFrame;
  10. import com.googlecode.javacv.cpp.opencv_core.CvContour;
  11. import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
  12. import com.googlecode.javacv.cpp.opencv_core.CvPoint;
  13. import com.googlecode.javacv.cpp.opencv_core.CvScalar;
  14. import com.googlecode.javacv.cpp.opencv_core.CvSeq;
  15. import com.googlecode.javacv.cpp.opencv_core.CvSeqReader;
  16. import com.googlecode.javacv.cpp.opencv_core.CvSeqWriter;
  17. import com.googlecode.javacv.cpp.opencv_core.IplImage;
  18. import static com.googlecode.javacv.cpp.opencv_imgproc.*;
  19. import static com.googlecode.javacv.cpp.opencv_core.*;
  20. import static com.googlecode.javacv.cpp.opencv_video.*;
  21. import static com.googlecode.javacv.cpp.opencv_highgui.*;
  22. public class ContourProcessor {
  23. //CvSeq contour = new CvSeq(null);
  24. int numOfContours;
  25. public void detect(IplImage binaryImage, CvSeq contours, CvMemStorage storage){
  26. cvClearMemStorage(storage);
  27. //numOfContours = cvFindContours(binaryImage, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
  28. numOfContours = cvFindContours(binaryImage, storage, contours, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  29. System.out.println("Number of contours detected:" + numOfContours);
  30. }
  31. public void draw(IplImage rawImage, CvSeq contours){
  32. //first cvScalar value external colour of contour: Green ***
  33. //second cvScalar hole colour: Blue
  34. if (!contours.isNull()){
  35. cvDrawContours(rawImage, contours, cvScalar(0, 255, 47, 255), cvScalar(255, 0, 0, 255), 100, 1, 8);
  36. }
  37. }
  38. public CvSeq loadContours(CvMemStorage storage){
  39. CvPoint p1 = new CvPoint();
  40. CvPoint p2 = new CvPoint();
  41. CvPoint p3 = new CvPoint();
  42. CvPoint p4 = new CvPoint();
  43. CvSeqWriter writer = new CvSeqWriter();
  44. cvStartWriteSeq(0, Loader.sizeof(CvSeq.class), Loader.sizeof(CvPoint.class), storage, writer);
  45. //Load points from file
  46. {
  47. p1.x(1); p1.y(1);
  48. p2.x(300); p2.y(300);
  49. p3.x(320); p3.y(260);
  50. p4.x(1); p4.y(1);
  51. CV_WRITE_SEQ_ELEM(p1, writer);
  52. CV_WRITE_SEQ_ELEM(p2, writer);
  53. CV_WRITE_SEQ_ELEM(p3, writer);
  54. CV_WRITE_SEQ_ELEM(p4, writer);
  55. }
  56. return cvEndWriteSeq( writer);
  57. }
  58. public CvSeq loadTestLeafContour(){
  59. IplImage frameRaw = cvLoadImage(
  60. "/home/johnflan/workspace/Maple/docs/misc_images/testLeaf.png",
  61. CV_LOAD_IMAGE_UNCHANGED);
  62. IplImage frameProcessed = cvCreateImage( cvGetSize(frameRaw), IPL_DEPTH_8U, 1 );
  63. CvMemStorage storage = CvMemStorage.create();
  64. CvSeq contour = new CvSeq();
  65. FramePreProcessor framePreProcessor = new FramePreProcessor();
  66. framePreProcessor.prepareImage(frameRaw, frameProcessed);
  67. framePreProcessor.dilate(frameProcessed);
  68. framePreProcessor.applyThreshold(frameProcessed, frameProcessed);
  69. cvNot(frameProcessed, frameProcessed); //invert the image
  70. cvFindContours(cvCloneImage(frameProcessed), storage, contour, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  71. //cvApproxPoly(contourPtr, Loader.sizeof(CvContour.class), storage,CV_POLY_APPROX_DP,1,1);
  72. return contour;
  73. }
  74. public void printContourPoints(CvSeq contour){
  75. //write out the each contour - so we can save it
  76. CvPoint point = new CvPoint();
  77. CvSeq extractSeq = contour;
  78. CvSeqReader cvSeqReader = new CvSeqReader();
  79. cvStartReadSeq(extractSeq, cvSeqReader, 0);
  80. for(int i = 0; i< extractSeq.total(); i++){
  81. CV_READ_SEQ_ELEM(point, cvSeqReader);
  82. System.out.println(point);
  83. }
  84. }
  85. public Map<String, CvSeq> loadTestLeaves() {
  86. Map<String, CvSeq> leafSet = new HashMap<String, CvSeq>();
  87. Map<String, String> fileNames = new HashMap<String, String>();
  88. fileNames.put("Maple", "/home/johnflan/workspace/Maple/docs/misc_images/testimg/leaf.png");
  89. //fileNames.put("Green Ash", "/home/johnflan/workspace/Maple/docs/misc_images/testimg/green_ash.png");
  90. //fileNames.put("Horse Chestnut Buckeye", "/home/johnflan/workspace/Maple/docs/misc_images/testimg/horse_chestnut_buckeye.png");
  91. //fileNames.put("White Birch", "/home/johnflan/workspace/Maple/docs/misc_images/testimg/white_birch.png");
  92. //fileNames.put("Twig", "/home/johnflan/workspace/Maple/docs/misc_images/testimg/twig1.png");
  93. Iterator it = fileNames.entrySet().iterator();
  94. while (it.hasNext()){
  95. Map.Entry<String, String> file = (Map.Entry) it.next();
  96. IplImage frameRaw = cvLoadImage(file.getValue(), CV_LOAD_IMAGE_UNCHANGED);
  97. if (frameRaw.isNull())
  98. continue;
  99. IplImage frameProcessed = cvCreateImage( cvGetSize(frameRaw), IPL_DEPTH_8U, 1 );
  100. CvMemStorage storage = CvMemStorage.create();
  101. CvSeq contour = new CvSeq();
  102. FramePreProcessor framePreProcessor = new FramePreProcessor();
  103. framePreProcessor.prepareImage(frameRaw, frameProcessed);
  104. framePreProcessor.dilate(frameProcessed);
  105. framePreProcessor.applyThreshold(frameProcessed, frameProcessed);
  106. cvNot(frameProcessed, frameProcessed); //invert the image
  107. cvFindContours(cvCloneImage(frameProcessed), storage, contour, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
  108. CvScalar colour = CV_RGB( 0, 255, 0 );
  109. CanvasFrame canvasFramePre = new CanvasFrame("Loading");
  110. canvasFramePre.setCanvasSize(frameRaw.width(), frameRaw.height());
  111. cvDrawContours(frameRaw, contour, colour, colour, -1, 0,8, cvPoint(0,0));
  112. canvasFramePre.showImage(frameRaw);
  113. canvasFramePre.dispose();
  114. if (!contour.isNull())
  115. leafSet.put(file.getKey(), contour);
  116. }
  117. return leafSet;
  118. }
  119. }