Advertisement
vergepuppeter

Square Detection Algorithm

Oct 16th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. Mat clonedMat = imgSource.clone();
  2.  
  3. Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);
  4. //convert the image to black and white does (8 bit)
  5. Imgproc.Canny(imgSource, imgSource, 50, 50);
  6. //apply gaussian blur to smoothen lines of dots
  7. Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 5);
  8. //find the contours
  9.  
  10. List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
  11. Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
  12.  
  13. double maxArea = -1;
  14. int maxAreaIdx = -1;
  15. MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
  16. MatOfPoint2f approxCurve = new MatOfPoint2f();
  17. List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
  18. MatOfPoint2f maxCurve = new MatOfPoint2f();
  19.  
  20. for (int idx = 0; idx < contours.size(); idx++) {
  21. temp_contour = contours.get(idx);
  22. double contourarea = Imgproc.contourArea(temp_contour);
  23. //compare this contour to the previous largest contour found
  24. if (contourarea > maxArea) {
  25. //check if this contour is a square
  26. MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
  27. int contourSize = (int)temp_contour.total();
  28. Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
  29. if (approxCurve.total() == 4) {
  30.  
  31. maxCurve = approxCurve;
  32. maxArea = contourarea;
  33. maxAreaIdx = idx;
  34. largest_contours.add(temp_contour);
  35. MatOfPoint points = new MatOfPoint( approxCurve.toArray() );
  36. Rect rect = Imgproc.boundingRect(points);
  37.  
  38. if(isDebug)
  39. {
  40. Imgproc.drawContours(clonedMat, contours, idx, new Scalar(0,0,255), 5);
  41. }
  42.  
  43. }
  44. }
  45. }
  46.  
  47. String temp_string = null;
  48.  
  49. try
  50. {
  51. //BGR
  52. point1 = maxCurve.get(1, 0);
  53. p1 = new org.opencv.core.Point(point1[0]+30, point1[1]+30);
  54. //Imgproc.circle(clonedMat, p1, 20, new Scalar(255, 0, 0), 5); //blue
  55. temp_string = "Point 1: (" + p1.x + ", " + p1.y + ")";
  56.  
  57. point2 = maxCurve.get(0, 0);
  58. p2 = new org.opencv.core.Point(point2[0]-30, point2[1]+30);
  59. //Imgproc.circle(clonedMat, p2, 20, new Scalar(0, 255, 0), 5);//green
  60. temp_string += "\nPoint 2: (" + p2.x + ", " + p2.y + ")";
  61.  
  62. point3 = maxCurve.get(3, 0);
  63. p3 = new org.opencv.core.Point(point3[0]-50, point3[1]-50);
  64. //Imgproc.circle(clonedMat, p3, 20, new Scalar(0, 0, 255), 5);//red
  65. temp_string += "\nPoint 3: (" + p3.x + ", " + p3.y + ")";
  66.  
  67. point4 = maxCurve.get(2, 0);
  68. p4 = new org.opencv.core.Point(point4[0]+50, point4[1]-50);
  69. //Imgproc.circle(clonedMat, p4, 20, new Scalar(0, 255, 255), 5); // yellow
  70. temp_string += "\nPoint 4: (" + p4.x + ", " + p4.y + ")";
  71. }catch (NullPointerException e)
  72. {
  73. this.cancel(true);
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement