将c ++ opencv代码更改为android代码

时间:2015-03-10 13:08:45

标签: java android c++ opencv

嗨我想改变图像的视角并将其与antoher图像合并。我发现在c ++中使用opencv库编写的示例,我试图将其转换为android,但我发现错误没有找到长org.opencv.highgui的实现。 这是我的代码:

public class TransformationImage {
private MatOfPoint2f pointsOfSuperimposedImage;
private MatOfPoint2f pointsOfMainImage;
private Mat imageMain;
private Mat imagelogo;

public TransformationImage(String imageMainPath, String imageLogoPath) {
    this.imagelogo = Highgui.imread(imageLogoPath);
    this.imageMain = Highgui.imread(imageMainPath);
    pointsOfMainImage = new MatOfPoint2f();
    pointsOfSuperimposedImage = new MatOfPoint2f();
}

public Bitmap transformTwoImages(){
    Mat finalTransformedImage;
    Bitmap bitmap = Bitmap.createBitmap(552, 256, Bitmap.Config.ARGB_4444);
    setMainImageVectors();
    setAppliedImagePosition();
    setWarpPerspectiveOfTwoImages();
    finalTransformedImage = showFinal(pointsOfMainImage, pointsOfMainImage);
    Utils.matToBitmap(finalTransformedImage, bitmap);
    return bitmap;
}

private Mat showFinal(Mat firstImage, Mat secondImage){
    Mat gray = new Mat();
    Mat gray_inv = new Mat();
    Mat src1final = new Mat();
    Mat src2final = new Mat();
    cvtColor(secondImage, gray, Imgproc.COLOR_BGR2GRAY);
    threshold(gray, gray, 0, 255, 0);
    bitwise_not(gray, gray_inv);
    firstImage.copyTo(src2final, gray_inv);
    secondImage.copyTo(src1final, gray);
    addWeighted(src1final, 0, src2final, 0, 0, src1final);
    return src1final;
}
private void setWarpPerspectiveOfTwoImages(){
    Imgproc.warpPerspective(imagelogo, imageMain, Calib3d.findHomography(pointsOfSuperimposedImage, pointsOfMainImage), imageMain.size());
}
private void setMainImageVectors(){
    List<Point> src_pnt = new ArrayList<>();
    Point p0 = new Point(0, 0);
    src_pnt.add(p0);
    Point p1 = new Point(552, 0);
    src_pnt.add(p1);
    Point p2 = new Point(0, 256);
    src_pnt.add(p2);
    Point p3 = new Point(552, 256);
    src_pnt.add(p3);
    pointsOfMainImage.push_back(Converters.vector_Point2f_to_Mat(src_pnt));
}
private void setAppliedImagePosition(){
    List<Point> src_pnt = new ArrayList<>();
    Point p0 = new Point(0, 0);
    src_pnt.add(p0);
    Point p1 = new Point(50, 0);
    src_pnt.add(p1);
    Point p2 = new Point(50, 20);
    src_pnt.add(p2);
    Point p3 = new Point(20, 0);
    src_pnt.add(p3);
    pointsOfSuperimposedImage.push_back(Converters.vector_Point2f_to_Mat(src_pnt));
}
}

以下是c ++ http://ramsrigoutham.com/2014/06/14/perspective-projection-with-homography-opencv/#comment-8114

中的代码示例

我通过导入模块将opencv库导入android studio中的项目。

2 个答案:

答案 0 :(得分:0)

我认为你没有按照正确的方式行事。你不能直接将c ++代码转换为java。 为了在java代码中使用openCV库,他们为android开发提供了java包装类。并且您必须下载并导入以下库-javacpp.jar -javacv.jar -javacv-android-arm.jar

有关详细信息,请参阅this博客

这个可以帮到你。如果您需要更多帮助,请随时提出。

答案 1 :(得分:0)

您可以参考以下链接,这可能会对您有所帮助。

link1 link2