List of usage examples for org.opencv.core Rect Rect
public Rect()
From source file:by.zuyeu.deyestracker.core.util.CVCoreUtils.java
public static Rect sumRect(Rect r1, Rect r2) { if (r1 == null && r2 == null) { return null; }/* w w w. j a va 2s . co m*/ if (r1 == null && r2 != null) { return r2; } if (r2 == null && r1 != null) { return r1; } final Rect sum = new Rect(); sum.x = r1.x + r2.x; sum.y = r1.y + r2.y; sum.width = r1.width + r2.width; sum.height = r1.height + r2.height; return sum; }
From source file:by.zuyeu.deyestracker.core.util.CVCoreUtils.java
public static Rect divideRect(Rect p, int divider) { final Rect result = new Rect(); result.x = p.x / divider;// ww w . ja va2 s.c o m result.y = p.y / divider; result.width = p.width / divider; result.height = p.height / divider; return result; }
From source file:by.zuyeu.deyestracker.reader.util.ObjectTransformer.java
private static Rect transformRect2ToCVType(Rect2 rect2) { final Rect rect = new Rect(); rect.x = rect2.getX();//from w w w. j a v a 2 s.co m rect.y = rect2.getY(); rect.width = rect2.getWidth(); rect.height = rect2.getHeight(); return rect; }
From source file:classes.FloodFiller.java
private void fillFrom(Point seed, int lo, int up, Scalar backgroundColor, Scalar contourFillingColor) { Mat object = ObjectGenerator.extract(image, seed.x, seed.y, 10, 10); this.meanColor = Core.mean(object); Rect ccomp = new Rect(); Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1); int connectivity = 4; int newMaskVal = 255; int ffillMode = 1; int flags = connectivity + (newMaskVal << 8) + (ffillMode == 1 ? Imgproc.FLOODFILL_FIXED_RANGE : 0); Scalar newVal = new Scalar(0.299, 0.587, 0.114); Imgproc.threshold(mask, mask, 1, 128, Imgproc.THRESH_BINARY); filledArea = Imgproc.floodFill(image.clone(), mask, seed, newVal, ccomp, new Scalar(lo, lo, lo), new Scalar(up, up, up), flags); // Highgui.imwrite("mask.png", mask); ImageUtils.saveImage(mask, "mask.png", request); morphologicalImage = new Mat(image.size(), CvType.CV_8UC3); Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1)); ArrayList<Mat> mask3 = new ArrayList<Mat>(); mask3.add(mask);//from w w w.j av a 2 s. c o m mask3.add(mask); mask3.add(mask); Core.merge(mask3, mask); // Applying morphological filters Imgproc.erode(mask, morphologicalImage, element); Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_CLOSE, element, new Point(-1, -1), 9); Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_OPEN, element, new Point(-1, -1), 2); Imgproc.resize(morphologicalImage, morphologicalImage, image.size()); // Highgui.imwrite("morphologicalImage.png", morphologicalImage); ImageUtils.saveImage(morphologicalImage, "morphologicalImage.png", request); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Core.split(mask, mask3); Mat binarymorphologicalImage = mask3.get(0); Imgproc.findContours(binarymorphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); contoursImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor); int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours Imgproc.drawContours(contoursImage, contours, -1, contourFillingColor, thickness); // Drawing all the contours found // Highgui.imwrite("allContoursImage.png", contoursImage); ImageUtils.saveImage(contoursImage, "allContoursImage.png", request); if (contours.size() > 1) { int minContourWith = 20; int minContourHeight = 20; int maxContourWith = 6400 / 2; int maxContourHeight = 4800 / 2; contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight); } if (contours.size() > 0) { MatOfPoint biggestContour = contours.get(0); // getting the biggest contour contourArea = Imgproc.contourArea(biggestContour); if (contours.size() > 1) { biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one } Point[] points = biggestContour.toArray(); path = "M " + (int) points[0].x + " " + (int) points[0].y + " "; for (int i = 1; i < points.length; ++i) { Point v = points[i]; path += "L " + (int) v.x + " " + (int) v.y + " "; } path += "Z"; biggestContourImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor); Imgproc.drawContours(biggestContourImage, contours, 0, contourFillingColor, thickness); // Highgui.imwrite("biggestContourImage.png", biggestContourImage); ImageUtils.saveImage(biggestContourImage, "biggestContourImage.png", request); Mat maskForColorExtraction = biggestContourImage.clone(); if (isWhite(backgroundColor)) { Imgproc.dilate(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3); } else { Imgproc.erode(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3); } // Highgui.imwrite("maskForColorExtraction.png", maskForColorExtraction); ImageUtils.saveImage(maskForColorExtraction, "maskForColorExtraction.png", request); Mat extractedColor = new Mat(); if (isBlack(backgroundColor) && isWhite(contourFillingColor)) { Core.bitwise_and(maskForColorExtraction, image, extractedColor); } else { Core.bitwise_or(maskForColorExtraction, image, extractedColor); } // Highgui.imwrite("extractedColor.png", extractedColor); ImageUtils.saveImage(extractedColor, "extractedColor.png", request); computedSearchWindow = Imgproc.boundingRect(biggestContour); topLeftCorner = computedSearchWindow.tl(); Rect croppingRect = new Rect(computedSearchWindow.x, computedSearchWindow.y, computedSearchWindow.width - 1, computedSearchWindow.height - 1); Mat imageForTextRecognition = new Mat(extractedColor.clone(), croppingRect); // Highgui.imwrite(outImageName, imageForTextRecognition); ImageUtils.saveImage(imageForTextRecognition, outImageName, request); // // // Mat data = new Mat(imageForTextRecognition.size(), CvType.CV_8UC3, backgroundColor); // imageForTextRecognition.copyTo(data); // data.convertTo(data, CvType.CV_8UC3); // // // The meanColor variable represents the color in the GBR space, the following line transforms this to the RGB color space, which // // is assumed in the prepareImage method of the TextRecognitionPreparer class // Scalar userColor = new Scalar(meanColor.val[2], meanColor.val[1], meanColor.val[0]); // // ArrayList<String> recognizableImageNames = TextRecognitionPreparer.generateRecognizableImagesNames(data, backgroundColor, userColor); // for (String imageName : recognizableImageNames) { // // try { // // First recognition step // String recognizedText = TextRecognizer.recognize(imageName, true).trim(); // if (recognizedText != null && !recognizedText.isEmpty()) { // recognizedStrings.add(recognizedText); // } // // Second recognition step // recognizedText = TextRecognizer.recognize(imageName, false).trim(); // if (recognizedText != null && !recognizedText.isEmpty()) { // recognizedStrings.add(recognizedText); // } // // } catch (Exception e) { // } // } // //// ArrayList<BufferedImage> recognizableBufferedImages = TextRecognitionPreparer.generateRecognizableBufferedImages(data, backgroundColor, userColor); //// for (BufferedImage bufferedImage : recognizableBufferedImages) { //// try { //// // First recognition step //// String recognizedText = TextRecognizer.recognize(bufferedImage, true).trim(); //// if (recognizedText != null && !recognizedText.isEmpty()) { //// recognizedStrings.add(recognizedText); //// } //// // Second recognition step //// recognizedText = TextRecognizer.recognize(bufferedImage, false).trim(); //// if (recognizedText != null && !recognizedText.isEmpty()) { //// recognizedStrings.add(recognizedText); //// } //// //// } catch (Exception e) { //// } //// } // // // // compute all moments Moments mom = Imgproc.moments(biggestContour); massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00()); // draw black dot Core.circle(contoursImage, massCenter, 4, contourFillingColor, 8); } }
From source file:classes.FloodFillFacade.java
public Mat fill(Mat image, Mat mask, int x, int y, Scalar newVal) { Point seedPoint = new Point(x, y); Rect rect = new Rect(); // Scalar newVal = isColored() ? new Scalar(b, g, r) : new Scalar(r * 0.299 + g * 0.587 + b * 0.114); Scalar lowerDifference = new Scalar(lowerDiff, lowerDiff, lowerDiff); Scalar upperDifference = new Scalar(upperDiff, upperDiff, upperDiff); if (range == NULL_RANGE) { lowerDifference = new Scalar(0, 0, 0); upperDifference = new Scalar(0, 0, 0); }/*from ww w . j a va 2s . c om*/ int flags = connectivity + (newMaskVal << 8) + ((range == FIXED_RANGE ? Imgproc.FLOODFILL_FIXED_RANGE : 0) | 0);//Imgproc.FLOODFILL_MASK_ONLY); int area = 0; if (masked) { area = Imgproc.floodFill(image, mask, seedPoint, newVal, rect, lowerDifference, upperDifference, flags); } else { area = Imgproc.floodFill(image, new Mat(), seedPoint, newVal, rect, lowerDifference, upperDifference, flags); } // Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\image_after_flood_" + cont + ".png", image); // Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\mask_" + cont + ".png", mask); // System.out.println("area: " + area); cont++; return image; }
From source file:classes.ObjectFinder.java
private void computeSearchWindow() { List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); // a vector of contours // retrieve the external contours // all pixels of each contours Imgproc.findContours(this.morphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);/* w w w . j a v a 2 s .co m*/ // Draw black contours on a white image this.contoursImage = new Mat(morphologicalImage.size(), CvType.CV_8U, new Scalar(255)); if (contours.size() > 1) { int minContourWith = 20; int minContourHeight = 20; int maxContourWith = 6400 / 2; int maxContourHeight = 4800 / 2; contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight); } if (contours.size() > 1) { Collections.sort(contours, new ContourComparator()); // Sorttig the contours to take ONLY the bigger one } computedSearchWindow = new Rect(); massCenter = new Point(-1, -1); if (contours.size() > 0) { this.firstContour = contours.get(0); Mat contournedImage = this.firstContour; // draw all contours in black with a thickness of 2 Scalar color = new Scalar(0); int thickness = 2; Imgproc.drawContours(contoursImage, contours, 0, color, thickness); // // testing the bounding box computedSearchWindow = Imgproc.boundingRect(this.firstContour); topLeftCorner = computedSearchWindow.tl(); // compute all moments Moments mom = Imgproc.moments(contournedImage); massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00()); // draw black dot Core.circle(contoursImage, massCenter, 4, color, 8); } }
From source file:com.mycompany.objectdetection.ObjectDetector.java
public void findObjects() { // Imgproc.cvtColor(img, imgGrayscale, Imgproc.COLOR_RGBA2GRAY, 1); // Core.convertScaleAbs(img, imgGrayscale); // Core.normalize(imgGrayscale, imgMeanShifted, 0.0, 1.0, NORM_MINMAX); preProcessImg();//from ww w . ja v a 2 s. c o m toGrayScale(imgMeanShifted); detectEdges(imgGrayscale); Imgproc.findContours(imgCanny, contours, imgCanny, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); objList = new ArrayList(); for (MatOfPoint mop : contours) { MatOfPoint2f m2p; m2p = new MatOfPoint2f(mop.toArray()); Double peri = Imgproc.arcLength(m2p, true); Imgproc.approxPolyDP(m2p, m2p, 0.02 * peri, true); Imgproc.drawContours(imgOut, contours, -1, new Scalar(0, 0, 255), 2); float area = img.width() * img.height(); Rect rect = Imgproc.boundingRect(mop); objList.add(rect); Imgproc.rectangle(imgOut, rect.tl(), rect.br(), new Scalar(255, 0, 0)); } Collections.sort(objList, new Comparator<Rect>() { @Override public int compare(Rect r1, Rect r2) { return (int) (r2.area() - r1.area()); } }); List<Rect> arr = objList; while (arr.size() > 0) { //System.out.println("---->" + arr); Rect bigRect = arr.get(0); arr.remove(0); Rect bigRect2 = new Rect(); while (!equals(bigRect, bigRect2)) { bigRect2 = bigRect; for (int i = 0; i < arr.size(); ++i) { // System.out.println("elotte"+arr.get(i)); if (doOverlap(bigRect, arr.get(i))) { //System.out.println("utana"+arr.get(i)); bigRect = union(bigRect, arr.get(i)); arr.remove(i); break; } } } mainRect = bigRect; if (objList.size() > 5 && mainRect.area() >= img.width() * img.height() * 3 / 100) { Imgproc.rectangle(imgOut, bigRect.tl(), bigRect.br(), new Scalar(255, 255, 0)); mainObjects.add(mainRect); } else if (objList.size() <= 5) { mainObjects.add(mainRect); } } }
From source file:com.serenegiant.usbcameratest.MainActivity.java
License:Apache License
@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageButton1 = (ImageButton) findViewById(R.id.imageButton1); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); imageView1 = (ImageView) findViewById(R.id.imageView1); imageButton1.setOnClickListener(imageButton1ClickListener); button1.setOnClickListener(button1ClickListener); button2.setOnClickListener(button2ClickListener); button3.setOnClickListener(button3ClickListener); button4.setOnClickListener(button4ClickListener); spinner1 = (Spinner) findViewById(R.id.spinner1); mUVCCameraView = (UVCCameraTextureView) findViewById(R.id.UVCCameraTextureView1); mUVCCameraView.setAspectRatio(UVCCamera.DEFAULT_PREVIEW_WIDTH / (float) UVCCamera.DEFAULT_PREVIEW_HEIGHT); mUSBMonitor = new USBMonitor(this, mOnDeviceConnectListener); textView1 = (TextView) findViewById(R.id.textView1); tmpImg = new template_image(MainActivity.this); measure = new stem_measure(MainActivity.this); soap = new my_soap(); createTemplateFlag = false;//from w ww . j ava 2 s. com templateRect = new Rect(); templateRect.x = -1; templateRect.y = -1; }
From source file:cubesolversimulator.VisualInputForm.java
private void reorderContours(List<Rect> roi) { Rect temp = new Rect(); for (int i = 0; (i < roi.size() - 1); i++) { for (int j = 0; j < (roi.size() - 1); j++) { if (roi.get(j).y > roi.get(j + 1).y) { temp = roi.get(j);//from ww w.j av a 2s . c o m roi.set(j, roi.get(j + 1)); roi.set(j + 1, temp); } } } for (int i = 0; i < (roi.size() / 3); i++) { for (int j = (i * 3); j < (i + 1) * 3; j++) { for (int k = (i * 3); k < ((i + 1) * 3) - 1; k++) { if (roi.get(k).x > roi.get(k + 1).x) { temp = roi.get(k); roi.set(k, roi.get(k + 1)); roi.set(k + 1, temp); } } } } }
From source file:emotion.Eye.java
private Rect recalculate(Rect _input, Mat canvas) { Rect output = new Rect(); int width = (int) (_input.width * 1.3); int height = (int) (_input.height * 1.3); output.x = _input.x - (width - _input.width) / 2; output.y = _input.y - (height) / 4;/*from w w w.j a v a 2 s.co m*/ if (output.x < 0) { output.x = 0; } else if (output.x >= canvas.width()) { output.x = canvas.width() - 1; } if (output.y < 0) { output.y = 0; } else if (output.y >= canvas.height()) { output.y = canvas.height() - 1; } output.width = width; output.height = height; return output; }