/** * Finds the center point of a given contour. * * @param contour The contour who's center is to be calculated * @return The coordinates of the center as a Point */ private Point findCenter(MatOfPoint contour) { // get moments Moments m = new Moments(); m = Imgproc.moments(contour, true); // get mass center Point mc; mc = new Point(m.get_m10() / m.get_m00(), m.get_m01() / m.get_m00()); return mc; }
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 ); // System.out.println( "mat = " + mat.dump() ); Sample n = new Sample(); // n.templateMatching(); // put text in image // Mat data= Highgui.imread("images/erosion.jpg"); // Core.putText(data, "Sample", new Point(50,80), Core.FONT_HERSHEY_SIMPLEX, 1, new // Scalar(0,0,0),2); // // Highgui.imwrite("images/erosion2.jpg", data); // getting dct of an image String path = "images/croppedfeature/go (20).jpg"; path = "images/wordseg/img1.png"; Mat image = Highgui.imread(path, Highgui.IMREAD_GRAYSCALE); ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.threshold(image, image, 0, 255, Imgproc.THRESH_OTSU); Imgproc.threshold(image, image, 220, 128, Imgproc.THRESH_BINARY_INV); Mat newImg = new Mat(45, 100, image.type()); newImg.setTo(new Scalar(0)); n.copyMat(image, newImg); int vgap = 25; int hgap = 45 / 3; Moments m = Imgproc.moments(image, false); Mat hu = new Mat(); Imgproc.HuMoments(m, hu); System.out.println(hu.dump()); // //divide the mat into 12 parts then get the features of each part // int count=1; // for(int j=0; j<45; j+=hgap){ // for(int i=0;i<100;i+=vgap){ // Mat result = newImg.submat(j, j+hgap, i, i+vgap); // // // Moments m= Imgproc.moments(result, false); // double m01= m.get_m01(); // double m00= m.get_m00(); // double m10 = m.get_m10(); // int x= m00!=0? (int)(m10/m00):0; // int y= m00!=0? (int)(m01/m00):0; // Mat hu= new Mat(); // Imgproc.HuMoments(m, hu); // System.out.println(hu.dump()); // System.out.println(count+" :"+x+" and "+y); // Imgproc.threshold(result, result, 0,254, Imgproc.THRESH_BINARY_INV); // Highgui.imwrite("images/submat/"+count+".jpg", result); // count++; // // } // } // // for(int i=vgap;i<100;i+=vgap){ // Point pt1= new Point(i, 0); // Point pt2= new Point(i, 99); // Core.line(newImg, pt1, pt2, new Scalar(0,0,0)); // } // for(int i=hgap;i<45;i+=hgap){ // Point pt1= new Point(0, i); // Point pt2= new Point(99, i); // Core.line(newImg, pt1, pt2, new Scalar(0,0,0)); // } // Highgui.imwrite("images/submat/copyto.jpg", newImg); }
public Point findLaser(Mat inputFrame) { Mat mHsv = new Mat(); Imgproc.cvtColor(inputFrame, mHsv, Imgproc.COLOR_RGB2HSV); // Find laser center Mat center = new Mat(); Core.inRange(mHsv, new Scalar(0, 0, 250), new Scalar(180, 16, 255), center); Mat h = new Mat(); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(center, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); center.release(); Mat center_mask = Mat.zeros(inputFrame.rows(), inputFrame.cols(), CvType.CV_8U); if (contours.size() > 0) { for (int i = 0; i < contours.size(); i++) { int radius = 10; // Point[] cont_pos = contours.get(i).toArray(); Moments m = Imgproc.moments(contours.get(i)); Point p = new Point(); p.x = m.get_m10() / m.get_m00(); p.y = m.get_m01() / m.get_m00(); Core.circle(center_mask, p, radius * 2, new Scalar(255), -1); } } // Find halo Mat ranged = new Mat(); Core.inRange(mHsv, new Scalar(100, 32, 225), new Scalar(150, 255, 255), ranged); mHsv.release(); // Mat f_frame =ranged.clone(); // Find halo around bright dot Core.bitwise_and(ranged, center_mask, ranged); center_mask.release(); // Find biggest resulting contour for (int i = 1; i < contours.size(); i++) { contours.get(i).release(); } contours.clear(); Imgproc.findContours(ranged, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); h.release(); ranged.release(); if (contours.size() > 0) { MatOfPoint biggest_cont = contours.get(0); double cont_size = Imgproc.contourArea(biggest_cont); for (int i = 1; i < contours.size(); i++) { MatOfPoint cur = contours.get(i); if (Imgproc.contourArea(cur) > cont_size) { biggest_cont = cur; cont_size = Imgproc.contourArea(cur); } } Moments m = Imgproc.moments(biggest_cont); Point p = new Point(); p.x = m.get_m10() / m.get_m00(); p.y = m.get_m01() / m.get_m00(); for (int i = 1; i < contours.size(); i++) { contours.get(i).release(); } biggest_cont.release(); return p; } else { return null; } }