gpt4 book ai didi

android - Android:使用OpenCV对齐图像

转载 作者:行者123 更新时间:2023-12-02 16:17:27 30 4
gpt4 key购买 nike

我正在尝试使用OpenCV对齐两个图像。我基于发现的C++ / Python教程编写此代码:http://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/

android中的findTransformECC()函数需要一个用于inputMask的额外参数。 C++和Python函数没有此功能。

我的代码:

import android.graphics.Bitmap;

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.TermCriteria;
import org.opencv.imgproc.Imgproc;

import static org.opencv.core.CvType.CV_32F;
import static org.opencv.video.Video.MOTION_EUCLIDEAN;
import static org.opencv.video.Video.findTransformECC;

public class ImageProcessor {

public static Bitmap alignImages(Bitmap A, Bitmap B){
final int warp_mode = MOTION_EUCLIDEAN;
Mat matA = new Mat(A.getHeight(),A.getWidth(), CvType.CV_8UC3);
Mat matAgray = new Mat(A.getHeight(),A.getWidth(), CvType.CV_8U);
Mat matB = new Mat(B.getHeight(),B.getWidth(), CvType.CV_8UC3);
Mat matBgray = new Mat(B.getHeight(),B.getWidth(), CvType.CV_8U);
Mat matBaligned = new Mat(A.getHeight(),A.getWidth(), CvType.CV_8UC3);
Mat warpMatrix = Mat.eye(3,3,CV_32F);

Utils.bitmapToMat(A,matA);
Utils.bitmapToMat(B,matB);


Imgproc.cvtColor(matA,matAgray, Imgproc.COLOR_BGR2GRAY);
Imgproc.cvtColor(matB,matBgray,Imgproc.COLOR_BGR2GRAY);

int numIter = 5000;
double terminationEps = 1e-10;
TermCriteria criteria = new TermCriteria(TermCriteria.COUNT+TermCriteria.EPS,numIter,terminationEps);

findTransformECC(matAgray,matBgray,warpMatrix,warp_mode,criteria,matBgray);
Imgproc.warpPerspective(matA,matBaligned,warpMatrix,matA.size(),Imgproc.INTER_LINEAR+ Imgproc.WARP_INVERSE_MAP);
Bitmap alignedBMP = Bitmap.createBitmap(A.getWidth(),A.getHeight(),null);
Utils.matToBitmap(matBaligned,alignedBMP);
return alignedBMP;
}

}

我收到以下错误
W/System.err: CvException [org.opencv.core.CvException: cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp:5987: error: (-215) (M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 2 && M0.cols == 3 in function void cv::warpAffine(cv::InputArray, cv::OutputArray, cv::InputArray, cv::Size, int, int, const Scalar&)
W/System.err: ]
W/System.err: at org.opencv.video.Video.findTransformECC_0(Native Method)
W/System.err: at org.opencv.video.Video.findTransformECC(Video.java:132)
W/System.err: at com.test.imgptest.ImageProcessor.alignImages(ImageProcessor.java:42)
W/System.err: at com.test.imgptest.MainActivity.onActivityResult(MainActivity.java:141)
W/System.err: at android.app.Activity.dispatchActivityResult(Activity.java:6937)
W/System.err: at android.app.ActivityThread.deliverResults(ActivityThread.java:4122)
W/System.err: at android.app.ActivityThread.handleSendResult(ActivityThread.java:4169)
W/System.err: at android.app.ActivityThread.-wrap20(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6186)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

最佳答案

您正在将完整的3x3单应性矩阵warpMatrix输入到findTransformECC,但是您选择的warp_modeMOTION_EUCLIDEAN

如果要使用3x3单应性,则应将warp_mode设置为MOTION_HOMOGRAPHY

如果您要进行欧几里德变换,则只需将最后一行从输入warpMatrix中剪切掉,因为欧几里德变换由2x3矩阵给出。根据 findTransformECC() documentation

MOTION_EUCLIDEAN sets a Euclidean (rigid) transformation as motion model; three parameters are estimated; warpMatrix is 2x3.



然后,由于您将使用 2x3扭曲矩阵,因此请使用 warpAffine() 而不是 warpPerspective()对齐图像。

关于android - Android:使用OpenCV对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383801/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com