简体   繁体   中英

Changing c++ opencv code to android code

Hi I want to change perspective of image and merge it with antoher image. I found example wrote with opencv library in c++, and i tried to convert this to android, but i have error no implementation found for long org.opencv.highgui. This is my code:

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));
}
}

And here is a sample of code in c++ http://ramsrigoutham.com/2014/06/14/perspective-projection-with-homography-opencv/#comment-8114

I import opencv library into my project in android studio by import module.

I think you have not follow proper way. You can not directly conver c++ code to java. To used openCV library in java code they provide java wrapper class for android development. and you must download and import following library -javacpp.jar -javacv.jar -javacv-android-arm.jar

For more detail step please see this blog

This one may help you. If you want more help then feel free to ask.

you can refer following link ,this may be help you.

link1 link2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM