Android OpenCV 旋轉圖像

使用opencv的攝像頭預覽圖像是逆時針旋轉90度的。

c++的方法多一點,我就想java也有直接的辦法。參考API後看到這樣一個方法,在Imgproc類下。

API說明:

getRotationMatrix2D
public static Mat getRotationMatrix2D(Point center,
                      double angle,
                      double scale)
Calculates an affine matrix of 2D rotation.

The function calculates the following matrix:

alpha beta(1- alpha) * center.x - beta * center.y - beta alpha beta * center.x + (1- alpha) * center.y

where

alpha = scale * cos angle, beta = scale * sin angle

The transformation maps the rotation center to itself. If this is not the target, adjust the shift.

Parameters:
center - Center of the rotation in the source image.
angle - Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
scale - Isotropic scale factor.

warpAffine
public static void warpAffine(Mat src,
              Mat dst,
              Mat M,
              Size dsize)
Applies an affine transformation to an image.

The function warpAffine transforms the source image using the specified matrix:

dst(x,y) = src(M _11 x + M _12 y + M _13, M _21 x + M _22 y + M _23)

when the flag WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with "invertAffineTransform" and then put in the formula above instead of M. The function cannot operate in-place.

Note: cvGetQuadrangleSubPix is similar to cvWarpAffine, but the outliers are extrapolated using replication border mode.

Parameters:
src - input image.
dst - output image that has the size dsize and the same type as src.
M - 2x 3 transformation matrix.
dsize - size of the output image.

我在預覽裏添加轉換函數:
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    // TODO Auto-generated method stub
    Mat dst = new Mat();
    Mat gray = inputFrame.gray();
    Mat rotateMat = Imgproc.getRotationMatrix2D(new Point(gray.rows()/2,gray.cols()/2), 90, 1);
    Imgproc.warpAffine(gray, dst, rotateMat, dst.size());
    return dst;
}
圖像順時針旋轉90度


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章