List of usage examples for org.opencv.core Rect size
public Size size()
From source file:classes.TextExtractor.java
public void extractText(Rect roi, double roiAngle) throws Exception { Point roiTopLeft = roi.tl();/* ww w .j a v a 2 s. co m*/ double radians = Math.toRadians(roiAngle); double sin = Math.abs(Math.sin(radians)); double cos = Math.abs(Math.cos(radians)); int newWidth = (int) (image.width() * cos + image.height() * sin); int newHeight = (int) (image.width() * sin + image.height() * cos); int[] newWidthHeight = { newWidth, newHeight }; int pivotX = newWidthHeight[0] / 2; int pivotY = newWidthHeight[1] / 2; Point center = new Point(pivotX, pivotY); Size targetSize = new Size(newWidthHeight[0], newWidthHeight[1]); Mat intermediateImage = new Mat(targetSize, image.type()); int offsetX = (newWidthHeight[0] - image.width()) / 2; int offsetY = (newWidthHeight[1] - image.height()) / 2; Point paddedTopLeft = new Point(roiTopLeft.x + offsetX, roiTopLeft.y + offsetY); Mat containerImage = intermediateImage.submat(offsetY, offsetY + image.height(), offsetX, offsetX + image.width()); image.copyTo(containerImage); Mat rotationMatrix = Imgproc.getRotationMatrix2D(center, roiAngle, 1.0); Point transformedTopLeft = transformPoint(paddedTopLeft, rotationMatrix); Mat rotatedImage = new Mat(); Imgproc.warpAffine(intermediateImage, rotatedImage, rotationMatrix, targetSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT, new Scalar(0)); ImageUtils.saveImage(rotatedImage, imageID + "_rotatedImage.png", request); double adjustedWidth = roi.size().width; double adjustedHeight = roi.size().height; if (transformedTopLeft.x + adjustedWidth > rotatedImage.width()) { adjustedWidth = rotatedImage.width() - transformedTopLeft.x; } if (transformedTopLeft.y + adjustedHeight > rotatedImage.height()) { adjustedHeight = rotatedImage.height() - transformedTopLeft.y; } Rect newROI = new Rect(transformedTopLeft, new Size(adjustedWidth, adjustedHeight)); Mat extractedROI = new Mat(rotatedImage, newROI); String fileName = ImageUtils.saveImage(extractedROI, imageID + "_ROI.png", request); extractText(fileName); }
From source file:opencltest.YetAnotherTestT.java
public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat kernel = new Mat(3, 3, CV_8UC1); kernel.put(0, 0, new double[] { 0, 1, 0, 1, 1, 1, 0, 1, 0 }); Mat source = Imgcodecs.imread("test.smaller.png"); Mat blur = new Mat(); Mat edges = new Mat(); Mat dilated = new Mat(); Imgproc.GaussianBlur(source, blur, new Size(13, 13), 0); Imgproc.Canny(blur, edges, 10, 30, 5, false); // Imgproc.cvtColor(edges, edges, Imgproc.COLOR_RGB2GRAY); // Imgproc.adaptiveThreshold(edges, edges, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 5, 2); // Core.bitwise_not(edges, edges); // Imgproc.dilate(edges, edges, kernel); // Imgproc.dilate(edges, dilated, kernel); dilated = edges;/* w w w . j a v a2 s . c om*/ // Core.bitwise_not(edges, edges); Mat lines = new Mat(); Imgproc.HoughLinesP(dilated, lines, 1, Math.PI / 180, 300, 10, 70); Mat empty = new Mat(source.height(), source.width(), source.type()); // paintLines(empty, lines); List<MatOfPoint> contours = new ArrayList<>(); Mat hier = new Mat(); Imgproc.findContours(edges, contours, hier, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); Mat foundSquare = new Mat(source.height(), source.width(), CvType.CV_8UC4); source.copyTo(foundSquare); List<Double> hor = new ArrayList<>(); for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) { MatOfPoint next = iterator.next(); Rect bounding = Imgproc.boundingRect(next); int tr = 20; if (diffLessThan(bounding.size().width - 40, tr) && diffLessThan(bounding.size().height - 40, tr)) { Imgproc.rectangle(empty, bounding.tl(), bounding.br(), randomColor(), 3); // hor.add(bounding.x + 0.0); hor.add(bounding.x + bounding.width / 2.0 + 0.0); drawRect(bounding, foundSquare); } } Imgcodecs.imwrite("test_2.png", source); Imgcodecs.imwrite("test_3.png", dilated); Imgcodecs.imwrite("test_4.png", empty); Imgcodecs.imwrite("test_h.png", foundSquare); hor.sort(Double::compare); double low = hor.get(0); double hih = hor.get(hor.size() - 1); double n = hor.size(); Function<Double, Double> K = (d) -> (Math.abs(d) <= 1) ? ((3.0 / 4.0) * (1 - (d * d))) : 0;//epanechnikov kernel List<Double> result = new ArrayList<>(); double h = 10; for (int i = 0; i < source.width() + 1; i++) result.add(0.0); for (double d = low; d <= hih; d += 1) { double sum = 0; for (Double di : hor) { sum += K.apply((d - di) / h); } result.set((int) d, sum / (n * h)); System.out.println(sum / (n * h)); } normalize(result, 255); Mat test = new Mat(source.height(), source.width(), source.type()); source.copyTo(test); draw(result, test); Imgcodecs.imwrite("test_uwot.png", test); }
From source file:org.lasarobotics.vision.detection.objects.Rectangle.java
License:Open Source License
private void setRect(Rect rect) { this.rect = new RotatedRect( new Point(rect.tl().x + rect.size().width / 2, rect.tl().y + rect.size().height / 2), rect.size(), 0.0);// w w w .j a va 2 s . co m }
From source file:org.openpnp.machine.reference.ReferenceCamera.java
License:Open Source License
private Mat rotate(Mat mat, double rotation) { if (rotation == 0D) { return mat; }// w ww . j a v a2s . co m // See: // http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c Point center = new Point(mat.width() / 2D, mat.height() / 2D); Mat mapMatrix = Imgproc.getRotationMatrix2D(center, rotation, 1.0); // determine bounding rectangle Rect bbox = new RotatedRect(center, mat.size(), rotation).boundingRect(); // adjust transformation matrix double[] cx = mapMatrix.get(0, 2); double[] cy = mapMatrix.get(1, 2); cx[0] += bbox.width / 2D - center.x; cy[0] += bbox.height / 2D - center.y; mapMatrix.put(0, 2, cx); mapMatrix.put(1, 2, cy); Mat dst = new Mat(bbox.width, bbox.height, mat.type()); Imgproc.warpAffine(mat, dst, mapMatrix, bbox.size(), Imgproc.INTER_LINEAR); mat.release(); mapMatrix.release(); return dst; }
From source file:uk.ac.horizon.artcodes.drawer.MarkerThumbnailDrawer.java
License:Open Source License
@Override public Mat drawMarker(Marker marker, ArrayList<MatOfPoint> contours, Mat hierarchy, Rect boundingRect, Mat imageToDrawOn) {//from w w w. j ava 2s . c om if (imageToDrawOn == null) { if (boundingRect == null) { boundingRect = Imgproc.boundingRect(contours.get(marker.markerIndex)); } Mat output = new Mat(boundingRect.size(), CvType.CV_8UC4, BACKGROUND); Imgproc.drawContours(output, contours, marker.markerIndex, COLOR, Core.FILLED, Core.LINE_8, hierarchy, 2, new Point(-boundingRect.tl().x, -boundingRect.tl().y)); return output; } else { Imgproc.drawContours(imageToDrawOn, contours, marker.markerIndex, COLOR, Core.FILLED, Core.LINE_8, hierarchy, 2, new Point(0, 0)); return imageToDrawOn; } }