List of usage examples for org.opencv.imgproc Imgproc getRotationMatrix2D
public static Mat getRotationMatrix2D(Point center, double angle, double scale)
From source file:OCV_GetRotationMatrix2D.java
License:Open Source License
@Override public void run(ImageProcessor ip) { Mat mat = Imgproc.getRotationMatrix2D(new Point(center_x, center_y), angle, scale); if (mat == null || mat.rows() <= 0 || mat.cols() <= 0) { IJ.showMessage("Output is null or error"); return;/*from w ww . j a v a2 s .co m*/ } ResultsTable rt = OCV__LoadLibrary.GetResultsTable(true); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(0, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(0, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(0, 2)[0])); rt.incrementCounter(); rt.addValue("Column01", String.valueOf(mat.get(1, 0)[0])); rt.addValue("Column02", String.valueOf(mat.get(1, 1)[0])); rt.addValue("Column03", String.valueOf(mat.get(1, 2)[0])); rt.show("Results"); }
From source file:LicenseDetection.java
public void rotate(Mat src, Mat dst, Point point, double angle) { Mat M = Imgproc.getRotationMatrix2D(point, angle, 1.0); Imgproc.warpAffine(src, dst, M, src.size(), Imgproc.INTER_CUBIC); }
From source file:ch.zhaw.facerecognitionlibrary.Helpers.MatOperation.java
License:Open Source License
public static Rect[] rotateFaces(Mat img, Rect[] faces, int angle) { Point center = new Point(img.cols() / 2, img.rows() / 2); Mat rotMat = Imgproc.getRotationMatrix2D(center, angle, 1); rotMat.convertTo(rotMat, CvType.CV_32FC1); float scale = img.cols() / img.rows(); for (Rect face : faces) { Mat m = new Mat(3, 1, CvType.CV_32FC1); m.put(0, 0, face.x);/*from w w w .j a va2 s . com*/ m.put(1, 0, face.y); m.put(2, 0, 1); Mat res = Mat.zeros(2, 1, CvType.CV_32FC1); Core.gemm(rotMat, m, 1, new Mat(), 0, res, 0); face.x = (int) res.get(0, 0)[0]; face.y = (int) res.get(1, 0)[0]; if (angle == 270 || angle == -90) { face.x = (int) (face.x * scale - face.width); face.x = face.x + face.width / 4; face.y = face.y + face.height / 4; } else if (angle == 180 || angle == -180) { face.x = face.x - face.width; face.y = face.y - face.height; } else if (angle == 90 || angle == -270) { face.y = (int) (face.y * scale - face.height); face.x = face.x - face.width / 4; face.y = face.y - face.height / 4; } } return faces; }
From source file:ch.zhaw.facerecognitionlibrary.PreProcessor.StandardPreprocessing.EyeAlignment.java
License:Open Source License
public PreProcessor preprocessImage(PreProcessor preProcessor) { List<Mat> images = preProcessor.getImages(); List<Mat> processed = new ArrayList<Mat>(); preProcessor.setEyes();// w ww. j av a2s . c om Eyes[] eyes = preProcessor.getEyes(); if (eyes == null || eyes[0] == null) { return null; } for (int i = 0; i < images.size(); i++) { Mat img = images.get(i); Eyes eye = eyes[i]; double desiredLen = (DESIRED_LEFT_EYE_X - DESIRED_RIGHT_EYE_X) * img.cols(); double scale = 0.9 * desiredLen / eye.getDist(); MatOfFloat leftCenter = eye.getLeftCenter(); MatOfFloat rightCenter = eye.getRightCenter(); double centerX = ((leftCenter.get(0, 0)[0] + rightCenter.get(0, 0)[0]) / 2); double centerY = ((leftCenter.get(1, 0)[0] + rightCenter.get(1, 0)[0]) / 2); Mat rotMat = Imgproc.getRotationMatrix2D(new Point(centerX, centerY), eye.getAngle(), scale); rotMat.put(2, 0, img.cols() * 0.5 - centerX); rotMat.put(2, 1, img.rows() * DESIRED_RIGHT_EYE_Y - centerY); Imgproc.warpAffine(img, img, rotMat, new Size(img.cols(), img.rows())); processed.add(img); } preProcessor.setImages(processed); return preProcessor; }
From source file:classes.TextExtractor.java
public void extractText(Rect roi, double roiAngle) throws Exception { Point roiTopLeft = roi.tl();//from w w w .j a v a 2 s . c o 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:com.joravasal.keyface.CameraAccessView.java
License:Open Source License
public Mat correctCameraImage(Mat image) { //Log.i(tag, "Correcting image rotation"); //Check rotation of device int rotation = ((KeyFaceActivity) this.getContext()).getWindowManager().getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: int degrees = 90; //Mirror (y axis) if front camera and rotation in any case Mat imageResult = new Mat(); //For some reason to rotate the image properly, we have to set the center like this Point center = new Point(image.width() / 2, image.width() / 2); Mat transform = Imgproc.getRotationMatrix2D(center, degrees, 1.0); try {// w w w . j a v a2 s. c om Imgproc.warpAffine(image, imageResult, transform, new Size(image.height(), image.width())); } catch (CvException e) { System.err.println(e.getMessage()); } if (KeyFaceActivity.cameraRearActive) Core.flip(imageResult, imageResult, -1); else Core.flip(imageResult, imageResult, 1); return imageResult; case Surface.ROTATION_90: //Mirror on y axis if front camera if (!KeyFaceActivity.cameraRearActive) Core.flip(image, image, 1); break; case Surface.ROTATION_180: //Never gets here but just in case: break; case Surface.ROTATION_270: //Mirror on the x axis if rear camera, both axis if front camera if (KeyFaceActivity.cameraRearActive) Core.flip(image, image, -1); else Core.flip(image, image, 0); break; default: break; } return image; }
From source file:com.shootoff.camera.autocalibration.AutoCalibrationManager.java
License:Open Source License
private Mat getRotationMatrix(final Point center, final double rotationAngle) { return Imgproc.getRotationMatrix2D(center, rotationAngle, 1.0); }
From source file:karthik.Barcode.CandidateMatrixBarcode.java
License:Open Source License
CandidateResult NormalizeCandidateRegion(double angle) { /* candidateRegion is the RotatedRect which contains a candidate region for the barcode // angle is the rotation angle or USE_ROTATED_RECT_ANGLE for this function to // estimate rotation angle from the rect parameter // returns Mat containing cropped area(region of interest) with just the barcode // The barcode region is from the *original* image, not the scaled image // the cropped area is also rotated as necessary to be horizontal or vertical rather than skewed // Some parts of this function are from http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/ // and http://stackoverflow.com/questions/22041699/rotate-an-image-without-cropping-in-opencv-in-c *//* ww w. j av a 2s. c om*/ double rotation_angle; CandidateResult result = new CandidateResult(); // scale candidate region back up to original size to return cropped part from *original* image // need the 1.0 there to force floating-point arithmetic from int values double scale_factor = img_details.src_original.rows() / (1.0 * img_details.src_grayscale.rows()); // expand the region found - this helps capture the entire code including the border zone candidateRegion.size.width += 2 * params.RECT_WIDTH; candidateRegion.size.height += 2 * params.RECT_HEIGHT; // calculate location of rectangle in original image and its corner points RotatedRect scaledRegion = new RotatedRect(candidateRegion.center, candidateRegion.size, candidateRegion.angle); scaledRegion.center.x = scaledRegion.center.x * scale_factor; scaledRegion.center.y = scaledRegion.center.y * scale_factor; scaledRegion.size.height *= scale_factor; scaledRegion.size.width *= scale_factor; scaledRegion.points(img_details.scaledCorners); // lets get the coordinates of the ROI in the original image and save it result.ROI_coords = Arrays.copyOf(img_details.scaledCorners, 4); // get the bounding rectangle of the ROI by sorting its corner points // we do it manually because RotatedRect can generate corner points outside the Mat area Arrays.sort(img_details.scaledCorners, CandidateBarcode.get_x_comparator()); int leftCol = (int) img_details.scaledCorners[0].x; int rightCol = (int) img_details.scaledCorners[3].x; leftCol = (leftCol < 0) ? 0 : leftCol; rightCol = (rightCol > img_details.src_original.cols() - 1) ? img_details.src_original.cols() - 1 : rightCol; Arrays.sort(img_details.scaledCorners, CandidateBarcode.get_y_comparator()); int topRow = (int) img_details.scaledCorners[0].y; int bottomRow = (int) img_details.scaledCorners[3].y; topRow = (topRow < 0) ? 0 : topRow; bottomRow = (bottomRow > img_details.src_original.rows() - 1) ? img_details.src_original.rows() - 1 : bottomRow; Mat ROI_region = img_details.src_original.submat(topRow, bottomRow, leftCol, rightCol); // create a container that is a square with side = diagonal of ROI. // this is large enough to accommodate the ROI region with rotation without cropping it int orig_rows = bottomRow - topRow; int orig_cols = rightCol - leftCol; int diagonal = (int) Math.sqrt(orig_rows * orig_rows + orig_cols * orig_cols); int newWidth = diagonal + 1; int newHeight = diagonal + 1; int offsetX = (newWidth - orig_cols) / 2; int offsetY = (newHeight - orig_rows) / 2; Mat enlarged_ROI_container = new Mat(newWidth, newHeight, img_details.src_original.type()); enlarged_ROI_container.setTo(ZERO_SCALAR); // copy ROI to centre of container and rotate it ROI_region.copyTo(enlarged_ROI_container.rowRange(offsetY, offsetY + orig_rows).colRange(offsetX, offsetX + orig_cols)); Point enlarged_ROI_container_centre = new Point(enlarged_ROI_container.rows() / 2.0, enlarged_ROI_container.cols() / 2.0); Mat rotated = Mat.zeros(enlarged_ROI_container.size(), enlarged_ROI_container.type()); if (angle == Barcode.USE_ROTATED_RECT_ANGLE) rotation_angle = estimate_barcode_orientation(); else rotation_angle = angle; // perform the affine transformation img_details.rotation_matrix = Imgproc.getRotationMatrix2D(enlarged_ROI_container_centre, rotation_angle, 1.0); img_details.rotation_matrix.convertTo(img_details.rotation_matrix, CvType.CV_32F); // convert type so matrix multip. works properly img_details.newCornerCoord.setTo(ZERO_SCALAR); // convert scaledCorners to contain locations of corners in enlarged_ROI_container Mat img_details.scaledCorners[0] = new Point(offsetX, offsetY); img_details.scaledCorners[1] = new Point(offsetX, offsetY + orig_rows); img_details.scaledCorners[2] = new Point(offsetX + orig_cols, offsetY); img_details.scaledCorners[3] = new Point(offsetX + orig_cols, offsetY + orig_rows); // calculate the new location for each corner point of the rectangle ROI after rotation for (int r = 0; r < 4; r++) { img_details.coord.put(0, 0, img_details.scaledCorners[r].x); img_details.coord.put(1, 0, img_details.scaledCorners[r].y); Core.gemm(img_details.rotation_matrix, img_details.coord, 1, img_details.delta, 0, img_details.newCornerCoord); updatePoint(img_details.newCornerPoints.get(r), img_details.newCornerCoord.get(0, 0)[0], img_details.newCornerCoord.get(1, 0)[0]); } rotated.setTo(ZERO_SCALAR); Imgproc.warpAffine(enlarged_ROI_container, rotated, img_details.rotation_matrix, enlarged_ROI_container.size(), Imgproc.INTER_CUBIC); // sort rectangles points in order by first sorting all 4 points based on x // we then sort the first two based on y and then the next two based on y // this leaves the array in order top-left, bottom-left, top-right, bottom-right Collections.sort(img_details.newCornerPoints, CandidateBarcode.get_x_comparator()); Collections.sort(img_details.newCornerPoints.subList(0, 2), CandidateBarcode.get_y_comparator()); Collections.sort(img_details.newCornerPoints.subList(2, 4), CandidateBarcode.get_y_comparator()); // calc height and width of rectangular region double height = length(img_details.newCornerPoints.get(1), img_details.newCornerPoints.get(0)); double width = length(img_details.newCornerPoints.get(2), img_details.newCornerPoints.get(0)); // create destination points for warpPerspective to map to updatePoint(img_details.transformedPoints.get(0), 0, 0); updatePoint(img_details.transformedPoints.get(1), 0, height); updatePoint(img_details.transformedPoints.get(2), width, 0); updatePoint(img_details.transformedPoints.get(3), width, height); Mat perspectiveTransform = Imgproc.getPerspectiveTransform( Converters.vector_Point2f_to_Mat(img_details.newCornerPoints), Converters.vector_Point2f_to_Mat(img_details.transformedPoints)); Mat perspectiveOut = Mat.zeros((int) height + 2, (int) width + 2, CvType.CV_32F); Imgproc.warpPerspective(rotated, perspectiveOut, perspectiveTransform, perspectiveOut.size(), Imgproc.INTER_CUBIC); result.ROI = perspectiveOut; return result; }
From source file:logic.analyzer.AnalyzerIF.java
/** * Obtain rotation matrix for further image rotation * Angle controlling algorithm: compute angle between eyes * check angle range: if it is bigger * or smaller than threshold, we return null * and freeze frame * check anle changing speed: compute first * deriviation on time. In other words, we * compute differences betwee angle that was * computed before and now. If angle value * changes faster than threshold, we reset * angle and compute all face features from * the begininig. Otherwise, compute rotation * matrix. * @param frame//from w w w .j a v a 2 s.com * @param faceRect * @param prevalpha angle, computed previously. If it is null, do not compute, * because we are dealing with a static image * @param p1 * @param p2 * @return rotation matrix */ public Mat getRotationMat(Mat frame, Rect faceRect, Point p1, Point p2, Point prevAlpha) { //compute angle double deltaX = p1.x - p2.x; double deltaY = p1.y - p2.y; double alpha = Math.tan(deltaY / deltaX) * 180. / Math.PI * (-1); double absAlpha = Math.abs(alpha); if (absAlpha > Parameters.maxIrisAngle) return null; if (prevAlpha != null) { double alphaDiff = Math.abs(absAlpha - prevAlpha.x); LOG.info("AlphaDif = " + alphaDiff); if (alphaDiff > Parameters.maxPrevAngDiff) { prevAlpha.x = -1; return null; } prevAlpha.x = absAlpha; } //save roteted angle prevAlpha.y = alpha; Point centerPoint = new Point(faceRect.x + faceRect.width / 2, faceRect.y + faceRect.height / 2); return Imgproc.getRotationMatrix2D(centerPoint, -alpha, 1); }
From source file:opencv.CaptchaDetection.java
/*** * ?/* w w w. j a v a 2 s .co m*/ * @param src * @param dst * @param degree */ private static void rotateImage(Mat src, Mat dst, int degree) { Point center = new Point(src.cols() / 2.0 + 0.5, src.rows() / 2.0 + 0.5); Mat M = Imgproc.getRotationMatrix2D(center, degree, 1.0); Imgproc.warpAffine(src, dst, M, src.size()); }