Advertisement
Guest User

blubb

a guest
Dec 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. package imageProcessing;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.opencv.core.*;
  6.  
  7. import org.opencv.imgproc.Imgproc;
  8.  
  9. import main.RunProgramm;
  10.  
  11. public class ProcessInputImage {
  12.  
  13.     public boolean checkForCircle(Mat srcImg, Mat grayImg){
  14.         Mat circles = new Mat();
  15.         Imgproc.HoughCircles(grayImg, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 400);
  16.         drawCircles(srcImg, circles);
  17.        
  18.         if (circles.cols() > 0){
  19.             return true;
  20.         }
  21.         return false;
  22.     }
  23.    
  24.     public boolean checkForTriangle(Mat srcImg, Mat invImg){
  25.         Mat hierarchy = new Mat();
  26.         ArrayList<MatOfPoint> contourList = new ArrayList<MatOfPoint>();
  27.         Imgproc.findContours(invImg, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  28.        
  29.         System.out.println("# of contours in contourList: "+contourList.size());
  30.        
  31.         MatOfPoint2f contourSimple = new MatOfPoint2f();
  32.         MatOfPoint2f srcContour = new MatOfPoint2f();
  33.         contourList.get(0).convertTo(srcContour, CvType.CV_32FC2);
  34.         double contourPerimeter = Imgproc.arcLength(srcContour, true);
  35.         Imgproc.approxPolyDP(srcContour, contourSimple, 0.05 * contourPerimeter, true);
  36.        
  37.         //Convert simplified contour back to MatOfPoint and put it back into the list
  38.         contourSimple.convertTo(contourList.get(0), CvType.CV_32S);
  39.        
  40.         System.out.println("Corners: " + contourSimple.rows());
  41.        
  42.         if (contourSimple.rows() == 3){
  43.             return true;
  44.         }else{
  45.  
  46.             //debug: Draw simplified contour onto srcImg
  47.             Imgproc.drawContours(srcImg, contourList, 0, new Scalar(0,0,255),1);
  48.            
  49.             ArrayList<Point>    pointList = new ArrayList<Point>(),
  50.                                 pointListFinal = new ArrayList<Point>();
  51.            
  52.             //Iterate over each vertex in simplified contour
  53.             for(int i = 0; i < contourSimple.rows(); i++) {
  54.                 double[] coords = contourSimple.get(i, 0);
  55.                 Point p =  new Point(coords[0],coords[1]);
  56.                
  57.                 //And add them to a list of vertices
  58.                 pointList.add(p);
  59.                
  60.                 //debug: Draw vertices onto srcImg
  61.                 Imgproc.circle(srcImg,p, 3, new Scalar(i*40,0,255-i*40),2);
  62.             }
  63.            
  64.             boolean[] blacklist = new boolean[pointList.size()];
  65.            
  66.             //Compare all points with each other
  67.             for (int i = 0; i < pointList.size(); i++) {   
  68.                 for (int k = i + 1; k < pointList.size(); k++) {
  69.                     if (i != k) {      
  70.                         Point   p1 = pointList.get(i),
  71.                                 p2 = pointList.get(k);
  72.                        
  73.                         //Calculate distance between point i <-> point k
  74.                         double dist = Math.sqrt(Math.pow((p2.x - p1.x), 2) + Math.pow((p2.y - p1.y), 2));
  75.                        
  76.                         //If distance is smaller than a given threshold value
  77.                         if(dist < 20) {
  78.                             //then blacklist point i. Every blacklisted point won't be copied in pointListFinal afterwards.
  79.                             blacklist[i] = true;
  80.                         }
  81.                        
  82.                     }
  83.                 }
  84.             }
  85.             //Construct final point list. These points don't have "duplicates" and might form a triangle.
  86.             for(int i = 0; i < pointList.size(); i++) {
  87.                 boolean isBlacklisted = blacklist[i];
  88.                
  89.                 //debug: print blacklist items in console
  90.                 System.out.println(isBlacklisted);
  91.                
  92.                 if(!isBlacklisted)
  93.                     pointListFinal.add(pointList.get(i));
  94.             }
  95.            
  96.             //debug: print final point list size
  97.             System.out.println(pointListFinal.size());
  98.            
  99.            
  100.            
  101.            
  102.            
  103.  
  104.         }
  105.        
  106.         return false;
  107.     }
  108.    
  109.     public boolean checkForLine(Mat srcImg, Mat invImg){
  110.         Mat a = new Mat();
  111.         ArrayList<MatOfPoint> contourList = new ArrayList<MatOfPoint>();
  112.         Imgproc.findContours(invImg, contourList, a, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
  113.         if (contourList.size() == 1 ){
  114.             Rect r = Imgproc.boundingRect(contourList.get(0));
  115.             if (r.width >= 30 && r.height <= 60){
  116.             return true;
  117.             }
  118.         }
  119.         return false;  
  120.     }
  121.    
  122.     public void drawCircles (Mat img, Mat circles){
  123.         for (int i = 0; i < circles.cols(); i++){
  124.             double[] circleVector = circles.get(0, i);
  125.             System.out.println(circleVector.length);
  126.             Point center = new Point (circleVector[0], circleVector[1]);
  127.             int radius = (int) Math.round(circleVector[2]);
  128.             Imgproc.circle(img, center, radius, new Scalar(255,0,0), 10 );
  129.         }
  130.        
  131.     }
  132.    
  133.     public void drawLines (Mat img, Mat lines){
  134.        
  135.     }
  136.    
  137.    
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement