privatevoidcutImagesToRows(){ ArrayList<Double> lineYs = new ArrayList<>(); ArrayList<Double> uniqueLineYs = new ArrayList<>();
//lines:a special mat for find lines Mat lines = new Mat(); //find lines and store in lines Imgproc.HoughLinesP(dilateMuchPic, lines, 1, Math.PI / 180, Y_THRESHOLD, Y_MINLINELENGTH, Y_MAXLINEGAP);
//get the lines information from lines and store in lineYs for (int i = 0; i < lines.rows(); i++) { double[] points = lines.get(i, 0); double y1, y2;
//just need the horizontal lines y1 = points[1]; y2 = points[3];
// if it slopes, get the average of them, store the y-coordinate if (Math.abs(y1 - y2) < 30) { lineYs.add((y1 + y2) / 2); } } getUniqueLines(lineYs, uniqueLineYs, 10);
/** * filter the source coordinates, if some values are too close ,get the average of them * * @param src source coordinates list * @param dst destination coordinate list * @param minGap the minimum gap between coordinates */ privatevoidgetUniqueLines(ArrayList<Double> src, ArrayList<Double> dst, int minGap){ Collections.sort(src); //sort the source coordinates list for (int i = 0; i < src.size(); i++) { double sum = src.get(i); double num = 1; //when the distance between lines less than minGap, get the average of thema while (i != src.size() - 1 && src.get(i + 1) - src.get(i) < minGap) { num++; sum = sum + src.get(i + 1); i++; } if (num == 1) { dst.add(src.get(i)); } else { dst.add(((sum / num))); } } }
privatevoidshowMarkedLines(Mat src, ArrayList<Double> lines){ Mat showLines = new Mat(); Imgproc.cvtColor(src, showLines, COLOR_GRAY2BGR); for (double y : lines) {
Point pt1 = new Point(0, y); Point pt2 = new Point(src.width(), y); Imgproc.line(showLines, pt1, pt2, new Scalar(0, 0, 255), 3); } Imgcodecs.imwrite("C:/Users/visea/Desktop/test/java/cut/" + String.valueOf(colNum) + ".jpg", showLines); }