List of usage examples for org.opencv.imgproc Imgproc warpAffine
public static void warpAffine(Mat src, Mat dst, Mat M, Size dsize)
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();//from w ww.j a v a 2 s . co m 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: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 {/*from w ww.ja v a2 s. c o m*/ 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:fi.conf.tabare.ARDataProvider.java
private void detect() { //Mat composite_image; Mat input_image = new Mat(); Mat undistorted_image = new Mat(); Mat circles = new Mat(); MatOfKeyPoint mokp = new MatOfKeyPoint(); Mat cameraMatrix = null;/* w ww. j av a2 s.com*/ //List<Mat> channels = new LinkedList<>(); //Loop while (running) { try { if (inputVideo.read(input_image)) { Mat preview_image = null; if (selectedView == View.calib) preview_image = input_image.clone(); //Imgproc.cvtColor(input_image, input_image, Imgproc.COLOR_RGB2HSV); //Core.split(input_image, channels); Imgproc.cvtColor(input_image, input_image, Imgproc.COLOR_BGR2GRAY); //Imgproc.equalizeHist(input_image, input_image); input_image.convertTo(input_image, -1, params.contrast, params.brightness); //image*contrast[1.0-3.0] + brightness[0-255] doBlur(input_image, input_image, params.blur, params.blurAmount); if (selectedView == View.raw) preview_image = input_image.clone(); if (params.enableDistortion) { if (cameraMatrix == null) cameraMatrix = Imgproc.getDefaultNewCameraMatrix(Mat.eye(3, 3, CvType.CV_64F), new Size(input_image.width(), input_image.height()), true); Imgproc.warpAffine(input_image, input_image, shiftMat, frameSize); if (undistorted_image == null) undistorted_image = new Mat((int) frameSize.width * 2, (int) frameSize.height * 2, CvType.CV_64F); Imgproc.undistort(input_image, undistorted_image, cameraMatrix, distCoeffs); input_image = undistorted_image.clone(); if (selectedView == View.dist) preview_image = input_image.clone(); } // if(background == null) background = input_image.clone(); // if(recaptureBg){ // backgSubstractor.apply(background, background); // System.out.println(background.channels() + " " + background.size() ); // System.out.println(input_image.channels() + " " + input_image.size() ); // recaptureBg = false; // } // if(dynamicBGRemoval){ // //Imgproc.accumulateWeighted(input_image, background, dynamicBGAmount); // //Imgproc.accumulateWeighted(input_image, background, 1.0f); // //Core.subtract(input_image, background, input_image); // //Core.bitwise_xor(input_image, background, input_image); // // doBlur(input_image, background, Blur.normal_7x7, 0); //Blur a little, to get nicer result when substracting // backgSubstractor.apply(background, background, dynamicBGAmount); // } // if(background != null) Core.add(input_image, background, input_image); if (params.blobTracking) { Mat blobs_image = input_image.clone(); Imgproc.threshold(blobs_image, blobs_image, params.blobThreshold, 254, (params.blobThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY)); Size kernelSize = null; switch (params.blobMorpthKernelSize) { case size_3x3: kernelSize = new Size(3, 3); break; case size_5x5: kernelSize = new Size(5, 5); break; case size_7x7: kernelSize = new Size(7, 7); break; case size_9x9: kernelSize = new Size(9, 9); break; } int kernelType = -1; switch (params.blobMorphKernelShape) { case ellipse: kernelType = Imgproc.MORPH_ELLIPSE; break; case rect: kernelType = Imgproc.MORPH_RECT; break; default: break; } switch (params.blobMorphOps) { case dilate: Imgproc.dilate(blobs_image, blobs_image, Imgproc.getStructuringElement(kernelType, kernelSize)); break; case erode: Imgproc.erode(blobs_image, blobs_image, Imgproc.getStructuringElement(kernelType, kernelSize)); break; default: break; } if (blobFeatureDetector == null) blobFeatureDetector = FeatureDetector.create(FeatureDetector.SIMPLEBLOB); blobFeatureDetector.detect(blobs_image, mokp); blobData.add(mokp); if (selectedView == View.blob) preview_image = blobs_image.clone(); blobs_image.release(); } if (params.tripTracking) { Mat trips_image = undistorted_image.clone(); if (params.tripEnableThresholding) if (params.tripAdaptThreshold) { Imgproc.adaptiveThreshold(trips_image, trips_image, 255, (params.tripThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY), Imgproc.ADAPTIVE_THRESH_MEAN_C, 5, params.tripThreshold * 0.256f); } else { Imgproc.threshold(trips_image, trips_image, params.tripThreshold, 255, (params.tripThInverted ? Imgproc.THRESH_BINARY_INV : Imgproc.THRESH_BINARY)); } switch (params.tripMorphOps) { case dilate: Imgproc.dilate(trips_image, trips_image, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3))); break; case erode: Imgproc.erode(trips_image, trips_image, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(3, 3))); break; default: break; } //Imgproc.HoughCircles(tres, circ, Imgproc.CV_HOUGH_GRADIENT, 1, tres.height()/8, 80, 1+p.par4, p.par5, p.par6); Imgproc.HoughCircles(trips_image, circles, Imgproc.CV_HOUGH_GRADIENT, params.tripDP, params.tripCenterDist, params.tripCannyThresh, params.tripAccumThresh, params.tripRadMin, params.tripRadMax); for (int i = 0; i < circles.cols(); i++) { double[] coords = circles.get(0, i); if (coords == null || coords[0] <= 1 || coords[1] <= 1) continue; //If the circle is off the limits, or too small, don't process it. TripcodeCandidateSample tc = new TripcodeCandidateSample(undistorted_image, coords); if (tc.isValid()) tripcodeData.add(tc); } if (selectedView == View.trip) preview_image = trips_image.clone(); trips_image.release(); } if (preview_image != null) { camPreviewPanel.updatePreviewImage(preview_image); preview_image.release(); } } else { System.out.println("frame/cam failiure!"); } } catch (Exception e) { e.printStackTrace(); running = false; } //FPS calculations if (camPreviewPanel != null) { long t = System.currentTimeMillis(); detectTime = (t - lastFrameDetectTime); lastFrameDetectTime = t; camPreviewPanel.updateDetectTime(detectTime); } } //De-init circles.release(); undistorted_image.release(); input_image.release(); inputVideo.release(); shiftMat.release(); }
From source file:opencv.CaptchaDetection.java
/*** * ?//from w ww . j a v a2 s . com * @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()); }
From source file:org.lasarobotics.vision.image.Transform.java
License:Open Source License
/** * Rotate an image by an angle (counterclockwise) * * @param image Transform matrix/* w w w . ja v a 2 s. c o m*/ * @param angle Angle to rotate by (counterclockwise) from -360 to 360 */ public static void rotate(Mat image, double angle) { //Calculate size of new matrix double radians = Math.toRadians(angle); 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); // rotating image Point center = new Point(newWidth / 2, newHeight / 2); Mat rotMatrix = Imgproc.getRotationMatrix2D(center, angle, 1.0); //1.0 means 100 % scale Size size = new Size(newWidth, newHeight); Imgproc.warpAffine(image, image, rotMatrix, image.size()); }
From source file:samples.FtcTestOpenCv.java
License:Open Source License
/** * This method rotate the image to the specified angle. * * @param src specifies the image to be rotated. * @param dst specifies the destination to put the rotated image. * @param angle specifies the rotation angle. *///from w w w.j a v a2 s . c o m private void rotateImage(Mat src, Mat dst, double angle) { angle %= 360.0; if (angle == 0.0) { src.copyTo(dst); } else if (angle == 90.0 || angle == -270.0) { Core.transpose(src, dst); Core.flip(dst, dst, 1); } else if (angle == 180.0 || angle == -180.0) { Core.flip(src, dst, -1); } else if (angle == 270.0 || angle == -90.0) { Core.transpose(src, dst); Core.flip(dst, dst, 0); } else { Mat rotMat = Imgproc.getRotationMatrix2D(new Point(src.cols() / 2.0, src.rows() / 2.0), angle, 1.0); Imgproc.warpAffine(src, dst, rotMat, src.size()); } }