Java Code Examples for org.opencv.imgproc.Imgproc#warpAffine()

The following examples show how to use org.opencv.imgproc.Imgproc#warpAffine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RotationUtils.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * 垂直旋转90度
 * 
 * @param mat
 * @return
 */
public static Mat rotation(Mat mat) {
	int len, wid;
	if (mat.width() > mat.height()) {
		len = mat.width();
		wid = mat.height();
	} else {
		len = mat.height();
		wid = mat.width();
	}

	// 得到旋转矩阵算子
	Mat matrix = Imgproc.getRotationMatrix2D(new Point(len / 2, len / 2), 90, 1);

	Mat CorrectImg = new Mat(new Size(len, len), mat.type());

	Imgproc.warpAffine(mat, CorrectImg, matrix, CorrectImg.size(), Imgproc.INTER_LINEAR, 0, new Scalar(0, 0, 0));

	int extend = 18;

	Mat temp = new Mat(CorrectImg, new Rect(0, len - wid + extend, len, wid - 2 * extend));

	return temp;
}
 
Example 2
Source File: EyeAlignment.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 6 votes vote down vote up
public PreProcessor preprocessImage(PreProcessor preProcessor) {
    List<Mat> images = preProcessor.getImages();
    List<Mat> processed = new ArrayList<Mat>();
    Eyes[] eyes = preProcessor.setEyes();
    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;
}
 
Example 3
Source File: Transform.java    From FTCVision with MIT License 6 votes vote down vote up
/**
 * Rotate an image by an angle (counterclockwise)
 *
 * @param image Transform matrix
 * @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());
}
 
Example 4
Source File: FtcTestOpenCv.java    From FtcSamples with MIT License 5 votes vote down vote up
/**
 * 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.
 */
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());
    }
}
 
Example 5
Source File: ImageUtils.java    From AndroidFaceRecognizer with MIT License 5 votes vote down vote up
public static Mat cropFace(Mat image, Point left_eye, Point right_eye,
		double offsetPercentage, int destWidth, int destHeight){
	
	int offset_horizontal = (int)Math.floor(offsetPercentage*(double)destWidth); 
	int offset_vertical = (int)Math.floor(offsetPercentage*(double)destHeight);
	
	double distance = getDistance(left_eye, right_eye);

	double rotation = getRotationToAlign(left_eye, right_eye);
	double reference = destWidth - 2*offset_horizontal;
	double scaleFactor = distance / reference;
	
	
   	Mat rot_mat = Imgproc.getRotationMatrix2D(left_eye, rotation, 1.0);
   	Mat dst = new Mat(image.rows(), image.cols(), image.type());
   	Imgproc.warpAffine(image, dst, rot_mat, new Size(destWidth, destHeight), Imgproc.INTER_CUBIC);
   	
   	Size size = new Size(destWidth*scaleFactor, destHeight*scaleFactor);
   	Point ctr = new Point(left_eye.x - scaleFactor*offset_horizontal+size.width/2,
   			left_eye.y - scaleFactor*offset_vertical+size.height/2);
   	
   	Mat cropped = new Mat();
   	Imgproc.getRectSubPix(dst, size, ctr, cropped);
   	Mat returnMatrix = new Mat();
   	Imgproc.resize(cropped, returnMatrix, new Size(destWidth, destHeight));
	
   	return returnMatrix;
}
 
Example 6
Source File: PatchGenerator.java    From OpenTLDAndroid with Apache License 2.0 5 votes vote down vote up
/**
	 * 
	 * @param image
	 * @param T
	 * @param patch OUTPUT
	 * @param patchSize
	 */
	void generate(final Mat image, final Mat T, Mat patch, Size patchSize, final RNG rng){
	    patch.create( patchSize, image.type() );
	    if( backgroundMin != backgroundMax ) {
	    	Core.randu(patch, backgroundMin, backgroundMax);
	    	// TODO if that null scalar OK or should it be new Scalar(0) ?
	    	Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_TRANSPARENT, null);
	    } else {
	    	Imgproc.warpAffine(image, patch, T, patchSize, Imgproc.INTER_LINEAR, Imgproc.BORDER_CONSTANT, new Scalar(backgroundMin));
	    }

	    int ksize = randomBlur ? rng.nextInt() % 9 - 5 : 0;
	    if( ksize > 0 ) {
	        ksize = ksize * 2 + 1;
	        Imgproc.GaussianBlur(patch, patch, new Size(ksize, ksize), 0, 0);
	    }

	    if( noiseRange > 0 ) {
	        final Mat noise = new Mat(patchSize, image.type());
	        int delta = (image.depth() == CvType.CV_8U ? 128 : (image.depth() == CvType.CV_16U ? 32768 : 0));
	        Core.randn(noise, delta, noiseRange);
	        
	        // TODO this was different !!
	        Core.addWeighted(patch, 1, noise, 1, -delta, patch);
	        
//	        if( backgroundMin != backgroundMax )
//	            addWeighted(patch, 1, noise, 1, -delta, patch);
//	        else
//	        {
//	            for( int i = 0; i < patchSize.height; i++ )
//	            {
//	                uchar* prow = patch.ptr<uchar>(i);
//	                const uchar* nrow =  noise.ptr<uchar>(i);
//	                for( int j = 0; j < patchSize.width; j++ )
//	                    if( prow[j] != backgroundMin )
//	                        prow[j] = saturate_cast<uchar>(prow[j] + nrow[j] - delta);
//	            }
//	        }
	    }		
	}