List of usage examples for org.opencv.core MatOfInt4 MatOfInt4
public MatOfInt4()
From source file:cpsd.ImageGUI.java
private void AnalyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnalyzeButtonActionPerformed try {//from w w w . j a v a2 s . c o m double pixelArea = 1; double imageSize = 1; if (magnification == 50) pixelArea = 1.2996; else if (magnification == 100) pixelArea = 0.329476; else if (magnification == 200) pixelArea = 0.08162; else { imageSize = pow(10, 10) * pow(magnification, -2); pixelArea = (imageSize) / (resolution1 * resolution2); } Mat source = ImageClass.getInstance().getImage(); Mat destination = new Mat(source.rows(), source.cols(), source.type()); //Imgproc.adaptiveThreshold(source,destination,255,ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY_INV,13,2); //Imgproc.GaussianBlur(destination,destination,new org.opencv.core.Size(0,0),5); threshold(source, destination, 30, 255, CV_THRESH_BINARY); distanceTransform(destination, destination, CV_DIST_L2, 3); normalize(destination, destination, 0, 1, NORM_MINMAX); threshold(destination, destination, 0.5, 1, CV_THRESH_BINARY); destination.convertTo(destination, CV_8U); /*ImageClass.getInstance().setImage(destination); displayImage();*/ ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); MatOfInt4 hierarchy = new MatOfInt4(); // Rect roi = new Rect(100,100,destination.cols()-100,destination.rows()-100); // Mat imageROI = destination.submat(roi); /*ImageClass.getInstance().setImage(imageROI); displayImage();*/ //Imgproc.Canny(source,destination,0.05,0.15); Imgproc.findContours(destination, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); int total = contours.size(); int count = 0; //System.out.println(total); MatOfPoint[] cntrs = contours.toArray(new MatOfPoint[contours.size()]); ArrayList<Double> value = new ArrayList<Double>(); double temp = 0; for (int i = 0; i < contours.size(); i++) { if (contourArea(cntrs[i]) > 1/*&& contourArea(cntrs[i])<3000*/) { temp = 2 * Math.sqrt((contourArea(cntrs[i]) * (pixelArea)) / Math.PI); //temp = contourArea(cntrs[i]); if (temp > 0) { value.add(count, temp); System.out.println("area of contour " + count++ + " is : " + contourArea(cntrs[i])); } } } System.out.println("total number of contours : " + count); double[] values = new double[count]; for (int i = 0; i < count; i++) { //temp = value.get(i);//2*Math.sqrt((value.get(i)*127.024)/Math.PI); //if(temp>0) values[i] = value.get(i); //System.out.println("the diameter of particle "+i+ "is "+values[i]); // values[i]=(contourArea(cntrs[i])*127.024)/100; } //int number = 300; /*HistogramDataset dataset = new HistogramDataset(); dataset.setType(HistogramType.FREQUENCY); XYSeries series = new XYSeries("Particle Size Distribution"); try{ dataset.addSeries("Histogram1",values,number,0,300); for(int i=0;i<300;i++){ if(dataset.getYValue(0,i)>0) series.add(dataset.getXValue(0,i),dataset.getYValue(0,i)); } // XYDataset xydataset = new XYSeriesCollection(series); }catch(Exception e) { e.printStackTrace(); } XYDataset xydataset = new XYSeriesCollection(series); String plotTitle = "Particle Size Distribution"; String xAxis = "particle diameter in microns"; String yAxis = "particle count"; PlotOrientation orientation = PlotOrientation.VERTICAL; boolean show = true; boolean toolTips = true; boolean urls = false; JFreeChart chart1 = ChartFactory.createXYLineChart(plotTitle,xAxis,yAxis,xydataset,orientation,show,toolTips,urls); JFreeChart chart2 = ChartFactory.createHistogram(plotTitle,xAxis,yAxis,dataset,orientation,show,toolTips,urls); int width1 = 500; int height1 = 500; ChartFrame frame1 = new ChartFrame("Coal PSD",chart1); frame1.setSize(width1,height1); frame1.setVisible(true); frame1.setDefaultCloseOperation(DISPOSE_ON_CLOSE); int width2 = 500; int height2 = 500; ChartFrame frame2 = new ChartFrame("Coal PSD",chart2); frame2.setSize(width2,height2); frame2.setVisible(true); frame2.setDefaultCloseOperation(DISPOSE_ON_CLOSE);*/ } catch (NullPointerException e) { System.err.println("..........Please load a valid Image.........."); } // TODO add your handling code here: }
From source file:tv.danmaku.ijk.media.example.activities.VideoActivity.java
License:Apache License
public Mat onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba();/* ww w . j a v a 2s . c o m*/ mGray = inputFrame.gray(); // return mRgba; // iThreshold = 10000; //Imgproc.blur(mRgba, mRgba, new Size(5,5)); Imgproc.GaussianBlur(mRgba, mRgba, new org.opencv.core.Size(3, 3), 1, 1); //Imgproc.medianBlur(mRgba, mRgba, 3); if (!mIsColorSelected) return mRgba; List<MatOfPoint> contours = mDetector.getContours(); mDetector.process(mRgba); Log.d(TAG, "Contours count: " + contours.size()); if (contours.size() <= 0) { return mRgba; } RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(0).toArray())); double boundWidth = rect.size.width; double boundHeight = rect.size.height; int boundPos = 0; for (int i = 1; i < contours.size(); i++) { rect = Imgproc.minAreaRect(new MatOfPoint2f(contours.get(i).toArray())); if (rect.size.width * rect.size.height > boundWidth * boundHeight) { boundWidth = rect.size.width; boundHeight = rect.size.height; boundPos = i; } } Rect boundRect = Imgproc.boundingRect(new MatOfPoint(contours.get(boundPos).toArray())); Imgproc.rectangle(mRgba, boundRect.tl(), boundRect.br(), CONTOUR_COLOR_WHITE, 2, 8, 0); Log.d(TAG, " Row start [" + (int) boundRect.tl().y + "] row end [" + (int) boundRect.br().y + "] Col start [" + (int) boundRect.tl().x + "] Col end [" + (int) boundRect.br().x + "]"); int rectHeightThresh = 0; double a = boundRect.br().y - boundRect.tl().y; a = a * 0.7; a = boundRect.tl().y + a; Log.d(TAG, " A [" + a + "] br y - tl y = [" + (boundRect.br().y - boundRect.tl().y) + "]"); //Core.rectangle( mRgba, boundRect.tl(), boundRect.br(), CONTOUR_COLOR, 2, 8, 0 ); Imgproc.rectangle(mRgba, boundRect.tl(), new Point(boundRect.br().x, a), CONTOUR_COLOR, 2, 8, 0); MatOfPoint2f pointMat = new MatOfPoint2f(); Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(boundPos).toArray()), pointMat, 3, true); contours.set(boundPos, new MatOfPoint(pointMat.toArray())); MatOfInt hull = new MatOfInt(); MatOfInt4 convexDefect = new MatOfInt4(); Imgproc.convexHull(new MatOfPoint(contours.get(boundPos).toArray()), hull); if (hull.toArray().length < 3) return mRgba; Imgproc.convexityDefects(new MatOfPoint(contours.get(boundPos).toArray()), hull, convexDefect); List<MatOfPoint> hullPoints = new LinkedList<MatOfPoint>(); List<Point> listPo = new LinkedList<Point>(); for (int j = 0; j < hull.toList().size(); j++) { listPo.add(contours.get(boundPos).toList().get(hull.toList().get(j))); } MatOfPoint e = new MatOfPoint(); e.fromList(listPo); hullPoints.add(e); List<MatOfPoint> defectPoints = new LinkedList<MatOfPoint>(); List<Point> listPoDefect = new LinkedList<Point>(); for (int j = 0; j < convexDefect.toList().size(); j = j + 4) { Point farPoint = contours.get(boundPos).toList().get(convexDefect.toList().get(j + 2)); Integer depth = convexDefect.toList().get(j + 3); if (depth > iThreshold && farPoint.y < a) { listPoDefect.add(contours.get(boundPos).toList().get(convexDefect.toList().get(j + 2))); } Log.d(TAG, "defects [" + j + "] " + convexDefect.toList().get(j + 3)); } MatOfPoint e2 = new MatOfPoint(); e2.fromList(listPo); defectPoints.add(e2); Log.d(TAG, "hull: " + hull.toList()); Log.d(TAG, "defects: " + convexDefect.toList()); Imgproc.drawContours(mRgba, hullPoints, -1, CONTOUR_COLOR, 3); int defectsTotal = (int) convexDefect.total(); Log.d(TAG, "Defect total " + defectsTotal); this.numberOfFingers = listPoDefect.size(); if (this.numberOfFingers > 5) this.numberOfFingers = 5; mHandler.post(mUpdateFingerCountResults); for (Point p : listPoDefect) { Imgproc.circle(mRgba, p, 6, new Scalar(255, 0, 255)); } return mRgba; }